attachment.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @fileoverview
  3. * attachments.js
  4. *
  5. */
  6. TrexMessage.addMsg({
  7. '@attacher.only.wysiwyg.alert': "에디터 상태에서만 본문에 삽입할 수 있습니다.\n에디터모드에서 첨부박스의 썸네일을 클릭해서 삽입할 수 있습니다."
  8. });
  9. /**
  10. * Trex.Attachment
  11. * 첨부된 data를 wrapping하는 class
  12. *
  13. * @abstract
  14. * @class
  15. * @extends Trex.Entry
  16. *
  17. * @param {Object} actor
  18. * @param {Object} data
  19. */
  20. Trex.Attachment = Trex.Class.draft(/** @lends Trex.Attachment.prototype */{
  21. /** @ignore */
  22. $extend: Trex.Entry,
  23. isChecked: _FALSE,
  24. focused: _FALSE,
  25. attrs: {
  26. align: "left"
  27. },
  28. initialize: function(actor, data) {
  29. this.actor = actor;
  30. this.canvas = actor.canvas;
  31. this.entryBox = actor.entryBox;
  32. this.type = this.constructor.__Identity;
  33. this.setProperties(data);
  34. if (this.oninitialized) {
  35. this.oninitialized(actor, data);
  36. }
  37. },
  38. /**
  39. * focused 값을 설정한다.
  40. * @function
  41. */
  42. setFocused: function(focused) {
  43. if (this.focused !== focused) {
  44. this.focused = focused;
  45. }
  46. },
  47. /**
  48. * existStage 값을 설정한다.
  49. * @function
  50. */
  51. setExistStage: function(existStage) { //just attachments~
  52. /**
  53. * attachment가 content에 존재하는지 확인할 때 사용되는 속성
  54. */
  55. this.existStage = existStage;
  56. if (this.entryBox.changeState) {
  57. this.entryBox.changeState(this);
  58. }
  59. },
  60. /**
  61. * content에서 attachment를 지운다.
  62. * @function
  63. */
  64. remove: function() {
  65. var _content = this.canvas.getContent();
  66. if (this.canvas.isWYSIWYG()) {
  67. if (_content.search(this.regHtml) > -1) {
  68. _content = _content.replace(this.regHtml, "");
  69. this.canvas.setContent(_content);
  70. }
  71. } else {
  72. if (_content.search(this.regText) > -1) {
  73. _content = _content.replace(this.regText, "");
  74. this.canvas.setContent(_content);
  75. }
  76. }
  77. },
  78. /**
  79. * attachment HTML을 에디터 본문에 붙여넣는다.
  80. * @function
  81. */
  82. register: function() {
  83. if (Editor.getSidebar().addOnlyBox) {
  84. return;
  85. }
  86. var _actor = this.actor;
  87. if (_actor.boxonly) {
  88. return;
  89. }
  90. if (this.canvas.isWYSIWYG()) {
  91. var _pastescope = this.pastescope;
  92. var _dispHtml = this.dispHtml;
  93. var objectElemTagName = "img";
  94. var findRegex = this.matchRegexStartTag;// /<(\w+)/
  95. var matched = _dispHtml.match(findRegex);
  96. //for other elements(Exam: button of file attachment).
  97. if (matched && matched[1]) {
  98. objectElemTagName = matched[1];
  99. }
  100. if (this.objectStyle) {
  101. var objectElemeReg = new RegExp("<" + objectElemTagName + " ", "i");
  102. _dispHtml = _dispHtml.replace(objectElemeReg, "<" + objectElemTagName + " style=\"" + Trex.Util.toStyleString(this.objectStyle) + "\" ");
  103. }
  104. if (this.objectAttr) {
  105. _dispHtml = _dispHtml.replace(objectElemeReg, "<" + objectElemTagName + " " + Trex.Util.toAttrString(this.objectAttr) + " ");
  106. }
  107. var _style = this.paragraphStyle || {};
  108. if ($tx.webkit) {
  109. this.canvas.getPanel('html').el.focus(); // FTDUEDTR-1281
  110. }
  111. this.canvas.execute(function(processor) {
  112. processor.moveCaretWith(_pastescope);
  113. processor.pasteContent(_dispHtml, _TRUE, {
  114. 'style': _style
  115. });
  116. });
  117. } else {
  118. if (this.actor.wysiwygonly) {
  119. alert(TXMSG("@attacher.only.wysiwyg.alert"));
  120. } else {
  121. this.canvas.getProcessor().insertTag('', this.dispText);
  122. }
  123. }
  124. },
  125. /**
  126. * 인자로 받은 old regex로 attachment를 식별해서 HTML을 교체한다.
  127. * @function
  128. */
  129. replace: function(oldReg) {
  130. var _canvas = this.canvas;
  131. var _content = _canvas.getContent();
  132. var _actor = this.actor;
  133. if (!_actor.boxonly) {
  134. if (_canvas.isWYSIWYG()) {
  135. if (_content.search(oldReg.regHtml) > -1) {
  136. _content = _content.replace(oldReg.regHtml, this.dispHtml);
  137. _canvas.setContent(_content);
  138. } else {
  139. _canvas.pasteContent(this.dispHtml, _TRUE);
  140. }
  141. } else {
  142. if (_content.search(oldReg.regText) > -1) {
  143. _content = _content.replace(oldReg.regText, "");
  144. _canvas.setContent(_content);
  145. }
  146. alert(TXMSG("@attacher.only.wysiwyg.alert"));
  147. }
  148. }
  149. },
  150. /**
  151. * attachment 관련하여 필요한 속성을 this 객체에 할당한다.
  152. * @function
  153. */
  154. setProperties: function(data) {
  155. var _data = data;
  156. this.data = _data;
  157. this.key = this.actor.getKey(_data) || 'K' + Trex.Util.generateKey();
  158. this.field = this.getFieldAttr(_data);
  159. this.boxAttr = this.getBoxAttr(_data);
  160. this.objectAttr = this.getObjectAttr.bind(this)(_data);
  161. this.objectStyle = this.getObjectStyle.bind(this)(_data);
  162. this.paragraphStyle = this.getParaStyle.bind(this)(_data);
  163. this.saveHtml = this.getSaveHtml.bind(this)(_data);
  164. this.dispHtml = this.getDispHtml.bind(this)(_data);
  165. this.dispText = this.getDispText.bind(this)(_data);
  166. this.regLoad = this.getRegLoad.bind(this)(_data);
  167. this.regHtml = this.getRegHtml.bind(this)(_data);
  168. this.regText = this.getRegText.bind(this)(_data);
  169. },
  170. refreshProperties: function() {
  171. this.setProperties(this.data);
  172. },
  173. /**
  174. * object의 attribute 값을 가져온다.
  175. * @function
  176. */
  177. getObjectAttr: function() {
  178. return this.actor.config.objattr;
  179. },
  180. getObjectStyle: function() {
  181. var objstyle = {};
  182. if (this.actor.config.objstyle) {
  183. objstyle = Object.extend(objstyle, this.actor.config.objstyle);
  184. }
  185. return objstyle;
  186. },
  187. getParaStyle: function(data) {
  188. var parastyle = Object.extend({}, this.actor.config.parastyle || this.actor.config.defaultstyle);
  189. return parastyle;
  190. },
  191. updateEntryElement: function(targetElement) {
  192. if (!targetElement) {
  193. return;
  194. }
  195. var tempNode = _DOC.createElement('div');
  196. tempNode.innerHTML = this.dispHtml;
  197. targetElement.innerHTML = $tom.first(tempNode).innerHTML;
  198. }
  199. });