Source: core/kekule.root.js

  1. /**
  2. * Defines root namespace of Kekule.js.
  3. */
  4. /**
  5. * @namespace
  6. * @description Root namespace of Kekule library.
  7. */
  8. var Kekule = {
  9. LIBNAME: 'Kekule.js',
  10. LIBNAME_CORE: 'Kekule',
  11. VERSION: '0.8.1.18112300',
  12. /**
  13. * A flag that indicate whether all essential Kekule modules are loaded into document.
  14. * @ignore
  15. */
  16. LOADED: false,
  17. /**
  18. * An array of functions that need be called after load all Kekule modules.
  19. * @private
  20. */
  21. _afterLoadSysProcedures: [],
  22. /**
  23. * An array of user functions that need be called after load all Kekule modules.
  24. * @private
  25. */
  26. _afterLoadUserProcedures: [],
  27. // Whether auto find title and description text for object property
  28. /** @ignore */
  29. PROP_AUTO_TITLE: true
  30. };
  31. /**
  32. * Called when all essential modules is loaded.
  33. * User should not call this function directly.
  34. * @private
  35. */
  36. Kekule._loaded = function()
  37. {
  38. if (Kekule.LOADED)
  39. return;
  40. Kekule.LOADED = true;
  41. var procs = Kekule._afterLoadSysProcedures;
  42. while (procs.length)
  43. {
  44. var proc = procs.shift();
  45. if (proc)
  46. proc();
  47. }
  48. var procs = Kekule._afterLoadUserProcedures;
  49. while (procs.length)
  50. {
  51. var proc = procs.shift();
  52. if (proc)
  53. proc();
  54. }
  55. // at last try fire a custom event
  56. var doc = Kekule.$jsRoot && Kekule.$jsRoot.document;
  57. if (doc && doc.createEvent && doc.body && doc.body.dispatchEvent)
  58. {
  59. var event = doc.createEvent('Event');
  60. event.initEvent('kekuleload', true, true);
  61. doc.body.dispatchEvent(event);
  62. }
  63. };
  64. /**
  65. * Return whether the whole lib is loaded.
  66. * @returns {boolean}
  67. * @private
  68. */
  69. Kekule._isLoaded = function()
  70. {
  71. return Kekule.LOADED;
  72. };
  73. /**
  74. * Register system procedure that need to be called after all modules are loaded.
  75. * User should not call this method directly.
  76. * @param {Func} proc
  77. * @private
  78. */
  79. Kekule._registerAfterLoadSysProc = function(proc)
  80. {
  81. if (proc)
  82. {
  83. if (Kekule.LOADED)
  84. proc();
  85. else
  86. Kekule._afterLoadSysProcedures.push(proc);
  87. }
  88. };
  89. /**
  90. * Register procedure that need to be called after all modules are loaded and all initial operations has been done.
  91. * @param {Func} proc
  92. * @private
  93. */
  94. Kekule._ready = function(proc)
  95. {
  96. if (proc)
  97. {
  98. if (Kekule.LOADED)
  99. proc();
  100. else
  101. Kekule._afterLoadUserProcedures.push(proc);
  102. }
  103. };
  104. Kekule._registerAfterLoadProc = Kekule._ready; // for backward
  105. /**
  106. * Root object of JavaScript environment, usually window.
  107. */
  108. Kekule.$jsRoot = this;
  109. /**
  110. * Root document of JavaScript environment.
  111. * Can be null in Node.js.
  112. */
  113. Kekule.$document = this.document || null;
  114. Kekule.scriptSrcInfo = Kekule.$jsRoot['__$kekule_load_info__'];
  115. if (Kekule.scriptSrcInfo && Kekule.scriptSrcInfo.language) // force Language
  116. {
  117. Kekule.language = Kekule.scriptSrcInfo.language;
  118. }
  119. if (!Kekule.scriptSrcInfo && Kekule.$jsRoot.document) // script info not found, may be use Kekule.min.js directly
  120. {
  121. Kekule.scriptSrcInfo = (function ()
  122. {
  123. var entranceSrc = /^(.*\/?)kekule\..*\.js(\?.*)?$/;
  124. var scriptElems = document.getElementsByTagName('script');
  125. var loc;
  126. for (var i = scriptElems.length - 1; i >= 0; --i)
  127. {
  128. var elem = scriptElems[i];
  129. if (elem.src)
  130. {
  131. var matchResult = elem.src.match(entranceSrc);
  132. if (matchResult)
  133. {
  134. var pstr = matchResult[2];
  135. if (pstr)
  136. pstr = pstr.substr(1); // eliminate starting '?'
  137. var result = {
  138. 'src': elem.src,
  139. 'path': matchResult[1],
  140. 'paramStr': pstr,
  141. 'useMinFile': true
  142. };
  143. return result;
  144. }
  145. }
  146. }
  147. return null;
  148. })();
  149. }
  150. Kekule.getScriptPath = function()
  151. {
  152. return Kekule.scriptSrcInfo.path;
  153. };
  154. Kekule.getScriptSrc = function()
  155. {
  156. return Kekule.scriptSrcInfo.src;
  157. };
  158. Kekule.getStyleSheetPath = function()
  159. {
  160. //var cssFileName = 'themes/default/kekule.css';
  161. var cssPath;
  162. var scriptInfo = Kekule.scriptSrcInfo;
  163. if (scriptInfo.useMinFile)
  164. cssPath = scriptInfo.path;
  165. else
  166. cssPath = scriptInfo.path + 'widgets/';
  167. return cssPath;
  168. };
  169. Kekule.getStyleSheetUrl = function()
  170. {
  171. var path = Kekule.getStyleSheetPath();
  172. return path + 'themes/default/kekule.css';
  173. };
  174. if (Kekule.$jsRoot && Kekule.$jsRoot.addEventListener && Kekule.$jsRoot.postMessage)
  175. {
  176. // response to special message, returns Kekule sys info.
  177. // This query is usually requested by browser addon to check
  178. // if Kekule lib is loaded into a web page
  179. Kekule.$jsRoot.addEventListener('message', function(event)
  180. {
  181. if (event.data === 'kekule-sys-info-query')
  182. {
  183. Kekule.$jsRoot.postMessage({
  184. 'msg': 'kekule-sys-info-result',
  185. 'libName': Kekule.LIBNAME,
  186. 'version': Kekule.VERSION
  187. //'scriptSrcInfo': Kekule.scriptSrcInfo
  188. }, '*');
  189. }
  190. }, false);
  191. }