From: hylom Date: Wed, 29 Aug 2018 12:30:04 +0000 (+0900) Subject: js: fix editor related code to make more readable X-Git-Tag: v0.1.5~63 X-Git-Url: http://git.osdn.net/view?p=newslash%2Fnewslash.git;a=commitdiff_plain;h=72bd102b517f6f3fe3d5b9d43081e7386a562184 js: fix editor related code to make more readable --- diff --git a/src/newslash_web/public/js/article-item2.js b/src/newslash_web/public/js/article-item2.js index 4908ada8..8baa60ac 100644 --- a/src/newslash_web/public/js/article-item2.js +++ b/src/newslash_web/public/js/article-item2.js @@ -175,6 +175,7 @@ articleItem.init = function init () { this.previewIntro = response.body.item.intro_text; this.item.primary_topic = response.body.item.topic; this.item.url = response.body.item.url; + this.item.dept = response.body.item.dept; this.updatePrimaryTopicIconURL(); this.mode = "preview"; @@ -320,14 +321,18 @@ articleItem.init = function init () { this.updatePrimaryTopicIconURL(); } - function _loadItem(vm, callback) { + function _startEdit(c) { + _loadItem(c, () => { _initEditor(c); }); + } + + function _loadItem(c, callback) { if (!page) { return callback(); } if (page.contentType == "journal") { newslash.getJournal(page.id).then( (resp) => { for (k in resp.journal) { - vm.item[k] = resp.journal[k]; + c.item[k] = resp.journal[k]; } callback(); }, @@ -340,79 +345,77 @@ articleItem.init = function init () { return callback(); } - function _startEdit(vm) { - _loadItem(vm, () => { _initEditor(vm); }); - } - - function _initEditor(vm) { + function _initEditor(c) { // backup original item - // copy item property to vm - vm.originalItem = {}; - for (k in vm.$data.item) { - if (vm.item[k]) { - vm.originalItem[k] = vm.item[k]; + // copy item property to c (component) + c.originalItem = {}; + for (k in c.$data.item) { + if (c.item[k]) { + c.originalItem[k] = c.item[k]; } } // set createtime - if (vm.item.createtime) { - var dt = nsUtil.decodeMySQLDateTime(vm.item.createtime); - vm.editor.createtime = nsUtil.formatToLocalISOString(dt); + if (c.item.createtime) { + var dt = nsUtil.decodeMySQLDateTime(c.item.createtime); + c.editor.createtime = nsUtil.formatToLocalISOString(dt); } else { - vm.editor.createtime = nsUtil.formatToLocalISOString(new Date()); + c.editor.createtime = nsUtil.formatToLocalISOString(new Date()); } - vm.editor.author = user.nickname; + c.editor.author = user.nickname; - if (vm.item.tags) { - var tagnames = vm.item.tags.map(x => {return x.tagname}); - vm.editor.tags_string = tagnames.join(" "); + if (c.item.tags) { + var tagnames = c.item.tags.map(x => {return x.tagname}); + c.editor.tags_string = tagnames.join(" "); } - updateTopics.call(vm); + updateTopics.call(c); // journal edit mode if (page.type == 'journal' && page.id) { - vm.cancelable = 1; - vm.editor.title = vm.item.title; - vm.editor.intro_text = vm.item.article; - vm.editor.tags_string = vm.item.tags_string || ""; - vm.editor.comment_status = vm.item.comment_status; - vm.editor.formatter = vm.item.formatter; - vm.editor.posttype = vm.item.posttype; - vm.editor.id = vm.item.id; + c.cancelable = 1; + c.editor.title = c.item.title; + c.editor.intro_text = c.item.article; + c.editor.tags_string = c.item.tags_string || ""; + c.editor.comment_status = c.item.comment_status; + c.editor.formatter = c.item.formatter; + c.editor.posttype = c.item.posttype; + c.editor.id = c.item.id; } // story edit mode if (page.type == 'story' && page.stoid) { - vm.cancelable = 1; + c.cancelable = 1; + c.item.content_type = "story"; - vm.$newslash.getStoryByID(page.stoid, + c.$newslash.getStoryByID(page.stoid, (resp) => { // succeed for (k in resp.body.item) { - vm.editor.item[k] = resp.body.item[k]; + c.editor.item[k] = resp.body.item[k]; } }, (resp) => { //failed - vm.message = resp.body.message; + c.message = resp.body.message; }); } // submission to story mode - if (vm.item.submission_id) { - vm.$newslash.getSubmissionByID(vm.item.submission_id, + if (c.item.submission_id) { + c.$newslash.getSubmissionByID(c.item.submission_id, (resp) => { // succeed - vm.editor.title = resp.body.item.title; - vm.editor.intro_text = resp.body.item.drafttext; + c.editor.title = resp.body.item.title; + c.editor.intro_text = resp.body.item.drafttext; + c.item.content_type = "story"; }, (resp) => { //failed - vm.message = resp.body.message; + c.message = resp.body.message; }); } // check formatter - if (vm.editor.formatter == "legacy") { - vm.enableAutoPreview = 0; + if (c.editor.formatter == "legacy") { + c.enableAutoPreview = 0; } - vm.mode = "editing"; + c.mode = "editing"; } /* @@ -430,6 +433,10 @@ articleItem.init = function init () { }; +/* + * utilities + */ + articleItem.run = function (params) { params = params || {}; if (!params.el) { diff --git a/src/newslash_web/public/js/base.js b/src/newslash_web/public/js/base.js index bfc9086c..928e6690 100644 --- a/src/newslash_web/public/js/base.js +++ b/src/newslash_web/public/js/base.js @@ -38,39 +38,6 @@ var Content = function(contentType) { this.editing = true; }; -// Contents - story, journal, submission, etc. -var Contents = function() { - - // Common - this.uid = 0; - this.title = ""; - this.createtime = ""; - this.author = "anonymous coward"; - this.introtext = ""; - this.content_type = ""; - - // Story - this.stoid = 0; - this.sid = 0; - this.dept = ""; - this.bodytext = ""; - this.add_related = ""; - this.url = ""; - this.mediaurl = ""; - this.mediatype = ""; - this.email = ""; - this.commentstatus = "enabled"; - this.display = 1; - this.submissioncopy = 0; - this.submission_id = 0; - - // for internal use - this.tagsString = ""; - this.related = ""; - this.editing = true; -}; - - // Comments var Comment = function () { diff --git a/src/newslash_web/public/js/editor.js b/src/newslash_web/public/js/editor.js index d8392134..594a1760 100644 --- a/src/newslash_web/public/js/editor.js +++ b/src/newslash_web/public/js/editor.js @@ -3,7 +3,7 @@ var editor = {}; editor.run = function run(params) { var type = params.type || 'submission'; var data = { - item: new Contents(), + item: new Content(), }; if (type == 'story' && params.submissionID) {