trex.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /**
  2. * @fileoverview
  3. * Trex 정의
  4. */
  5. /** @namespace */
  6. var Trex = {
  7. __WORD_JOINER: "\ufeff",
  8. __WORD_JOINER_REGEXP: /\ufeff/g,
  9. __KEY: {
  10. ENTER: '13',
  11. DELETE: '46',
  12. SPACE: '32',
  13. BACKSPACE: '8',
  14. TAB: '9',
  15. PASTE: '86', //+ ctrl
  16. CUT: '88' //+ ctrl
  17. },
  18. I: {},
  19. X: {},
  20. define: function(destination, properties) {
  21. return Object.extend(destination, properties);
  22. },
  23. available: function(config, name) {
  24. if(!$tx("tx_" + name)){
  25. //console.log("Warning: JsObject is existed but element 'tx_" + name + "' is not found.");
  26. return _FALSE;
  27. }
  28. if(!config){
  29. //console.log("Warning: no config for" + name);
  30. return _FALSE;
  31. }
  32. if(config.use == _FALSE) {
  33. //console.log("Warning: config.use == _FALSE");
  34. return _FALSE;
  35. }
  36. return _TRUE;
  37. }
  38. };
  39. //oop
  40. (function(Trex){
  41. function $$reference($instance) {
  42. var _$ref = $instance;
  43. while(_$ref.$reference) {
  44. _$ref = _$ref.$reference;
  45. }
  46. return _$ref;
  47. }
  48. function $$super($instance) {
  49. var _$superclass = $instance.constructor.superclass;
  50. if(_$superclass) {
  51. var _$initbak = _$superclass.prototype.initialize;
  52. _$superclass.prototype.initialize = function() {
  53. this.$reference = $instance;
  54. }; //fake initialize
  55. var _$superobj = new _$superclass();
  56. _$superclass.prototype.initialize = _$initbak;
  57. var _wrapFunc = function(name) {
  58. if(!_$superobj[name]) return _NULL;
  59. return function() {
  60. var _arguments = arguments;
  61. var _$reference = $$reference($instance);
  62. var _$superbak = _$reference.$super;
  63. _$reference.$super = _$superobj.$super;
  64. var _returns = _$superobj[name].apply(_$reference, _arguments);
  65. _$reference.$super = _$superbak;
  66. return _returns;
  67. };
  68. };
  69. var _$wrapobj = {};
  70. for(var _name in _$superobj) {
  71. if(_name.charAt(0) != '$') {
  72. if (typeof(_$superobj[_name]) == 'function') {
  73. _$wrapobj[_name] = _wrapFunc(_name);
  74. }
  75. }
  76. }
  77. $instance.$super = _$wrapobj;
  78. }
  79. }
  80. /**
  81. * @namespace
  82. * @name Trex.Class
  83. */
  84. Trex.Class = /** @lends Trex.Class */ {
  85. /**
  86. * creates class
  87. * @param {Object} properties
  88. */
  89. create: function(properties) {
  90. var _class = function() {
  91. var _proto = this.constructor.prototype; //NOTE: Cuz properties must not share
  92. for(var _name in _proto) {
  93. if(_proto[_name] && typeof(_proto[_name]) === 'object') {
  94. if(_proto[_name].constructor == Array) { //Array
  95. this[_name] = [].concat(_proto[_name]);
  96. } else {
  97. this[_name] = Object.extend({}, _proto[_name]);
  98. }
  99. }
  100. }
  101. $$super(this);
  102. var _arguments = arguments;
  103. this.initialize.apply(this, _arguments);
  104. };
  105. return Trex.Class.draft(properties, _class);
  106. },
  107. draft: function(properties, aClass) {
  108. var _class = aClass ?
  109. aClass :
  110. function() {
  111. $$super(this);
  112. };
  113. if(properties.$const) {
  114. Object.extend(_class, properties.$const);
  115. }
  116. if(properties.$extend) {
  117. Object.extend(_class.prototype, properties.$extend.prototype);
  118. _class.superclass = properties.$extend;
  119. }
  120. if(properties.$mixins) {
  121. var sources = $A(properties.$mixins);
  122. sources.each(function(source) {
  123. Object.extend(_class.prototype, source);
  124. });
  125. }
  126. for(var _name in properties) {
  127. if(_name.charAt(0) != '$') {
  128. _class.prototype[_name] = properties[_name];
  129. }
  130. }
  131. return _class;
  132. },
  133. overwrite: function(source, properties) {
  134. if(source.prototype) {
  135. Object.extend(source.prototype, properties);
  136. }
  137. return source;
  138. }
  139. };
  140. /**
  141. * @namespace
  142. * @name Trex.Faculty, Trex.Mixin
  143. */
  144. Trex.Mixin = Trex.Faculty = /** @lends Trex.Mixin */ {
  145. /**
  146. * Creates
  147. * @param {Object} properties
  148. */
  149. create: function(properties) {
  150. var _class = {};
  151. for(var _name in properties) {
  152. if(properties[_name] && typeof(properties[_name]) === 'object') {
  153. if(properties[_name].constructor == Array) { //Array
  154. _class[_name] = [].concat(properties[_name]);
  155. } else {
  156. _class[_name] = Object.extend({}, properties[_name]);
  157. }
  158. } else {
  159. _class[_name] = properties[_name];
  160. }
  161. }
  162. return _class;
  163. },
  164. toClass: function(properties, initializeFunc) {
  165. return Trex.Class.create(
  166. Object.extend({
  167. initialize: initializeFunc? initializeFunc: function() {}
  168. }, properties)
  169. );
  170. }
  171. };
  172. })(Trex);
  173. //module
  174. (function(Trex){
  175. Object.extend(Trex, /** @lends Trex */ {
  176. installs: [],
  177. registers: [],
  178. modules: [],
  179. modulesX: [],
  180. /**
  181. * Installs component
  182. * @param {Object} description
  183. * @param {Object} fn
  184. */
  185. install: function(description, fn){
  186. fn.desc = '[install] ' + description;
  187. Trex.installs.push(fn);
  188. },
  189. register: function(description, fn){
  190. fn.desc = '[register] ' + description;
  191. Trex.registers.push(fn);
  192. },
  193. module: function(description, fn){
  194. //console.log(' >>> ' + description);
  195. fn.desc = '[module] ' + description;
  196. Trex.modules.push(fn);
  197. },
  198. moduleX: function(description, fn){
  199. fn.desc = '[moduleX] ' + description;
  200. Trex.modulesX.push(fn);
  201. },
  202. invoke: function(fns, editor, toolbar, sidebar, canvas, config){
  203. for(var i=0,len=fns.length; i<len; i++){
  204. var fn = fns[i];
  205. fn(editor, toolbar, sidebar, canvas, config);
  206. }
  207. },
  208. invokeInstallation: function(editor, toolbar, sidebar, canvas, config){
  209. Trex.invoke(Trex.installs, editor, toolbar, sidebar, canvas, config);
  210. },
  211. invokeRegisters: function(editor, toolbar, sidebar, canvas, config){
  212. Trex.invoke(Trex.registers, editor, toolbar, sidebar, canvas, config);
  213. },
  214. invokeModules: function(editor, toolbar, sidebar, canvas, config){
  215. Trex.invoke(Trex.modules, editor, toolbar, sidebar, canvas, config);
  216. },
  217. group: function(){},
  218. groupEnd: function(){}
  219. });
  220. })(Trex);
  221. _WIN.Trex = Trex;