2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.skins} object, which is used to
8 * manage skins loading.
12 * Manages skins loading.
16 CKEDITOR.skins = (function()
18 // Holds the list of loaded skins.
22 var loadPart = function( editor, skinName, part, callback )
24 // Get the skin definition.
25 var skinDefinition = loaded[ skinName ];
29 editor.skin = skinDefinition;
31 // Trigger init function if any.
32 if ( skinDefinition.init )
33 skinDefinition.init( editor );
36 var appendSkinPath = function( fileNames )
38 for ( var n = 0 ; n < fileNames.length ; n++ )
40 fileNames[ n ] = CKEDITOR.getUrl( paths[ skinName ] + fileNames[ n ] );
44 function fixCSSTextRelativePath( cssStyleText, baseUrl )
46 return cssStyleText.replace( /url\s*\(([\s'"]*)(.*?)([\s"']*)\)/g,
47 function( match, opener, path, closer )
49 if ( /^\/|^\w?:/.test( path ) )
52 return 'url(' + baseUrl + opener + path + closer + ')';
56 // Get the part definition.
57 part = skinDefinition[ part ];
58 var partIsLoaded = !part || !!part._isLoaded;
60 // Call the callback immediately if already loaded.
62 callback && callback();
65 // Put the callback in a queue.
66 var pending = part._pending || ( part._pending = [] );
67 pending.push( callback );
69 // We may have more than one skin part load request. Just the first
70 // one must do the loading job.
71 if ( pending.length > 1 )
74 // Check whether the "css" and "js" properties have been defined
76 var cssIsLoaded = !part.css || !part.css.length,
77 jsIsLoaded = !part.js || !part.js.length;
79 // This is the function that will trigger the callback calls on
81 var checkIsLoaded = function()
83 if ( cssIsLoaded && jsIsLoaded )
85 // Mark the part as loaded.
88 // Call all pending callbacks.
89 for ( var i = 0 ; i < pending.length ; i++ )
97 // Load the "css" pieces.
100 var cssPart = part.css;
102 if ( CKEDITOR.tools.isArray( cssPart ) )
104 appendSkinPath( cssPart );
105 for ( var c = 0 ; c < cssPart.length ; c++ )
106 CKEDITOR.document.appendStyleSheet( cssPart[ c ] );
110 cssPart = fixCSSTextRelativePath(
111 cssPart, CKEDITOR.getUrl( paths[ skinName ] ) );
112 // Processing Inline CSS part.
113 CKEDITOR.document.appendStyleText( cssPart );
121 // Load the "js" pieces.
124 appendSkinPath( part.js );
125 CKEDITOR.scriptLoader.load( part.js, function()
132 // We may have nothing to load, so check it immediately.
137 return /** @lends CKEDITOR.skins */ {
140 * Registers a skin definition.
141 * @param {String} skinName The skin name.
142 * @param {Object} skinDefinition The skin definition.
145 add : function( skinName, skinDefinition )
147 loaded[ skinName ] = skinDefinition;
149 skinDefinition.skinPath = paths[ skinName ]
150 || ( paths[ skinName ] =
152 '_source/' + // @Packager.RemoveLine
153 'skins/' + skinName + '/' ) );
157 * Loads a skin part. Skins are defined in parts, which are basically
158 * separated CSS files. This function is mainly used by the core code and
159 * should not have much use out of it.
160 * @param {String} skinName The name of the skin to be loaded.
161 * @param {String} skinPart The skin part to be loaded. Common skin parts
162 * are "editor" and "dialog".
163 * @param {Function} [callback] A function to be called once the skin
164 * part files are loaded.
167 load : function( editor, skinPart, callback )
169 var skinName = editor.skinName,
170 skinPath = editor.skinPath;
172 if ( loaded[ skinName ] )
173 loadPart( editor, skinName, skinPart, callback );
176 paths[ skinName ] = skinPath;
177 CKEDITOR.scriptLoader.load( CKEDITOR.getUrl( skinPath + 'skin.js' ), function()
179 loadPart( editor, skinName, skinPart, callback );