plugins.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.plugins} object, which is used to
  7. * manage plugins registration and loading.
  8. */
  9. /**
  10. * Manages plugins registration and loading.
  11. *
  12. * @class
  13. * @extends CKEDITOR.resourceManager
  14. * @singleton
  15. */
  16. CKEDITOR.plugins = new CKEDITOR.resourceManager( 'plugins/', 'plugin' );
  17. // PACKAGER_RENAME( CKEDITOR.plugins )
  18. CKEDITOR.plugins.load = CKEDITOR.tools.override( CKEDITOR.plugins.load, function( originalLoad ) {
  19. var initialized = {};
  20. return function( name, callback, scope ) {
  21. var allPlugins = {};
  22. var loadPlugins = function( names ) {
  23. originalLoad.call( this, names, function( plugins ) {
  24. CKEDITOR.tools.extend( allPlugins, plugins );
  25. var requiredPlugins = [];
  26. for ( var pluginName in plugins ) {
  27. var plugin = plugins[ pluginName ],
  28. requires = plugin && plugin.requires;
  29. if ( !initialized[ pluginName ] ) {
  30. // Register all icons eventually defined by this plugin.
  31. if ( plugin.icons ) {
  32. var icons = plugin.icons.split( ',' );
  33. for ( var ic = icons.length; ic--; ) {
  34. CKEDITOR.skin.addIcon( icons[ ic ],
  35. plugin.path +
  36. 'icons/' +
  37. ( CKEDITOR.env.hidpi && plugin.hidpi ? 'hidpi/' : '' ) +
  38. icons[ ic ] +
  39. '.png' );
  40. }
  41. }
  42. initialized[ pluginName ] = 1;
  43. }
  44. if ( requires ) {
  45. // Trasnform it into an array, if it's not one.
  46. if ( requires.split )
  47. requires = requires.split( ',' );
  48. for ( var i = 0; i < requires.length; i++ ) {
  49. if ( !allPlugins[ requires[ i ] ] )
  50. requiredPlugins.push( requires[ i ] );
  51. }
  52. }
  53. }
  54. if ( requiredPlugins.length )
  55. loadPlugins.call( this, requiredPlugins );
  56. else {
  57. // Call the "onLoad" function for all plugins.
  58. for ( pluginName in allPlugins ) {
  59. plugin = allPlugins[ pluginName ];
  60. if ( plugin.onLoad && !plugin.onLoad._called ) {
  61. // Make it possible to return false from plugin::onLoad to disable it.
  62. if ( plugin.onLoad() === false )
  63. delete allPlugins[ pluginName ];
  64. plugin.onLoad._called = 1;
  65. }
  66. }
  67. // Call the callback.
  68. if ( callback )
  69. callback.call( scope || window, allPlugins );
  70. }
  71. }, this );
  72. };
  73. loadPlugins.call( this, name );
  74. };
  75. } );
  76. /**
  77. * Loads a specific language file, or auto detect it. A callback is
  78. * then called when the file gets loaded.
  79. *
  80. * CKEDITOR.plugins.setLang( 'myPlugin', 'en', {
  81. * title: 'My plugin',
  82. * selectOption: 'Please select an option'
  83. * } );
  84. *
  85. * @param {String} pluginName The name of the plugin to which the provided translation
  86. * should be attached.
  87. * @param {String} languageCode The code of the language translation provided.
  88. * @param {Object} languageEntries An object that contains pairs of label and
  89. * the respective translation.
  90. */
  91. CKEDITOR.plugins.setLang = function( pluginName, languageCode, languageEntries ) {
  92. var plugin = this.get( pluginName ),
  93. pluginLangEntries = plugin.langEntries || ( plugin.langEntries = {} ),
  94. pluginLang = plugin.lang || ( plugin.lang = [] );
  95. if ( pluginLang.split )
  96. pluginLang = pluginLang.split( ',' );
  97. if ( CKEDITOR.tools.indexOf( pluginLang, languageCode ) == -1 )
  98. pluginLang.push( languageCode );
  99. pluginLangEntries[ languageCode ] = languageEntries;
  100. };