Source: env/media/waitReadyIndicator.js

  1. /*
  2. * Copyright (C) 2012-2016 DFKI GmbH
  3. * Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
  4. * German Research Center for Artificial Intelligence
  5. * http://www.dfki.de
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. /**
  27. * Default / standard implementation for wait-/ready-indication:
  28. *
  29. * media-modules may signal that they are <code>preparing</code> a resource, and then that
  30. * they are <code>ready</code> via the {@link mmir.MediaManager#_preparing} and
  31. * {@link mmir.MediaManager#_ready}.
  32. *
  33. * This implementation provides a simple mechanism for showing these states to the user:
  34. * upon <em>preparing</em> an overlay with a "please wait" message is shown, and upon
  35. * <em>ready</em> the overlay is hidden again.
  36. *
  37. * See {@link #setWaitCaption} for details on setting a custom text message.
  38. *
  39. * In order to load this implementation, add the entry <code>"waitReadyIndicator"</code>
  40. * to the <code>mediaManager.plugins</code> list in the MMIR configuration file
  41. * at <code>config/configuration.json</code>:
  42. * <pre>
  43. * ...
  44. * "mediaManager": {
  45. * "plugins": {
  46. * "browser": ["waitReadyIndicator",
  47. * "html5AudioOutput",
  48. * ...
  49. * ],
  50. * "cordova": ["waitReadyIndicator",
  51. * "androidAudioInput",
  52. * ...
  53. * ]
  54. * ...
  55. * }
  56. * },
  57. * ...
  58. * </pre>
  59. *
  60. * @example
  61. *
  62. * //starting to prepare a resource:
  63. * mmir.MediaManager._preparing();
  64. *
  65. * // do something ...
  66. *
  67. * //... when the resouce has been prepared
  68. * // and is ready to be used:
  69. * mmir.MediaManager._ready();
  70. *
  71. * @class
  72. * @public
  73. * @name WaitReadyIndicatorImpl
  74. * @memberOf mmir.env.media
  75. *
  76. * @requires stlne-wait-dlg (waitDialog module and CSS)
  77. */
  78. newMediaPlugin = {
  79. /** @memberOf WaitReadyIndicatorImpl# */
  80. initialize: function(callBack, mediaManager){//, ctxId, moduleConfig){//DISABLED this argument is currently un-used -> disabled
  81. /**
  82. * legacy mode: use pre-v4 API of mmir-lib
  83. * @memberOf WaitReadyIndicatorImpl#
  84. */
  85. var _isLegacyMode = true;
  86. /**
  87. * Reference to the mmir-lib core (only available in non-legacy mode)
  88. * @type mmir
  89. * @memberOf WaitReadyIndicatorImpl#
  90. */
  91. var _mmir = null;
  92. if(mediaManager._get_mmir){
  93. //_get_mmir() is only available for >= v4
  94. _mmir = mediaManager._get_mmir();
  95. //just to make sure: set legacy-mode if version is < v4
  96. _isLegacyMode = _mmir? _mmir.isVersion(4, '<') : true;
  97. }
  98. /**
  99. * HELPER for require():
  100. * use module IDs (and require instance) depending on legacy mode
  101. *
  102. * @param {String} id
  103. * the require() module ID
  104. *
  105. * @returns {any} the require()'ed module
  106. *
  107. * @memberOf WaitReadyIndicatorImpl#
  108. */
  109. var _req = function(id){
  110. var name = (_isLegacyMode? '' : 'mmirf/') + id;
  111. return _mmir? _mmir.require(name) : require(name);
  112. };
  113. _req(['waitDialog'], function(dlg){
  114. /** @memberOf WaitReadyIndicatorImpl# */
  115. var _pluginName = 'waitReadyIndicator';
  116. /**
  117. * @type mmir.LanguageManager
  118. * @memberOf WaitReadyIndicatorImpl#
  119. */
  120. var languageManager = _req('languageManager');
  121. /** @memberOf WaitReadyIndicatorImpl# */
  122. var _id = 'media-plugin-wait';
  123. /** @memberOf WaitReadyIndicatorImpl# */
  124. var caption;
  125. /** @memberOf WaitReadyIndicatorImpl# */
  126. var cssUrl = 'mmirf/vendor/styles/' + dlg.styleUrl;//TODO make this configurable / retrieve this setting from somewhere
  127. //load the stylesheet file for the wait-dialog
  128. // (does nothing, if this load-function was already called before)
  129. dlg._loadStyle(cssUrl);
  130. //create the DOM elements for the wait dialog (hidden)
  131. dlg.create(_id);//, {type: 'verbose', theme: 'a'});//DISABLED: these are the default options
  132. //invoke the passed-in initializer-callback and export the public functions:
  133. callBack({waitReadyImpl: {
  134. /**
  135. * Shows wait dialog.
  136. *
  137. * @public
  138. * @memberOf WaitReadyIndicatorImpl.prototype
  139. * @see mmir.MediaManager#_preparing
  140. */
  141. preparing: function (){
  142. var text = typeof caption !== 'undefined'? caption : languageManager.getText('loadingText');
  143. dlg.show(text, _id);
  144. },
  145. /**
  146. * Hides wait dialog.
  147. *
  148. * @public
  149. * @memberOf WaitReadyIndicatorImpl.prototype
  150. * @see mmir.MediaManager#_ready
  151. */
  152. ready: function(){
  153. dlg.hide(_id);
  154. },
  155. /**
  156. * Set caption for wait dialog.
  157. *
  158. * <p>
  159. * By default (i.e. not set), the dictionary entry for
  160. * "loadingText" is used as caption / label.
  161. *
  162. * @param {String} text
  163. * set the caption / label for the wait-dialog.<br>
  164. * If <code>undefined</code>, the default caption will be used.
  165. *
  166. * @public
  167. * @memberOf WaitReadyIndicatorImpl.prototype
  168. * @see mmir.MediaManager#ready
  169. * @see mmir.LanguageManager#getText
  170. */
  171. setWaitCaption: function(text){
  172. caption = text;
  173. },
  174. /**
  175. * Get current caption for wait dialog.
  176. *
  177. * NOTE if none is set, then internally the value of "loadingText"
  178. * property of the current language dictionary will be used.
  179. *
  180. * @public
  181. * @memberOf WaitReadyIndicatorImpl.prototype
  182. * @see mmir.MediaManager#_ready
  183. * @see #setWaitCaption
  184. */
  185. getWaitCaption: function(){
  186. return caption;
  187. }
  188. }});
  189. });
  190. }
  191. };