skins.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview Defines the {@link CKEDITOR.skins} object, which is used to
  7. * manage skins loading.
  8. */
  9. /**
  10. * Manages skins loading.
  11. * @namespace
  12. * @example
  13. */
  14. CKEDITOR.skins = (function()
  15. {
  16. // Holds the list of loaded skins.
  17. var loaded = {},
  18. paths = {};
  19. var loadPart = function( editor, skinName, part, callback )
  20. {
  21. // Get the skin definition.
  22. var skinDefinition = loaded[ skinName ];
  23. if ( !editor.skin )
  24. {
  25. editor.skin = skinDefinition;
  26. // Trigger init function if any.
  27. if ( skinDefinition.init )
  28. skinDefinition.init( editor );
  29. }
  30. var appendSkinPath = function( fileNames )
  31. {
  32. for ( var n = 0 ; n < fileNames.length ; n++ )
  33. {
  34. fileNames[ n ] = CKEDITOR.getUrl( paths[ skinName ] + fileNames[ n ] );
  35. }
  36. };
  37. function fixCSSTextRelativePath( cssStyleText, baseUrl )
  38. {
  39. return cssStyleText.replace( /url\s*\(([\s'"]*)(.*?)([\s"']*)\)/g,
  40. function( match, opener, path, closer )
  41. {
  42. if ( /^\/|^\w?:/.test( path ) )
  43. return match;
  44. else
  45. return 'url(' + baseUrl + opener + path + closer + ')';
  46. } );
  47. }
  48. // Get the part definition.
  49. part = skinDefinition[ part ];
  50. var partIsLoaded = !part || !!part._isLoaded;
  51. // Call the callback immediately if already loaded.
  52. if ( partIsLoaded )
  53. callback && callback();
  54. else
  55. {
  56. // Put the callback in a queue.
  57. var pending = part._pending || ( part._pending = [] );
  58. pending.push( callback );
  59. // We may have more than one skin part load request. Just the first
  60. // one must do the loading job.
  61. if ( pending.length > 1 )
  62. return;
  63. // Check whether the "css" and "js" properties have been defined
  64. // for that part.
  65. var cssIsLoaded = !part.css || !part.css.length,
  66. jsIsLoaded = !part.js || !part.js.length;
  67. // This is the function that will trigger the callback calls on
  68. // load.
  69. var checkIsLoaded = function()
  70. {
  71. if ( cssIsLoaded && jsIsLoaded )
  72. {
  73. // Mark the part as loaded.
  74. part._isLoaded = 1;
  75. // Call all pending callbacks.
  76. for ( var i = 0 ; i < pending.length ; i++ )
  77. {
  78. if ( pending[ i ] )
  79. pending[ i ]();
  80. }
  81. }
  82. };
  83. // Load the "css" pieces.
  84. if ( !cssIsLoaded )
  85. {
  86. var cssPart = part.css;
  87. if ( CKEDITOR.tools.isArray( cssPart ) )
  88. {
  89. appendSkinPath( cssPart );
  90. for ( var c = 0 ; c < cssPart.length ; c++ )
  91. CKEDITOR.document.appendStyleSheet( cssPart[ c ] );
  92. }
  93. else
  94. {
  95. cssPart = fixCSSTextRelativePath(
  96. cssPart, CKEDITOR.getUrl( paths[ skinName ] ) );
  97. // Processing Inline CSS part.
  98. CKEDITOR.document.appendStyleText( cssPart );
  99. }
  100. part.css = cssPart;
  101. cssIsLoaded = 1;
  102. }
  103. // Load the "js" pieces.
  104. if ( !jsIsLoaded )
  105. {
  106. appendSkinPath( part.js );
  107. CKEDITOR.scriptLoader.load( part.js, function()
  108. {
  109. jsIsLoaded = 1;
  110. checkIsLoaded();
  111. });
  112. }
  113. // We may have nothing to load, so check it immediately.
  114. checkIsLoaded();
  115. }
  116. };
  117. return /** @lends CKEDITOR.skins */ {
  118. /**
  119. * Registers a skin definition.
  120. * @param {String} skinName The skin name.
  121. * @param {Object} skinDefinition The skin definition.
  122. * @example
  123. */
  124. add : function( skinName, skinDefinition )
  125. {
  126. loaded[ skinName ] = skinDefinition;
  127. skinDefinition.skinPath = paths[ skinName ]
  128. || ( paths[ skinName ] =
  129. CKEDITOR.getUrl(
  130. '_source/' + // @Packager.RemoveLine
  131. 'skins/' + skinName + '/' ) );
  132. },
  133. /**
  134. * Loads a skin part. Skins are defined in parts, which are basically
  135. * separated CSS files. This function is mainly used by the core code and
  136. * should not have much use out of it.
  137. * @param {String} skinName The name of the skin to be loaded.
  138. * @param {String} skinPart The skin part to be loaded. Common skin parts
  139. * are "editor" and "dialog".
  140. * @param {Function} [callback] A function to be called once the skin
  141. * part files are loaded.
  142. * @example
  143. */
  144. load : function( editor, skinPart, callback )
  145. {
  146. var skinName = editor.skinName,
  147. skinPath = editor.skinPath;
  148. if ( loaded[ skinName ] )
  149. loadPart( editor, skinName, skinPart, callback );
  150. else
  151. {
  152. paths[ skinName ] = skinPath;
  153. CKEDITOR.scriptLoader.load( CKEDITOR.getUrl( skinPath + 'skin.js' ), function()
  154. {
  155. loadPart( editor, skinName, skinPart, callback );
  156. });
  157. }
  158. }
  159. };
  160. })();