sidebar.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * @fileoverview
  3. * Trex.Sidebar, Trex.EntryBox, Trex.Entry, Trex.Actor를 포함하고 있다.
  4. */
  5. /**
  6. * 에디터와 외부 component사이의 연동을 하는 class
  7. *
  8. * @class
  9. * @param {object} editor
  10. * @param {object} config
  11. */
  12. Trex.Sidebar = Trex.Class.create({
  13. /** @ignore */
  14. $const: {
  15. __REG_ENTRY_ATTR_PAIR_Q: new RegExp("([\\w]+)=\"([^\"]+)\"", "g"),
  16. __REG_ENTRY_ATTR_PAIR_NQ: new RegExp("([\\w]+)=([\\w]+)", "g")
  17. },
  18. /** @ignore */
  19. $mixins: [
  20. Trex.I.JobObservable
  21. ],
  22. entryboxRegistry: _NULL,
  23. initialize: function(editor) {
  24. var _canvas = editor.getCanvas();
  25. this.entryboxRegistry = {};
  26. this.getFields = function() {
  27. var fields = [];
  28. for(var i in this.entryboxRegistry){
  29. var entrybox = this.entryboxRegistry[i];
  30. fields = fields.concat(entrybox.getFields());
  31. }
  32. return fields;
  33. };
  34. this.syncSidebar = function() {
  35. var _content = _canvas.getContent();
  36. for(var i in this.entryboxRegistry){
  37. this.entryboxRegistry[i].syncBox(_content);
  38. }
  39. };
  40. this.emptyEntries = function() {
  41. for(var i in this.entryboxRegistry){
  42. this.entryboxRegistry[i].empty();
  43. }
  44. };
  45. _canvas.observeJob(Trex.Ev.__CANVAS_PANEL_DELETE_SOMETHING, function() {
  46. this.syncSidebar();
  47. }.bind(this));
  48. }
  49. });
  50. /**
  51. * Trex.entryBox
  52. * @class
  53. */
  54. Trex.EntryBox = Trex.Class.draft({
  55. /** @ignore */
  56. $mixins: [
  57. Trex.I.JobObservable
  58. ],
  59. autoSeq: 0,
  60. datalist: [],
  61. initialize: function() {
  62. throw new Error("[Exception]Trex.EntryBox : not implements function(initialize)");
  63. },
  64. newSeq: function() {
  65. return (++this.autoSeq);
  66. },
  67. syncSeq: function(existedSeq) {
  68. this.autoSeq = (existedSeq > this.autoSeq)? existedSeq: this.autoSeq;
  69. return existedSeq;
  70. },
  71. empty: function() {
  72. this.fireJobs(Trex.Ev.__ENTRYBOX_ALL_ENTRY_REMOVED);
  73. this.datalist = [];
  74. },
  75. append: function(entry) {
  76. this.datalist.push(entry);
  77. this.fireJobs(Trex.Ev.__ENTRYBOX_ENTRY_ADDED, entry);
  78. },
  79. modify: function(entry) {
  80. this.fireJobs(Trex.Ev.__ENTRYBOX_ENTRY_MODIFIED, entry);
  81. },
  82. remove: function(entry) {
  83. entry.deletedMark = _TRUE;
  84. this.fireJobs(Trex.Ev.__ENTRYBOX_ENTRY_REMOVED, entry);
  85. },
  86. syncBox: function(content) {
  87. this.datalist.each(function(entry) {
  88. entry.execSync(content);
  89. });
  90. },
  91. getFields: function() {
  92. var _fields = [];
  93. this.datalist.each(function(entry) {
  94. _fields.push(entry.getField());
  95. });
  96. return _fields.findAll(function(field) {
  97. return (field != _NULL);
  98. });
  99. },
  100. getEntries: function(name) {
  101. if(!name) { //all file
  102. return this.datalist;
  103. }
  104. var _entries = [];
  105. this.datalist.each(
  106. function(entry){
  107. if(entry.type == name){
  108. _entries.push(entry);
  109. }
  110. }
  111. );
  112. return _entries;
  113. }
  114. });
  115. /**
  116. * Trex.Entry
  117. * @class
  118. */
  119. Trex.Entry = Trex.Class.draft({
  120. /** @ignore */
  121. $mixins: [
  122. Trex.I.JobObservable
  123. ],
  124. existStage: _FALSE,
  125. deletedMark: _FALSE,
  126. initialize: function(/*actor, canvas, entryBox, config*/) {
  127. throw new Error("[Exception]Trex.Entry : not implements function(initialize)");
  128. },
  129. setExistStage: function(existStage) {
  130. this.existStage = existStage;
  131. },
  132. execRegister: function() {
  133. this.register();
  134. this.entryBox.append(this);
  135. this.setExistStage(_TRUE);
  136. },
  137. execReload: function() {
  138. if(this.reload) {
  139. this.reload();
  140. }
  141. this.entryBox.append(this);
  142. this.exchangeHandlerAtReload();
  143. },
  144. execRemove: function() {
  145. this.remove();
  146. this.entryBox.remove(this);
  147. },
  148. execReplace: function(oldReg) {
  149. this.replace(oldReg);
  150. this.entryBox.modify(this);
  151. this.setExistStage(_TRUE);
  152. },
  153. execAppend: function() {
  154. this.register();
  155. this.setExistStage(_TRUE);
  156. },
  157. execSync: function(content) {
  158. this.setExistStage(this.checkExisted(content));
  159. },
  160. checkExisted: function(content) {
  161. if(this.canvas.isWYSIWYG()) {
  162. return (content.search(this.regHtml) > -1);
  163. } else {
  164. return (content.search(this.regText) > -1);
  165. }
  166. },
  167. getChangedContent: function(content, rex, str, param) {
  168. var _existStage = _FALSE;
  169. if(content.search(rex) > -1) {
  170. _existStage = _TRUE;
  171. if (this.actor.canResized) {
  172. content = this.getChangedContentWithAttr(content, rex, str, param);
  173. } else {
  174. content = content.replace(rex, str);
  175. }
  176. }
  177. this.setExistStage(_existStage);
  178. return content;
  179. },
  180. getChangedContentFromHtml: function(content) {
  181. return this.getChangedContent(content, this.regHtml, this.dispText, ["id", "class"]);
  182. },
  183. getChangedContentToHtml: function(content) {
  184. return this.getChangedContent(content, this.regText, this.dispHtml);
  185. },
  186. getChangedContentAtSave: function(content) { //Only HTML
  187. return this.getChangedContent(content, this.regHtml, this.saveHtml, ["id", "class"]);
  188. },
  189. getChangedContentAtLoad: function(content) { //Only HTML
  190. return this.getChangedContent(content, this.regLoad, this.dispHtml);
  191. },
  192. getChangedContentWithAttr: function(content, reg, disp, excepts) {
  193. excepts = excepts || [];
  194. var _attrMap = Trex.Util.getAllAttributes(disp);
  195. var _getChangedTag = function(source) {
  196. var _tag = Trex.Util.getMatchValue(/<([a-z]*)/i, disp, 1);
  197. var _attr = ["<"+_tag.toLowerCase()];
  198. var _overMap = Trex.Util.getAllAttributes(source);
  199. for(var _name in _attrMap) {
  200. if (["width", "height"].contains(_name)) {
  201. if(!_overMap[_name]) {
  202. _attr.push(_name + "=\"" + _attrMap[_name] + "\"");
  203. }
  204. } else {
  205. _attr.push(_name + "=\"" + _attrMap[_name] + "\"");
  206. }
  207. }
  208. for(var _name in _overMap) {
  209. if(!excepts.contains(_name)) {
  210. if (["width", "height"].contains(_name)) {
  211. _attr.push(_name + "=\"" + _overMap[_name] + "\"");
  212. } else if(!_attrMap[_name]) {
  213. _attr.push(_name + "=\"" + _overMap[_name] + "\"");
  214. }
  215. }
  216. }
  217. _attr.push("/>");
  218. return _attr.join(" ");
  219. };
  220. var _orgContent = content;
  221. var _matchs;
  222. reg.lastIndex = 0;
  223. while ((_matchs = reg.exec(_orgContent)) != _NULL) {
  224. var _textOrg = _matchs[0];
  225. var _dispTrans = _getChangedTag(_textOrg);
  226. var _regOrg = _textOrg.getRegExp();
  227. content = content.replace(new RegExp(_regOrg, "gmi"), _dispTrans);
  228. }
  229. return content;
  230. },
  231. getField: function() {
  232. if(!this.field) {
  233. return _NULL;
  234. }
  235. return {
  236. name: this.field.name,
  237. value: [this.field.value, this.existStage].join('|')
  238. };
  239. },
  240. exchangeHandlerAtReload: function(){}
  241. });
  242. /**
  243. * Trex.Actor
  244. * @class
  245. */
  246. Trex.Actor = Trex.Class.draft({
  247. /** @ignore */
  248. $mixins: [
  249. Trex.I.JobObservable
  250. ],
  251. isDisabled: _FALSE,
  252. initialize: function(/*config, canvas*/) {
  253. throw new Error("[Exception]Trex.Actor : not implements function(initialize)");
  254. },
  255. execAttach: function(data, type) {
  256. var _entry = this.createEntry(this.getDataForEntry(data), type);
  257. _entry.execRegister();
  258. this.canvas.fireJobs('canvas.' + (type || this.constructor.__Identity) + '.added', _entry);
  259. },
  260. getDatalist: function(){
  261. return this.entryBox.getEntries(this.name);
  262. },
  263. execReattach: function(data, type) {
  264. var datalist = this.getDatalist();
  265. var parsedData = this.getDataForEntry(data);
  266. if(datalist.length < 1) {
  267. var _entry = this.createEntry(parsedData, type);
  268. _entry.execRegister();
  269. } else {
  270. var _entry = datalist[0];
  271. var _oldReg = {
  272. regHtml: _entry.regHtml,
  273. regText: _entry.regText
  274. };
  275. _entry.setProperties(parsedData);
  276. _entry.execReplace(_oldReg);
  277. }
  278. },
  279. execReload: function(data, content, type) {
  280. var _dataForEntry = this.getDataForEntry(data, content);
  281. if (_dataForEntry) { // FTDUEDTR-1361
  282. var _entry = this.createEntry(_dataForEntry, type);
  283. _entry.execReload();
  284. }
  285. },
  286. existEntry: function() {
  287. var list = this.getDatalist().findAll(function(entry) {
  288. return entry.deletedMark != _TRUE;
  289. });
  290. return list.length !== 0;
  291. },
  292. getFirstEntryData: function() {
  293. var list = this.getDatalist().findAll(function(entry) {
  294. return entry.deletedMark != _TRUE;
  295. });
  296. return ((list.length == 0)? _NULL: list[0].data);
  297. }
  298. });