This repository has been archived by the owner on Oct 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
toot_new.js
423 lines (393 loc) · 14.4 KB
/
toot_new.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
function post_cw() {
var cw_input = document.getElementById("cw_input");
var cwicon = document.getElementById("cw_bt");
if (cw_input.style.display == "block") { //CW オン→オフ
cw_input.style.display = "none";
cw_input.value = "";
cwicon.className = quiet;
} else { //CW オフ→オン
cw_input.style.display = "block";
cwicon.className = button;
}
}
function post_vote() {
var cw_input = document.getElementById("vote_new_list");
var cwicon = document.getElementById("vote_bt");
if (cw_input.style.display == "block") { //vote オン→オフ
cw_input.style.display = "none";
document.getElementById("vote_new_1").value = "";
document.getElementById("vote_new_2").value = "";
document.getElementById("vote_new_3").value = "";
document.getElementById("vote_new_4").value = "";
document.getElementById("vote_new_time").value = "30";
cwicon.className = quiet;
} else { //vote オフ→オン
cw_input.style.display = "block";
cwicon.className = button;
}
}
function up_file(simple) {
var simple_id = "";
if (simple) image_mode = simple_id = "_simple"; else image_mode = simple_id = "";
var card = document.getElementsByClassName("media-upload"+simple_id);
if (card.length >= 4) {
showtoast("maximum-media");
} else {
navigator.camera.getPicture(up_file_suc, file_error,
{ quality: 100,
destinationType: Camera.DestinationType.DATA_URL,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
encodingType: 1
});
}
}
function up_file_suc(base64, mode_blob) {
var blob;
if (base64 || mode_blob) {
show('now_loading');
if (base64) {
var binary = atob(base64);
var array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
blob = new Blob([new Uint8Array(array)], {type: 'image/png'});
} else if (mode_blob) {
blob = mode_blob;
}
var formData = new FormData();
formData.append('file', blob);
fetch("https://"+inst+"/api/v1/media", {
headers: {'Authorization': 'Bearer '+localStorage.getItem('knzkapp_now_mastodon_token')},
method: 'POST',
body: formData
}).then(function(response) {
if(response.ok) {
return response.json();
} else {
throw new Error();
}
}).then(function(json) {
if (json["id"] && json["preview_url"] != "https://knzk.me/files/small/missing.png") {
document.getElementById("image_list"+image_mode).innerHTML = "<ons-card onclick=\"file_del(this)\" style='background-image: url("+json["preview_url"]+")' class='card-image media-upload"+image_mode+"' data-mediaid='"+json["id"]+"'></ons-card>" + document.getElementById("image_list"+image_mode).innerHTML;
image_mode = "";
hide('now_loading');
} else {
hide('now_loading');
showtoast('cannot-pros');
}
}).catch(function(error) {
showtoast('cannot-pros');
console.log(error);
hide('now_loading');
});
}
}
function file_del(card) {
ons.notification.confirm('画像を削除してもよろしいですか?').then(function (e) {
if (e === 1) {
card.parentNode.removeChild(card);
}
});
}
function file_error(msg) {
console.log(msg);
}
function post_nsfw(simple) {
var simple_id = "";
if (simple) simple_id = "_simple";
var cw_input = document.getElementById("nsfw_input"+simple_id);
var cwicon = document.getElementById("nsfw_bt"+simple_id);
if (cwicon.className == button + " w-max") { //選択済み→解除
cw_input.value = "";
cwicon.className = quiet;
} else {
cw_input.value = "1";
cwicon.className = button + " w-max";
}
}
function post_localonly(simple) {
var simple_id = "";
if (simple) simple_id = "_simple";
var cw_input = document.getElementById("localonly_input"+simple_id);
var cwicon = document.getElementById("localonly_bt"+simple_id);
if (cwicon.className == button) { //選択済み→解除
cw_input.value = "";
if (simple)
cwicon.className = quiet + " no-rd";
else
cwicon.className = quiet;
} else {
cw_input.value = "1";
cwicon.className = button;
}
}
function post_mode(simple) {
var simple_id = "";
if (simple) simple_id = "_simple";
var input_obj = document.getElementById("post_mode"+simple_id);
var bt_obj = document.getElementById("post_mode_bt"+simple_id);
ons.openActionSheet({
cancelable: true,
buttons: [
{label: '公開', icon: 'fa-globe'},
{label: '非収載', icon: 'fa-unlock-alt'},
{label: '非公開', icon: 'fa-lock'},
{label: 'ダイレクト', icon: 'fa-envelope'},
{
label: 'キャンセル',
icon: 'md-close'
}
]
}).then(function (index) {
if (index == 0) {
input_obj.value = "public";
bt_obj.innerHTML = "公開";
} else if (index == 1) {
input_obj.value = "unlisted";
bt_obj.innerHTML = "非収載";
} else if (index == 2) {
input_obj.value = "private";
bt_obj.innerHTML = "非公開";
} else if (index == 3) {
input_obj.value = "direct";
bt_obj.innerHTML = "ダイレクト";
}
});
}
function check_limit(value, id, tb_id, cw_id) {
var limit = 0;
if (cw_id) {
var cw = document.getElementById(cw_id).value;
limit = toot_limit - value.length - cw.length;
} else {
limit = toot_limit - value.length;
}
document.getElementById(id).innerHTML = limit;
if (limit < 0) {
document.getElementById(id).setAttribute('style', 'color: red');
} else {
document.getElementById(id).setAttribute('style', '');
}
}
function show_bbcodegen(id, limit, button) {
tmp_bbcode_limit = limit;
tmp_bbcode_tootbutton = button;
tmp_bbcode_id = id;
tmp_post_text = document.getElementById(id).value;
loadNav("bbcode.html");
}
function bbcodegen(force) {
var text = document.getElementById("bbcode_text").value;
var base = document.getElementById("bbcode_base").value;
var color = document.getElementById("bbcode_color").value;
var large = document.getElementById("bbcode_large").value;
var spin = parseInt(document.getElementById("bbcode_spin").value);
var pulse = parseInt(document.getElementById("bbcode_pulse").value);
var pre = "", suf = "", buf = "", value = "";
if (spin > 9 && !force) {
ons.notification.confirm('回転を多く指定すると、TLが重くなる原因となり、警告を受ける可能性があります。続行しますか?', {title: '注意:TLが重くなります'}).then(function (e) {
if (e === 1) {
bbcodegen(true);
}
});
} else {
if (spin) {
for (var i = 0; i < spin; i++) {
pre += "[spin]";
suf = "[/spin]" + suf;
}
}
if (base) {
pre += "["+base+"]";
suf = "[/"+base+"]" + suf;
}
if (color) {
pre += "[colorhex="+color+"]";
suf = "[/colorhex]" + suf;
}
if (pulse) {
for (var p = 0; p < pulse; p++) {
pre += "[pulse]";
suf = "[/pulse]" + suf;
}
}
if (large) {
/* sizeは潰れた
large = large * 16;
pre += "[size="+large+"]";
suf = "[/size]" + suf;
*/
pre += "[large="+large+"x]";
suf = "[/large]" + suf;
}
buf = pre + text + suf;
value = tmp_post_text + buf;
var limit = toot_limit - value.length;
if (limit < 0) {
showtoast('bbcode-limit');
} else {
document.querySelector('#navigator').popPage();
document.getElementById(tmp_bbcode_id).value = value;
check_limit(value, tmp_bbcode_limit, tmp_bbcode_tootbutton);
}
}
}
function bbcode_color(color) {
BackTab();
var color_s = "#" + color;
if (color_mode) {
var d_box = document.getElementById("doodle-color-box-mini");
if (color) {
doodle_old_color = color_s;
sketcher.color = color_s;
d_box.style.backgroundColor = color_s;
} else {
sketcher.color = color_s;
d_box.style.backgroundColor = "#000000";
}
} else {
var b_color = document.getElementById("bbcode_color");
var b_box = document.getElementById("color-box-mini");
if (color) {
b_color.value = color;
b_box.style.backgroundColor = color_s;
} else {
b_color.value = "";
b_box.style.backgroundColor = "";
}
}
}
function post(id, option, simple) {
var media_id = Array(), i, simple_id = "", optiondata = {
status: document.getElementById(id).value,
visibility: option.visibility
};
if (simple) {
simple_close();
show('post_now');
simple_id = "_simple";
} else
show('now_loading');
var media = document.getElementsByClassName("media-upload"+simple_id);
var vote1 = document.getElementById("vote_new_1"+simple_id).value;
var vote2 = document.getElementById("vote_new_2"+simple_id).value;
var vote3 = document.getElementById("vote_new_3"+simple_id).value;
var vote4 = document.getElementById("vote_new_4"+simple_id).value;
var votem = document.getElementById("vote_new_time"+simple_id).value;
if (vote1 != "" && vote2 != "") {
optiondata.isEnquete = true;
optiondata.enquete_duration = parseInt(votem);
optiondata.enquete_items = [vote1, vote2, vote3, vote4];
}
if (option.cw) {
optiondata.spoiler_text = option.cw;
}
if (option.local_only) {
optiondata.status += " 👁️";
}
if (option.sensitive && media[0]) {
optiondata.sensitive = true;
}
if (option.in_reply_to_id) {
optiondata.in_reply_to_id = option.in_reply_to_id;
}
if (media[0]) {
i = 0;
while (media[i]) {
media_id[i] = media[i].dataset.mediaid;
i++;
}
optiondata.media_ids = media_id;
}
fetch("https://"+inst+"/api/v1/statuses", {
headers: {'content-type': 'application/json', 'Authorization': 'Bearer '+localStorage.getItem('knzkapp_now_mastodon_token')},
method: 'POST',
body: JSON.stringify(optiondata)
}).then(function(response) {
if(response.ok) {
return response.json();
} else {
throw new Error();
}
}).then(function(json) {
if (json["id"]) {
if (simple) {
$("#simple_toot_form").find("textarea, input, select").val("").end().find(":checked").prop("checked", false);
$("#simple_vote").find("textarea, input, select").val("").end().find(":checked").prop("checked", false);
$("#simple_toot_cw").val("");
check_limit(document.getElementById("simple_toot_TL_input").value, 'toot_limit_simple', 'toot-button_simple', 'simple_toot_cw');
document.getElementById("image_list_simple").innerHTML = "";
$("#post_mode_simple").val(default_post_visibility);
document.getElementById("post_mode_bt_simple").innerHTML = visibility_name(default_post_visibility);
hide('post_now');
} else {
hide('now_loading');
BackTab('down');
}
} else {
showtoast('cannot-post');
if (simple) hide('post_now');
else hide('now_loading');
}
}).catch(function(error) {
showtoast('cannot-post');
console.log(error);
if (simple) hide('post_now');
else hide('now_loading');
});
}
function simple_open() {
if (getConfig(1, 'st_stop')) home_auto_event = false;
document.getElementById("simple_toot_TL_input").rows = 3;
$("#simple_toot_TL_toolbar").addClass("simple_toot_open");
$("#simple_more").removeClass("invisible");
$("#dial_main").addClass("fab_simple_toot_open");
$("#dial_TL").addClass("fab_simple_toot_open");
var emoji = document.getElementById("emoji_list_popover"), i = 0, reshtml = "";
if (emoji.innerHTML == "load") {
fetch("https://"+inst+"/api/v1/custom_emojis", {
headers: {'content-type': 'application/json'},
method: 'GET',
}).then(function(response) {
if(response.ok) {
return response.json();
} else {
//カスタム絵文字非対応インスタンス
$("#simple_emoji_bt").addClass("invisible");
}
}).then(function(json) {
while (json[i]) {
reshtml += "<ons-button modifier=\"quiet\" onclick='add_emoji_simple(\""+json[i]["shortcode"]+"\")'><img draggable=\"false\" class=\"emojione\" src=\""+json[i]["url"]+"\"></ons-button>\n";
i++;
}
emoji.innerHTML = reshtml;
});
}
}
function simple_close() {
document.getElementById("simple_toot_TL_input").rows = 1;
$("#simple_toot_TL_toolbar").removeClass("simple_toot_open");
$("#simple_more").addClass("invisible");
$("#dial_main").removeClass("fab_simple_toot_open");
$("#dial_TL").removeClass("fab_simple_toot_open");
if (getConfig(1, 'st_stop')) {
home_auto_event = true;
home_autoevent();
}
}
function add_emoji_simple(addtext, mode) {
// https://qiita.com/noraworld/items/d6334a4f9b07792200a5
var id = "simple_toot_TL_input";
if (mode) id = "toot_textarea";
var textarea = document.getElementById(id);
var sentence = textarea.value;
var len = sentence.length;
var pos = textarea.selectionStart;
var before = sentence.substr(0, pos);
var word = " :" + addtext + ": ";
var after = sentence.substr(pos, len);
sentence = before + word + after;
textarea.value = sentence;
hidePopover('emoji_popover');
}