utils.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * A utilities library.
  8. *
  9. * @class utils
  10. * @singleton
  11. */
  12. CKEDITOR.define( [ 'utils-lodash', 'lib/lodash/lodash-ckeditor' ], function( lodashIncludes, lodash ) {
  13. var utils = {
  14. /**
  15. * A mixin function to be used to implement the static `extend()` method in classes. It allows for easy creation
  16. * of subclasses.
  17. *
  18. * @param {Object} [proto] Extensions to be added to the subclass prototype.
  19. * @param {Object} [statics] Additional static properties to be added to the subclass constructor.
  20. * @returns {Object} When executed as a static method of a class, it returns the new subclass constructor.
  21. */
  22. extendMixin: function( proto, statics ) {
  23. var that = this;
  24. var child = ( proto && proto.hasOwnProperty( 'constructor' ) ) ?
  25. proto.constructor :
  26. function() {
  27. that.apply( this, arguments );
  28. };
  29. // Copy the statics.
  30. utils.extend( child, this, statics );
  31. // Use the same prototype.
  32. child.prototype = Object.create( this.prototype );
  33. // Add the new prototype stuff.
  34. if ( proto ) {
  35. proto = utils.clone( proto );
  36. delete proto.constructor;
  37. utils.extend( child.prototype, proto );
  38. }
  39. return child;
  40. },
  41. /**
  42. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  43. *
  44. * The following are the present features:
  45. *
  46. * * spy.called: property set to `true` if the function has been called at least once.
  47. *
  48. * @returns {Function} The spy function.
  49. */
  50. spy: function() {
  51. var spy = function() {
  52. spy.called = true;
  53. };
  54. return spy;
  55. },
  56. /**
  57. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  58. * to this method.
  59. *
  60. * @returns {Number} A number representing the id.
  61. */
  62. uid: ( function() {
  63. var next = 1;
  64. return function() {
  65. return next++;
  66. };
  67. } )()
  68. };
  69. // Extend "utils" with Lo-Dash methods.
  70. for ( var i = 0; i < lodashIncludes.length; i++ ) {
  71. utils[ lodashIncludes[ i ] ] = lodash[ lodashIncludes[ i ] ];
  72. }
  73. return utils;
  74. } );
  75. /**
  76. * Creates a subclass constructor based on this class.
  77. *
  78. * @member utils
  79. * @method extend
  80. * @abstract
  81. * @static
  82. * @inheritable
  83. *
  84. * @param {Object} [proto] Extensions to be added to the subclass prototype.
  85. * @param {Object} [statics] Extension to be added as static members of the subclass constructor.
  86. * @returns {Object} The subclass constructor.
  87. */