OSDN Git Service

js: fix indenting
[newslash/newslash.git] / src / newslash_web / public / js / editor.js
1 /* post.js */
2 var post = {};
3 var editor = {};
4 var viewModel;
5
6 var allowed_tags = {
7   "a": ["href"],
8   "blockquote": [],
9   "i": [],
10   "strong": [],
11 };
12
13 post.quote_html = function _post_quote_html(html) {
14   const escaped = eh.escape(allowed_tags, html);
15   const quoted = eh.blank_line_to_paragraph(escaped);
16   const parsed = $.parseHTML(quoted, null);
17   const result = [];
18   parsed.forEach((i) => {
19     i.normalize();
20     result.push(i.outerHTML);
21   });
22   return result.join('\n');
23 };
24
25 post.quote_title = function _post_quote_title(html) {
26   const escaped = '<div>' + eh.escape({}, html) + '</div>';
27   const parsed = $.parseHTML(escaped, null);
28   const result = [];
29   parsed.forEach((i) => {
30     i.normalize();
31     const t = i.innerHTML || i.wholeText;
32     result.push(t);
33   });
34   return result.join('');
35 };
36
37 var editor = {
38   title: "",
39   createtime: "",
40   author: "anonymous coward",
41   dept: "",
42   introtext: "",
43   bodytext: "",
44   add_related: "",
45   url: "",
46   mediaurl: "",
47   mediatype: "",
48   email: "",
49   commentstatus: "enabled",
50   display: 1,
51   submissioncopy: 0,
52
53   show_form: true,
54   show_submit: false,
55   preview_introtext: "",
56   preview_bodytext: "",
57   preview_title: "",
58   message: "",
59   action: "preview",
60 };
61
62 var computed = {
63   introtext_raw: function () {
64     if (this.preview_introtext.length > 0) {
65       return this.preview_introtext;
66     } else {
67       return post.quote_html(this.introtext);
68     }
69   },
70   bodytext_raw: function () {
71     if (this.preview_bodytext.length > 0) {
72       return this.preview_bodytext;
73     } else {
74       return post.quote_html(this.bodytext);
75     }
76   },
77   title_raw: function () {
78     if (this.preview_title.length > 0) {
79       return this.preview_title;
80     } else {
81       return post.quote_title(this.title);
82     }
83   },
84   author_name: function () {
85     if (this.author.length > 0) {
86       return this.author;
87     } else {
88       return authors[this.uid];
89     }
90   }
91 };
92
93 var methods = {
94   show_preview: function (event) {
95     this.action = "preview";
96     this.message = "";
97     this.$http.post(this.postUrl, this.$data).then(
98       (response) => { // success
99         this.message = "";
100         this.preview_title = response.body.title;
101         this.preview_introtext = response.body.introtext;
102         this.show_form = false;
103         this.show_submit = true;
104       },
105       (response) => { // fail
106         if (response.body.message) {
107           this.message = response.body.message;
108         }
109       }
110     );
111   },
112   post_submission: function (event) {
113     this.action = "post";
114     this.message = "";
115     this.$http.post(this.postUrl, this.$data).then(
116       (response) => { // success
117         this.message = "";
118         const id = response.body.id;
119         const type = response.body.type;
120         const url = '/' + type + '/' + id;
121         this.message = "投稿しました。URL:"
122           + "<a href='" + url + "'>" + url + "</a>";
123         this.show_form = false;
124         this.show_submit = false;
125       },
126       (response) => { // fail
127         if (response.body.message) {
128           this.message = response.body.message;
129         }
130       }
131     );
132   },
133   leave_preview: function (event) {
134     this.preview_title = "";
135     this.preview_introtext = "";
136     this.preview_bodytext = "";
137     this.show_form = true;
138     this.show_submit = false;
139   },
140 };
141
142 editor.run = function run (params) {
143   if (params.type == 'submission') {
144     editor.postUrl = '/api/v1/submission';
145   } else if (params.type == 'story') {
146     editor.postUrl = '/api/v1/story';
147   } else if (params.type == 'journal') {
148     editor.postUrl = '/api/v1/journal';
149   }
150   if (params.author) {
151     editor.author = params.author;
152   }
153   viewModel = new Vue({el: params.el,
154                        data: editor,
155                        computed: computed,
156                        methods: methods});
157 };