5 articleItem.init = function init () {
15 commentstatus: "enabled",
31 primaryTopicIconURL: "",
42 default: function () {return new Contents();},
45 type: [Boolean, Number],
55 'editor.introtext': watchIntrotext,
56 'editor.bodytext': watchBodytext,
57 'editor.title': watchTitle,
58 'editor.url': watchURL,
59 'editor.createtime': watchCreatetime,
60 'editor.author': watchAuthor,
61 'editor.dept': watchDept,
62 'editor.tags_string': updateTopics,
65 function watchIntrotext(val, oldVal) {
66 this.item.introtext = (val.length > 0) ? quoteHtml(val) : "";
69 function watchBodytext(val, oldVal) {
70 this.item.bodytext = (val.length > 0) ? quoteHtml(val) : "";
73 function watchTitle(val, oldVal) {
74 this.item.title = (val.length > 0) ? quoteTitle(val) : "";
77 function watchURL(val, oldVal) {
78 this.item.url = (val.length > 0) ? val : "";
81 function watchCreatetime(val, oldVal) {
82 this.item.time_string = nsUtil.formatToLocalDateTime(new Date(val));
85 function watchAuthor(val, oldVal) {
86 this.item.author = val;
89 function watchDept(val, oldVal) {
101 function editable() {
102 return (this.vm.user.uid == this.item.uid);
106 if (this.item.content_type == 'story') {
107 return "/story/" + this.item.sid + "/";
108 } else if (this.item.content_type == 'journal') {
109 return "/journal/" + this.item.id + "/";
110 } else if (this.item.content_type == 'submission') {
111 return "/submission/" + this.item.subid + "/";
117 * methods and relative private functions
121 showPreview: showPreview,
123 leavePreview: leavePreview,
124 startEdit: startEdit,
125 cancelEdit: cancelEdit,
126 updateTopics: updateTopics,
127 topicIconURL: topicIconURL,
128 updatePrimaryTopicIconURL: updatePrimaryTopicIconURL,
131 function updatePrimaryTopicIconURL() {
132 if (this.item.primary_topic) {
133 this.primaryTopicIconURL = nsUtil.topicIconURL(this.item.primary_topic.keyword);
136 this.primaryTopicIconURL = "";
139 function editItem(item) {
141 item.editor.startEdit();
144 function showPreview(event) {
145 var type = this.item.content_type;
147 console.log("no content_type!");
150 var postData = _preparePostData(this, "preview");
152 this.$newslash.post(type,
154 { csrfToken: this.csrfToken,
156 (response) => { // success
158 this.previewTitle = response.body.item.title;
159 this.previewIntro = response.body.item.introtext;
160 this.item.primary_topic = response.body.item.topic;
161 this.item.url = response.body.item.url;
162 this.updatePrimaryTopicIconURL();
164 this.showSubmit = true;
165 this.showForm = false;
166 this.disablePost= false;
168 (response) => { // fail
169 if (response.body.message) {
170 this.message = response.body.message;
176 function postItem(event) {
177 this.disablePost = true;
178 var postData = _preparePostData(this, "post");
179 var type = this.item.content_type;
181 console.log("no content_type!");
185 this.$newslash.post(type, postData, {csrfToken: this.csrfToken},
186 (response) => { // success
188 var id = response.body.id;
189 var type = response.body.type;
190 var url = '/' + type + '/' + id;
194 this.createdUrl = url;
195 this.message = "post_success";
198 this.editing = false;
199 this.showSubmit = false;
200 this.showForm = false;
202 (response) => { // fail
203 this.message = response.body.message;
204 this.disablePost= false;
210 function _preparePostData(vm, action) {
211 action = action || "preview";
216 postData.action = action;
218 if (articleItem.recaptchaToken) {
219 postData.recaptcha_token = articleItem.recaptchaToken;
222 Object.keys(vm.editor).forEach(k => {
223 postData.item[k] = vm.editor[k];
229 function leavePreview(event) {
230 this.previewTitle = "";
231 this.previewIntro = "";
232 this.previewBody = "";
233 this.showSubmit = false;
234 this.showForm = true;
237 function quoteHtml(html) {
238 return nsUtil.quoteHtml(html, editorConfig.allowed_tags);
241 function quoteTitle(html) {
242 return nsUtil.quoteTitle(html, {});
245 function startEdit() {
249 function cancelEdit() {
250 for (k in this.originalItem) {
251 this.item[k] = this.originalItem[k];
253 this.editing = false;
256 function updateTopics() {
257 var tags = this.editor.tags_string.split(" ");
258 this.currentTopics.splice(0);
259 if (tags.length == 0) {
263 tags.forEach((tag) => {
265 var keyword = topics[tag].keyword;
266 if (!pushed[keyword]) {
267 this.currentTopics.push(keyword);
274 function topicIconURL(topic) {
275 return nsUtil.topicIconURL(topic);
279 * created and related private functions
282 if (this.showEditor) {
286 this.updatePrimaryTopicIconURL();
289 function _initEditor(vm) {
290 // backup original item
291 // copy item property to vm
292 vm.originalItem = {};
293 for (k in vm.$data.item) {
295 vm.originalItem[k] = vm.item[k];
300 if (vm.item.createtime) {
301 var dt = nsUtil.decodeMySQLDateTime(vm.item.createtime);
302 vm.editor.createtime = nsUtil.formatToLocalISOString(dt);
304 vm.editor.createtime = nsUtil.formatToLocalISOString(new Date());
306 vm.editor.author = user.nickname;
309 var tagnames = vm.item.tags.map(x => {return x.tagname});
310 vm.editor.tags_string = tagnames.join(" ");
312 updateTopics.call(vm);
316 if (page.type == 'story' && page.stoid) {
319 vm.$newslash.getStoryByID(page.stoid,
320 (resp) => { // succeed
321 for (k in resp.body.item) {
322 vm.editor.item[k] = resp.body.item[k];
326 vm.message = resp.body.message;
330 // submission to story mode
331 if (vm.item.submission_id) {
332 vm.$newslash.getSubmissionByID(vm.item.submission_id,
333 (resp) => { // succeed
334 vm.editor.title = resp.body.item.title;
335 vm.editor.introtext = resp.body.item.drafttext;
338 vm.message = resp.body.message;
346 Vue.component('article-item', {
347 template: '#article-item-template',
357 articleItem.run = function (params) {
358 params = params || {};
360 console.log('error in articleItem.run(): no element given');
363 this.vm = new Vue({el: params.el});
366 articleItem.addTrigger = function addTrigger (selector, params) {
367 params = params || {};
369 console.log('error in articleItem.addTrigger(): no element given');
373 console.log('error in articleItem.addTrigger(): no selector given');
376 $(selector).on('click', function (e) {
377 articleItem.run(params);