saver.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. Trex.install("editor.getSaver & editor.getDataAsJSON & editor.setDataByJSON",
  2. function(editor, toolbar, sidebar, canvas, config){
  3. var _saver = new Trex.Save(editor, toolbar, sidebar, canvas, config);
  4. editor.getSaver = function() {
  5. return _saver;
  6. };
  7. editor.getDataAsJSON = function() {
  8. var _content = canvas.getContent(); // getContent() of current mode
  9. var _validator = new Trex.Validator();
  10. if(!_validator.exists(_content)) {
  11. return _NULL;
  12. }
  13. return {
  14. 'inputmode': canvas.getCurrentPanel().getName(),
  15. 'content': _content,
  16. 'attachments': function() {
  17. var _attachments = sidebar.getAttachments(); // all getAttachments()
  18. return editor.getEntryProxy().getAttachments(_attachments, _TRUE);
  19. }(),
  20. 'resultBox': function() {
  21. var _resultBox = editor.getResultBox();
  22. var datas = [];
  23. _resultBox.datalist.each(function(entry){
  24. datas.push(entry.data);
  25. });
  26. return datas;
  27. }(),
  28. 'formfield': editor.getForm().getFormField()
  29. };
  30. };
  31. editor.setDataByJSON = function(jsonData) {
  32. if(!jsonData) {
  33. return;
  34. }
  35. var _editorMode = canvas.mode;
  36. var _inputMode = jsonData.inputmode || _editorMode;
  37. if (_inputMode == 'original') { //save
  38. } else if(_inputMode != _editorMode) {
  39. canvas.fireJobs(Trex.Ev.__CANVAS_MODE_INITIALIZE, _editorMode, _inputMode);
  40. canvas.changeMode(_inputMode);
  41. }
  42. var _content = jsonData.content;
  43. if(jsonData.attachments) {
  44. editor.getEntryProxy().setAttachments(jsonData.attachments, _content);
  45. }
  46. if(_content) {
  47. _content = editor.getDocParser().convertAtLoad(_content, _editorMode, _inputMode); //onlyHTML
  48. canvas.initContent(_content);
  49. }
  50. if (jsonData.resultBox) {
  51. jsonData.resultBox.each(function(data){
  52. var _actor;
  53. _actor = sidebar.searchers[data._meta.type];
  54. if (_actor) {
  55. _actor.execReload(data, _content);
  56. }
  57. });
  58. }
  59. sidebar.syncSidebar(); //sync
  60. if(jsonData.formfield) {
  61. editor.getForm().setFormField(jsonData.formfield);
  62. }
  63. };
  64. }
  65. );
  66. Trex.Save = Trex.Class.create({
  67. editor: _NULL,
  68. toolbar: _NULL,
  69. sidebar: _NULL,
  70. canvas: _NULL,
  71. config: _NULL,
  72. form: _NULL,
  73. initialize: function(editor, toolbar, sidebar, canvas, config) {
  74. this.editor = editor;
  75. this.toolbar = toolbar;
  76. this.sidebar = sidebar;
  77. this.canvas = canvas;
  78. this.config = config;
  79. this.form = editor.getForm();
  80. this.docparser = editor.getDocParser();
  81. this.entryproxy = editor.getEntryProxy();
  82. },
  83. save: function() {
  84. try {
  85. if (typeof validForm == "function") {
  86. if (!validForm(this.editor)) {
  87. return _FALSE;
  88. }
  89. }
  90. if (typeof setForm == "function") {
  91. if (!setForm(this.editor)) {
  92. return _FALSE;
  93. }
  94. }
  95. return _TRUE;
  96. } catch(e) {
  97. this.editor.fireJobs(Trex.Ev.__RUNTIME_EXCEPTION, e);
  98. return _FALSE;
  99. }
  100. },
  101. submit: function() {
  102. if(this.save()) {
  103. this.editor.fireJobs(Trex.Ev.__ON_SUBMIT, this.editor);
  104. if ( this.config.save && typeof this.config.save.onSave == "function" ){
  105. var externalSaveHandler = this.config.save.onSave;
  106. externalSaveHandler();
  107. } else {
  108. this.form.submit();
  109. }
  110. }
  111. },
  112. getContent: function(outputMode) {
  113. var _canvas = this.canvas;
  114. //에디터모드, 출력모드
  115. var _editorMode = _canvas.mode;
  116. var _outputMode = outputMode || "original";
  117. var _content = _canvas.getContent(); // getContent() of current mode
  118. _content = this.docparser.convertAtSave(_content, _editorMode, _outputMode);
  119. return _content;
  120. },
  121. getAttachments: function(type, all) {
  122. all = all || _FALSE;
  123. var _attachments = this.sidebar.getAttachments(type); // all getAttachments()
  124. return this.entryproxy.getAttachments(_attachments, all);
  125. },
  126. getEmbeddedData: function(type) {
  127. return this.sidebar.getEmbeddedData(type);
  128. },
  129. getResults: function(type) {
  130. return this.sidebar.getResults(type);
  131. },
  132. /*
  133. data = {
  134. content: "string",
  135. inputmode: "string",
  136. attachments: [{
  137. attacher: "string",
  138. data: {object}
  139. }]
  140. }
  141. */
  142. load: function(jsonData) { //NOTE: data format = JSON
  143. this.editor.fireJobs(Trex.Ev.__EDITOR_LOAD_DATA_BEGIN);
  144. if (!jsonData) {
  145. throw new Error("[Exception]Trex.Save : not exist argument(data)");
  146. }
  147. if (typeof loadForm == "function") {
  148. loadForm(this.editor, jsonData);
  149. }
  150. try { //#FTDUEDTR-1111
  151. this.setDataByJSONToEditor(jsonData);
  152. } catch (error) {
  153. alert(' - Error: ' + error.message + '\n소스보기 모드로 전환합니다.\n잘못된 HTML이 있는지 확인해주세요.');
  154. jsonData.inputmode = Trex.Canvas.__HTML_MODE;
  155. try {
  156. this.setDataByJSONToEditor(jsonData);
  157. } catch(ignore) {}
  158. }
  159. if (typeof postLoad == "function") {
  160. postLoad(this.editor, jsonData);
  161. }
  162. this.editor.fireJobs(Trex.Ev.__EDITOR_LOAD_DATA_END);
  163. },
  164. setDataByJSONToEditor: function (jsonData) {
  165. this.editor.setDataByJSON({
  166. 'inputmode': (!jsonData.inputmode || jsonData.inputmode == 'html')? 'original': jsonData.inputmode,
  167. 'content': function() {
  168. var _contentObj = jsonData.content;
  169. if (typeof _contentObj == "string") {
  170. return jsonData.content;
  171. } else if (_contentObj && _contentObj.nodeType && (_contentObj.nodeType == 1)) {
  172. return jsonData.content.value;
  173. } else {
  174. return '';
  175. }
  176. }(),
  177. 'attachments': jsonData.attachments
  178. });
  179. },
  180. makeField: function() {
  181. var _sidebar = this.sidebar;
  182. var _form = this.form;
  183. //NOTE: create field content
  184. var _content = this.getContent();
  185. _form.createField(tx.textarea({ name: "tx_content", style: { display: "none" } }, _content));
  186. //NOTE: create field attach
  187. var _fields = _sidebar.getFields();
  188. _fields.each(function(field) {
  189. _form.createField(tx.input({ type: "hidden", name: field.name, value: field.value }));
  190. });
  191. }
  192. });