module.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define('simple-module', ["jquery"], function ($) {
  5. return (root.returnExportsGlobal = factory($));
  6. });
  7. } else if (typeof exports === 'object') {
  8. // Node. Does not work with strict CommonJS, but
  9. // only CommonJS-like enviroments 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, val, _ref;
  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, val, _ref;
  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 cls, instance, instances, name, _base, _i, _len;
  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, result, _ref;
  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. }));