Class.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Class.js
  3. *
  4. * Copyright 2003-2012, Moxiecode Systems AB, All rights reserved.
  5. */
  6. /**
  7. * This utilitiy class is used for easier inheritage.
  8. *
  9. * Features:
  10. * * Exposed super functions: this._super();
  11. * * Mixins
  12. * * Dummy functions
  13. * * Property functions: var value = object.value(); and object.value(newValue);
  14. * * Static functions
  15. * * Defaults settings
  16. */
  17. define("tinymce/util/Class", [
  18. "tinymce/util/Tools"
  19. ], function(Tools) {
  20. var each = Tools.each, extend = Tools.extend;
  21. var extendClass, initializing;
  22. function Class() {
  23. }
  24. // Provides classical inheritance, based on code made by John Resig
  25. Class.extend = extendClass = function(prop) {
  26. var self = this, _super = self.prototype, prototype, name, member;
  27. // The dummy class constructor
  28. function Class() {
  29. var i, mixins, mixin, self = this;
  30. // All construction is actually done in the init method
  31. if (!initializing) {
  32. // Run class constuctor
  33. if (self.init) {
  34. self.init.apply(self, arguments);
  35. }
  36. // Run mixin constructors
  37. mixins = self.Mixins;
  38. if (mixins) {
  39. i = mixins.length;
  40. while (i--) {
  41. mixin = mixins[i];
  42. if (mixin.init) {
  43. mixin.init.apply(self, arguments);
  44. }
  45. }
  46. }
  47. }
  48. }
  49. // Dummy function, needs to be extended in order to provide functionality
  50. function dummy() {
  51. return this;
  52. }
  53. // Creates a overloaded method for the class
  54. // this enables you to use this._super(); to call the super function
  55. function createMethod(name, fn) {
  56. return function(){
  57. var self = this, tmp = self._super, ret;
  58. self._super = _super[name];
  59. ret = fn.apply(self, arguments);
  60. self._super = tmp;
  61. return ret;
  62. };
  63. }
  64. // Instantiate a base class (but only create the instance,
  65. // don't run the init constructor)
  66. initializing = true;
  67. /*eslint new-cap:0 */
  68. prototype = new self();
  69. initializing = false;
  70. // Add mixins
  71. if (prop.Mixins) {
  72. each(prop.Mixins, function(mixin) {
  73. mixin = mixin;
  74. for (var name in mixin) {
  75. if (name !== "init") {
  76. prop[name] = mixin[name];
  77. }
  78. }
  79. });
  80. if (_super.Mixins) {
  81. prop.Mixins = _super.Mixins.concat(prop.Mixins);
  82. }
  83. }
  84. // Generate dummy methods
  85. if (prop.Methods) {
  86. each(prop.Methods.split(','), function(name) {
  87. prop[name] = dummy;
  88. });
  89. }
  90. // Generate property methods
  91. if (prop.Properties) {
  92. each(prop.Properties.split(','), function(name) {
  93. var fieldName = '_' + name;
  94. prop[name] = function(value) {
  95. var self = this, undef;
  96. // Set value
  97. if (value !== undef) {
  98. self[fieldName] = value;
  99. return self;
  100. }
  101. // Get value
  102. return self[fieldName];
  103. };
  104. });
  105. }
  106. // Static functions
  107. if (prop.Statics) {
  108. each(prop.Statics, function(func, name) {
  109. Class[name] = func;
  110. });
  111. }
  112. // Default settings
  113. if (prop.Defaults && _super.Defaults) {
  114. prop.Defaults = extend({}, _super.Defaults, prop.Defaults);
  115. }
  116. // Copy the properties over onto the new prototype
  117. for (name in prop) {
  118. member = prop[name];
  119. if (typeof member == "function" && _super[name]) {
  120. prototype[name] = createMethod(name, member);
  121. } else {
  122. prototype[name] = member;
  123. }
  124. }
  125. // Populate our constructed prototype object
  126. Class.prototype = prototype;
  127. // Enforce the constructor to be what we expect
  128. Class.constructor = Class;
  129. // And make this class extendible
  130. Class.extend = extendClass;
  131. return Class;
  132. };
  133. return Class;
  134. });