module.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module unless amdModuleId is set
  4. define('simple-module', ["jquery"], function (a0) {
  5. return (root['Module'] = factory(a0));
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like environments that support module.exports,
  10. // like Node.
  11. module.exports = factory(require("jquery"));
  12. } else {
  13. root['SimpleModule'] = factory(jQuery);
  14. }
  15. }(this, function ($) {
  16. var Module,
  17. slice = [].slice;
  18. Module = (function() {
  19. Module.extend = function(obj) {
  20. var key, ref, val;
  21. if (!((obj != null) && typeof obj === 'object')) {
  22. return;
  23. }
  24. for (key in obj) {
  25. val = obj[key];
  26. if (key !== 'included' && key !== 'extended') {
  27. this[key] = val;
  28. }
  29. }
  30. return (ref = obj.extended) != null ? ref.call(this) : void 0;
  31. };
  32. Module.include = function(obj) {
  33. var key, ref, val;
  34. if (!((obj != null) && typeof obj === 'object')) {
  35. return;
  36. }
  37. for (key in obj) {
  38. val = obj[key];
  39. if (key !== 'included' && key !== 'extended') {
  40. this.prototype[key] = val;
  41. }
  42. }
  43. return (ref = obj.included) != null ? ref.call(this) : void 0;
  44. };
  45. Module.connect = function(cls) {
  46. if (typeof cls !== 'function') {
  47. return;
  48. }
  49. if (!cls.pluginName) {
  50. throw new Error('Module.connect: cannot connect plugin without pluginName');
  51. return;
  52. }
  53. cls.prototype._connected = true;
  54. if (!this._connectedClasses) {
  55. this._connectedClasses = [];
  56. }
  57. this._connectedClasses.push(cls);
  58. if (cls.pluginName) {
  59. return this[cls.pluginName] = cls;
  60. }
  61. };
  62. Module.prototype.opts = {};
  63. function Module(opts) {
  64. var base, cls, i, instance, instances, len, name;
  65. this.opts = $.extend({}, this.opts, opts);
  66. (base = this.constructor)._connectedClasses || (base._connectedClasses = []);
  67. instances = (function() {
  68. var i, len, ref, results;
  69. ref = this.constructor._connectedClasses;
  70. results = [];
  71. for (i = 0, len = ref.length; i < len; i++) {
  72. cls = ref[i];
  73. name = cls.pluginName.charAt(0).toLowerCase() + cls.pluginName.slice(1);
  74. if (cls.prototype._connected) {
  75. cls.prototype._module = this;
  76. }
  77. results.push(this[name] = new cls());
  78. }
  79. return results;
  80. }).call(this);
  81. if (this._connected) {
  82. this.opts = $.extend({}, this.opts, this._module.opts);
  83. } else {
  84. this._init();
  85. for (i = 0, len = instances.length; i < len; i++) {
  86. instance = instances[i];
  87. if (typeof instance._init === "function") {
  88. instance._init();
  89. }
  90. }
  91. }
  92. this.trigger('initialized');
  93. }
  94. Module.prototype._init = function() {};
  95. Module.prototype.on = function() {
  96. var args, ref;
  97. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  98. (ref = $(this)).on.apply(ref, args);
  99. return this;
  100. };
  101. Module.prototype.one = function() {
  102. var args, ref;
  103. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  104. (ref = $(this)).one.apply(ref, args);
  105. return this;
  106. };
  107. Module.prototype.off = function() {
  108. var args, ref;
  109. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  110. (ref = $(this)).off.apply(ref, args);
  111. return this;
  112. };
  113. Module.prototype.trigger = function() {
  114. var args, ref;
  115. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  116. (ref = $(this)).trigger.apply(ref, args);
  117. return this;
  118. };
  119. Module.prototype.triggerHandler = function() {
  120. var args, ref;
  121. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  122. return (ref = $(this)).triggerHandler.apply(ref, args);
  123. };
  124. Module.prototype._t = function() {
  125. var args, ref;
  126. args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  127. return (ref = this.constructor)._t.apply(ref, args);
  128. };
  129. Module._t = function() {
  130. var args, key, ref, result;
  131. key = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  132. result = ((ref = this.i18n[this.locale]) != null ? ref[key] : void 0) || '';
  133. if (!(args.length > 0)) {
  134. return result;
  135. }
  136. result = result.replace(/([^%]|^)%(?:(\d+)\$)?s/g, function(p0, p, position) {
  137. if (position) {
  138. return p + args[parseInt(position) - 1];
  139. } else {
  140. return p + args.shift();
  141. }
  142. });
  143. return result.replace(/%%s/g, '%s');
  144. };
  145. Module.i18n = {
  146. 'zh-CN': {}
  147. };
  148. Module.locale = 'zh-CN';
  149. return Module;
  150. })();
  151. return Module;
  152. }));