utils.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. utils.extend( child.prototype, proto );
  36. }
  37. return child;
  38. },
  39. /**
  40. * Creates a spy function (ala Sinon.js) that can be used to inspect call to it.
  41. *
  42. * The following are the present features:
  43. *
  44. * * spy.called: property set to `true` if the function has been called at least once.
  45. *
  46. * @returns {Function} The spy function.
  47. */
  48. spy: function() {
  49. var spy = function() {
  50. spy.called = true;
  51. };
  52. return spy;
  53. },
  54. /**
  55. * Returns a unique id. This id is a number (starting from 1) which will never get repeated on successive calls
  56. * to this method.
  57. *
  58. * @returns {Number} A number representing the id.
  59. */
  60. uid: ( function() {
  61. var next = 1;
  62. return function() {
  63. return next++;
  64. };
  65. } )()
  66. };
  67. // Extend "utils" with Lo-Dash methods.
  68. for ( var i = 0; i < lodashIncludes.length; i++ ) {
  69. utils[ lodashIncludes[ i ] ] = lodash[ lodashIncludes[ i ] ];
  70. }
  71. return utils;
  72. } );
  73. /**
  74. * Creates a subclass constructor based on this class.
  75. *
  76. * @member utils
  77. * @method extend
  78. * @abstract
  79. * @static
  80. * @inheritable
  81. *
  82. * @param {Object} [proto] Extensions to be added to the subclass prototype.
  83. * @param {Object} [statics] Extension to be added as static members of the subclass constructor.
  84. * @returns {Object} The subclass constructor.
  85. */