skin.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview Defines the {@link CKEDITOR.skin} class that is used to manage skin parts.
  7. */
  8. ( function() {
  9. var cssLoaded = {};
  10. function getName() {
  11. return CKEDITOR.skinName.split( ',' )[ 0 ];
  12. }
  13. function getConfigPath() {
  14. return CKEDITOR.getUrl( CKEDITOR.skinName.split( ',' )[ 1 ] || ( 'skins/' + getName() + '/' ) );
  15. }
  16. /**
  17. * Manages the loading of skin parts among all editor instances.
  18. *
  19. * @class
  20. * @singleton
  21. */
  22. CKEDITOR.skin = {
  23. /**
  24. * Returns the root path to the skin directory.
  25. *
  26. * @method
  27. * @todo
  28. */
  29. path: getConfigPath,
  30. /**
  31. * Loads a skin part into the page. Does nothing if the part has already been loaded.
  32. *
  33. * **Note:** The "editor" part is always auto loaded upon instance creation,
  34. * thus this function is mainly used to **lazy load** other parts of the skin
  35. * that do not have to be displayed until requested.
  36. *
  37. * // Load the dialog part.
  38. * editor.skin.loadPart( 'dialog' );
  39. *
  40. * @param {String} part The name of the skin part CSS file that resides in the skin directory.
  41. * @param {Function} fn The provided callback function which is invoked after the part is loaded.
  42. */
  43. loadPart: function( part, fn ) {
  44. if ( CKEDITOR.skin.name != getName() ) {
  45. CKEDITOR.scriptLoader.load( CKEDITOR.getUrl( getConfigPath() + 'skin.js' ), function() {
  46. loadCss( part, fn );
  47. } );
  48. } else {
  49. loadCss( part, fn );
  50. }
  51. },
  52. /**
  53. * Retrieves the real URL of a (CSS) skin part.
  54. *
  55. * @param {String} part
  56. */
  57. getPath: function( part ) {
  58. return CKEDITOR.getUrl( getCssPath( part ) );
  59. },
  60. /**
  61. * The list of registered icons. To add new icons to this list, use {@link #addIcon}.
  62. */
  63. icons: {},
  64. /**
  65. * Registers an icon.
  66. *
  67. * @param {String} name The icon name.
  68. * @param {String} path The path to the icon image file.
  69. * @param {Number} [offset] The vertical offset position of the icon, if
  70. * available inside a strip image.
  71. * @param {String} [bgsize] The value of the CSS "background-size" property to
  72. * use for this icon
  73. */
  74. addIcon: function( name, path, offset, bgsize ) {
  75. name = name.toLowerCase();
  76. if ( !this.icons[ name ] ) {
  77. this.icons[ name ] = {
  78. path: path,
  79. offset: offset || 0,
  80. bgsize: bgsize || '16px'
  81. };
  82. }
  83. },
  84. /**
  85. * Gets the CSS background styles to be used to render a specific icon.
  86. *
  87. * @param {String} name The icon name, as registered with {@link #addIcon}.
  88. * @param {Boolean} [rtl] Indicates that the RTL version of the icon is
  89. * to be used, if available.
  90. * @param {String} [overridePath] The path to the icon image file. It
  91. * overrides the path defined by the named icon, if available, and is
  92. * used if the named icon was not registered.
  93. * @param {Number} [overrideOffset] The vertical offset position of the
  94. * icon. It overrides the offset defined by the named icon, if
  95. * available, and is used if the named icon was not registered.
  96. * @param {String} [overrideBgsize] The value of the CSS "background-size" property
  97. * to use for the icon. It overrides the value defined by the named icon,
  98. * if available, and is used if the named icon was not registered.
  99. */
  100. getIconStyle: function( name, rtl, overridePath, overrideOffset, overrideBgsize ) {
  101. var icon, path, offset, bgsize;
  102. if ( name ) {
  103. name = name.toLowerCase();
  104. // If we're in RTL, try to get the RTL version of the icon.
  105. if ( rtl )
  106. icon = this.icons[ name + '-rtl' ];
  107. // If not in LTR or no RTL version available, get the generic one.
  108. if ( !icon )
  109. icon = this.icons[ name ];
  110. }
  111. path = overridePath || ( icon && icon.path ) || '';
  112. offset = overrideOffset || ( icon && icon.offset );
  113. bgsize = overrideBgsize || ( icon && icon.bgsize ) || '16px';
  114. return path &&
  115. ( 'background-image:url(' + CKEDITOR.getUrl( path ) + ');background-position:0 ' + offset + 'px;background-size:' + bgsize + ';' );
  116. }
  117. };
  118. function getCssPath( part ) {
  119. // Check for ua-specific version of skin part.
  120. var uas = CKEDITOR.skin[ 'ua_' + part ], env = CKEDITOR.env;
  121. if ( uas ) {
  122. // Having versioned UA checked first.
  123. uas = uas.split( ',' ).sort( function( a, b ) {
  124. return a > b ? -1 : 1;
  125. } );
  126. // Loop through all ua entries, checking is any of them match the current ua.
  127. for ( var i = 0, ua; i < uas.length; i++ ) {
  128. ua = uas[ i ];
  129. if ( env.ie ) {
  130. if ( ( ua.replace( /^ie/, '' ) == env.version ) || ( env.quirks && ua == 'iequirks' ) )
  131. ua = 'ie';
  132. }
  133. if ( env[ ua ] ) {
  134. part += '_' + uas[ i ];
  135. break;
  136. }
  137. }
  138. }
  139. return CKEDITOR.getUrl( getConfigPath() + part + '.css' );
  140. }
  141. function loadCss( part, callback ) {
  142. // Avoid reload.
  143. if ( !cssLoaded[ part ] ) {
  144. CKEDITOR.document.appendStyleSheet( getCssPath( part ) );
  145. cssLoaded[ part ] = 1;
  146. }
  147. // CSS loading should not be blocking.
  148. callback && callback();
  149. }
  150. CKEDITOR.tools.extend( CKEDITOR.editor.prototype, {
  151. /** Gets the color of the editor user interface.
  152. *
  153. * CKEDITOR.instances.editor1.getUiColor();
  154. *
  155. * @method
  156. * @member CKEDITOR.editor
  157. * @returns {String} uiColor The editor UI color or `undefined` if the UI color is not set.
  158. */
  159. getUiColor: function() {
  160. return this.uiColor;
  161. },
  162. /** Sets the color of the editor user interface. This method accepts a color value in
  163. * hexadecimal notation, with a `#` character (e.g. #ffffff).
  164. *
  165. * CKEDITOR.instances.editor1.setUiColor( '#ff00ff' );
  166. *
  167. * @method
  168. * @member CKEDITOR.editor
  169. * @param {String} color The desired editor UI color in hexadecimal notation.
  170. */
  171. setUiColor: function( color ) {
  172. var uiStyle = getStylesheet( CKEDITOR.document );
  173. return ( this.setUiColor = function( color ) {
  174. this.uiColor = color;
  175. var chameleon = CKEDITOR.skin.chameleon,
  176. editorStyleContent = '',
  177. panelStyleContent = '';
  178. if ( typeof chameleon == 'function' ) {
  179. editorStyleContent = chameleon( this, 'editor' );
  180. panelStyleContent = chameleon( this, 'panel' );
  181. }
  182. var replace = [ [ uiColorRegexp, color ] ];
  183. // Update general style.
  184. updateStylesheets( [ uiStyle ], editorStyleContent, replace );
  185. // Update panel styles.
  186. updateStylesheets( uiColorMenus, panelStyleContent, replace );
  187. } ).call( this, color );
  188. }
  189. } );
  190. var uiColorStylesheetId = 'cke_ui_color',
  191. uiColorMenus = [],
  192. uiColorRegexp = /\$color/g;
  193. function getStylesheet( document ) {
  194. var node = document.getById( uiColorStylesheetId );
  195. if ( !node ) {
  196. node = document.getHead().append( 'style' );
  197. node.setAttribute( 'id', uiColorStylesheetId );
  198. node.setAttribute( 'type', 'text/css' );
  199. }
  200. return node;
  201. }
  202. function updateStylesheets( styleNodes, styleContent, replace ) {
  203. var r, i, content;
  204. // We have to split CSS declarations for webkit.
  205. if ( CKEDITOR.env.webkit ) {
  206. styleContent = styleContent.split( '}' ).slice( 0, -1 );
  207. for ( i = 0; i < styleContent.length; i++ )
  208. styleContent[ i ] = styleContent[ i ].split( '{' );
  209. }
  210. for ( var id = 0; id < styleNodes.length; id++ ) {
  211. if ( CKEDITOR.env.webkit ) {
  212. for ( i = 0; i < styleContent.length; i++ ) {
  213. content = styleContent[ i ][ 1 ];
  214. for ( r = 0; r < replace.length; r++ )
  215. content = content.replace( replace[ r ][ 0 ], replace[ r ][ 1 ] );
  216. styleNodes[ id ].$.sheet.addRule( styleContent[ i ][ 0 ], content );
  217. }
  218. } else {
  219. content = styleContent;
  220. for ( r = 0; r < replace.length; r++ )
  221. content = content.replace( replace[ r ][ 0 ], replace[ r ][ 1 ] );
  222. if ( CKEDITOR.env.ie && CKEDITOR.env.version < 11 )
  223. styleNodes[ id ].$.styleSheet.cssText += content;
  224. else
  225. styleNodes[ id ].$.innerHTML += content;
  226. }
  227. }
  228. }
  229. CKEDITOR.on( 'instanceLoaded', function( evt ) {
  230. // The chameleon feature is not for IE quirks.
  231. if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
  232. return;
  233. var editor = evt.editor,
  234. showCallback = function( event ) {
  235. var panel = event.data[ 0 ] || event.data;
  236. var iframe = panel.element.getElementsByTag( 'iframe' ).getItem( 0 ).getFrameDocument();
  237. // Add stylesheet if missing.
  238. if ( !iframe.getById( 'cke_ui_color' ) ) {
  239. var node = getStylesheet( iframe );
  240. uiColorMenus.push( node );
  241. var color = editor.getUiColor();
  242. // Set uiColor for new panel.
  243. if ( color )
  244. updateStylesheets( [ node ], CKEDITOR.skin.chameleon( editor, 'panel' ), [ [ uiColorRegexp, color ] ] );
  245. }
  246. };
  247. editor.on( 'panelShow', showCallback );
  248. editor.on( 'menuShow', showCallback );
  249. // Apply UI color if specified in config.
  250. if ( editor.config.uiColor )
  251. editor.setUiColor( editor.config.uiColor );
  252. } );
  253. } )();
  254. /**
  255. * The list of file names matching the browser user agent string from
  256. * {@link CKEDITOR.env}. This is used to load the skin part file in addition
  257. * to the "main" skin file for a particular browser.
  258. *
  259. * **Note:** For each of the defined skin parts the corresponding
  260. * CSS file with the same name as the user agent must exist inside
  261. * the skin directory.
  262. *
  263. * @property ua
  264. * @todo type?
  265. */
  266. /**
  267. * The name of the skin that is currently used.
  268. *
  269. * @property {String} name
  270. * @todo
  271. */
  272. /**
  273. * The editor skin name. Note that it is not possible to have editors with
  274. * different skin settings in the same page. In such case just one of the
  275. * skins will be used for all editors.
  276. *
  277. * This is a shortcut to {@link CKEDITOR#skinName}.
  278. *
  279. * It is possible to install skins outside the default `skin` folder in the
  280. * editor installation. In that case, the absolute URL path to that folder
  281. * should be provided, separated by a comma (`'skin_name,skin_path'`).
  282. *
  283. * config.skin = 'moono';
  284. *
  285. * config.skin = 'myskin,/customstuff/myskin/';
  286. *
  287. * @cfg {String} skin
  288. * @member CKEDITOR.config
  289. */
  290. /**
  291. * A function that supports the chameleon (skin color switch) feature, providing
  292. * the skin color style updates to be applied in runtime.
  293. *
  294. * **Note:** The embedded `$color` variable is to be substituted with a specific UI color.
  295. *
  296. * @method chameleon
  297. * @param {String} editor The editor instance that the color changes apply to.
  298. * @param {String} part The name of the skin part where the color changes take place.
  299. */