script_util.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. checkRoute();
  2. let userBasics = JSON.parse(localStorage.getItem('user_profile')) || [];
  3. if(userBasics !== []){
  4. $('.userName').html(`<h2 class="user-name text-white mt-4 fw-bold">Hi ${userBasics.user_info.userName}</h2>`);
  5. }
  6. function checkRoute() {
  7. let jwt_token = get_jwt_token();
  8. if(jwt_token == undefined) {
  9. window.location.replace("login.html");
  10. }
  11. axios({
  12. method: 'post',
  13. url: 'https://www.choozmo.com:8887/user_profile',
  14. headers: {
  15. 'accept': 'text/html',
  16. 'Authorization': `Bearer ${jwt_token}`
  17. }
  18. }).then(res => {
  19. console.log(res.data);
  20. if(res.status !== 200) {
  21. window.location.replace("login.html");
  22. }
  23. var userName='';
  24. userName+=`<h2 class="user-name text-white mt-4 fw-bold">Hi ${res.data.user_info.userName}</h2>`;
  25. $('.userName').html(userName);
  26. }).catch(err => {
  27. console.log(err);
  28. });
  29. }
  30. const btnLoginPage = document.querySelector('.btn-login');
  31. const btnUserProfile = document.querySelector('.btn-userProfile');
  32. const btnLogout = document.querySelector('.btn-logout');
  33. function loginControl() {
  34. btnLoginPage.style.display = 'none';
  35. btnLogout.style.display = 'block';
  36. btnUserProfile.style.display = 'block';
  37. }
  38. loginControl();
  39. var client_id = Date.now()
  40. var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
  41. var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
  42. return new bootstrap.Tooltip(tooltipTriggerEl)
  43. });
  44. var myModal = new bootstrap.Modal(document.getElementById('history'), {
  45. keyboard: false
  46. })
  47. var avatarModal = new bootstrap.Modal(document.getElementById('avatarmega'), {
  48. keyboard: false
  49. })
  50. var modalImg = document.querySelector("#avatarmega .modal-img");
  51. var modalTitle = document.querySelector("#avatarmega .modal-title");
  52. var avatarSelector = document.getElementById("avatar");
  53. var card = document.getElementsByClassName('card');
  54. card = [...card];
  55. avatarSelector.addEventListener('change', avatarChange);
  56. avatarChange();
  57. function addCardListener() {
  58. for (let i = 0; i < card.length; i++) {
  59. card[i].addEventListener('click', openavatarModel);
  60. }
  61. }
  62. addCardListener();
  63. function avatarChange() {
  64. var value = avatarSelector.options[avatarSelector.selectedIndex].text;
  65. $('.owl-carousel').trigger('to.owl.carousel', avatarSelector.selectedIndex);
  66. for (let i = 0; i < card.length; i++) {
  67. card[i].classList.remove('active');
  68. if (card[i].dataset.avatar == value) {
  69. card[i].classList.add('active');
  70. }
  71. }
  72. }
  73. function openavatarModel() {
  74. console.log(this.dataset.img);
  75. modalImg.setAttribute("src", `static/img/${this.dataset.img}.webp`);
  76. modalTitle.textContent = `${this.dataset.avatar}`;
  77. avatarModal.show();
  78. }
  79. $('input[type=file]').on('change', prepareUpload);
  80. function prepareUpload(event) {
  81. files = event.target.files;
  82. var data = new FormData();
  83. //data.append('file', $('.img_up1').prop('files')[0]);
  84. data.append('file', files[0]);
  85. // append other variables to data if you want: data.append('field_name_x', field_value_x);
  86. $(this).next().text('');
  87. $(this).next().html('<img src="static/img/Spinner-1s-181px.gif">');
  88. $.ajax({
  89. type: 'POST',
  90. processData: false, // important
  91. contentType: false, // important
  92. data: data,
  93. url: 'https://www.choozmo.com:8887/uploadfile',
  94. dataType: 'json',
  95. success: function (jsonData) {
  96. event.target.previousSibling.value = jsonData.msg;
  97. $(this).prev().val(jsonData.msg);
  98. event.target.nextSibling.innerHTML = '';
  99. event.target.nextSibling.textContent = '上傳檔案';
  100. //console.log($(this).next());
  101. //$(this).next().html('上傳檔案');
  102. //$(this).next().text('上傳檔案');
  103. },
  104. error: function (error) {
  105. event.target.nextSibling.innerHTML = '';
  106. event.target.nextSibling.textContent = '上傳檔案';
  107. alert('圖片錯誤');
  108. }
  109. });
  110. }
  111. const button = document.querySelector('.next');
  112. $(".next").click(function () {
  113. button.setAttribute('disabled', '');
  114. setTimeout(function () {
  115. button.removeAttribute('disabled')
  116. }, 10000);
  117. avatar = $('.avatar').val();
  118. name_title = $('.title_new').val();
  119. txtARR = [];
  120. imgARR = [];
  121. var step;
  122. let contentIdx = document.querySelectorAll(".txtsrc").length;
  123. for (let i = 1; i < (contentIdx + 1); i++) {
  124. if ($(`.txtsrc${i}`).val() != "") {
  125. txtARR.push($(`.txtsrc${i}`).val())
  126. }
  127. }
  128. let imgIdx = document.querySelectorAll(".imgsrc").length;
  129. for (let i = 1; i < (imgIdx + 1); i++) {
  130. if ($(`.imgsrc${i}`).val() != "") {
  131. imgARR.push($(`.imgsrc${i}`).val())
  132. }
  133. }
  134. multiLang = 0;
  135. if ($('#multiLang').prop("checked")) {multiLang = 1;}
  136. dataOBJ = { "name": name_title, "text_content": txtARR, "image_urls": imgARR, "avatar": avatar,"multiLang":multiLang, "client_id": client_id }
  137. objstr = JSON.stringify(dataOBJ);
  138. console.log(dataOBJ)
  139. jwt_token = get_jwt_token()
  140. var xhr = new XMLHttpRequest();
  141. xhr.open("POST", "https://www.choozmo.com:8887/make_anchor_video");
  142. xhr.setRequestHeader("accept", "application/json");
  143. xhr.setRequestHeader("Authorization","Bearer "+jwt_token)
  144. xhr.setRequestHeader("Content-Type", "application/json");
  145. xhr.onreadystatechange = function () {
  146. if (xhr.readyState === 4) {
  147. responseOBJ = JSON.parse(xhr.responseText)
  148. console.log(responseOBJ.msg);
  149. //let title = "資料已送出";
  150. let text;
  151. if(lang == 'en') {
  152. //title = 'Submitted Successfully!';
  153. text = responseOBJ.msg.eng;
  154. } else {
  155. text = responseOBJ.msg.zh;
  156. }
  157. Swal.fire({
  158. // title: title,
  159. icon: 'info',
  160. text: text,
  161. confirmButtonColor: '#3085d6',
  162. });
  163. }
  164. };
  165. var data = renderXHR_data(dataOBJ)
  166. console.log(data)
  167. result = xhr.send(objstr);
  168. });
  169. const buttonSend = document.querySelector('#sendBTN');
  170. $("#sendBTN").click(function () {
  171. buttonSend.setAttribute('disabled', '');
  172. setTimeout(function () {
  173. buttonSend.removeAttribute('disabled')
  174. }, 4000);
  175. avatar = $('.avatar').val();
  176. name_title = $('.title_new').val();
  177. txtARR = [];
  178. imgARR = [];
  179. var step;
  180. let contentIdx = document.querySelectorAll(".txtsrc").length;
  181. for (let i = 1; i < (contentIdx + 1); i++) {
  182. if ($(`.txtsrc${i}`).val() != "") {
  183. txtARR.push($(`.txtsrc${i}`).val())
  184. }
  185. }
  186. let imgIdx = document.querySelectorAll(".imgsrc").length;
  187. for (let i = 1; i < (imgIdx + 1); i++) {
  188. if ($(`.imgsrc${i}`).val() != "") {
  189. imgARR.push($(`.imgsrc${i}`).val())
  190. }
  191. }
  192. multiLang = 0
  193. if ($('#multiLang').prop("checked")) {multiLang = 1;}
  194. dataOBJ = { "name": name_title, "text_content": txtARR, "image_urls": imgARR, "avatar": avatar,"multiLang":multiLang, "client_id": client_id }
  195. objstr = JSON.stringify(dataOBJ);
  196. console.log(dataOBJ)
  197. jwt_token = get_jwt_token()
  198. var xhr = new XMLHttpRequest();
  199. xhr.open("POST", "https://www.choozmo.com:8887/make_anchor_video_long");
  200. xhr.setRequestHeader("accept", "application/json");
  201. xhr.setRequestHeader("Authorization","Bearer "+jwt_token)
  202. xhr.setRequestHeader("Content-Type", "application/json");
  203. xhr.onreadystatechange = function () {
  204. if (xhr.readyState === 4) {
  205. responseOBJ = JSON.parse(xhr.responseText)
  206. if (responseOBJ.msg=='ok'){
  207. let title = "資料已送出";
  208. let text = '資料已傳送,請耐心等候';
  209. if (lang == 'en') { // 英文版訊息
  210. title = "Submitted Successfully!";
  211. text = 'We are working on your video. You will get notfication in line group when your video is done.';
  212. }
  213. Swal.fire({
  214. title: title,
  215. icon: 'success',
  216. text: text,
  217. confirmButtonColor: '#3085d6',
  218. });
  219. } else {
  220. let title = '發生錯誤';
  221. let text;
  222. if(lang == 'en') {
  223. title = 'Error';
  224. text = responseOBJ.msg.eng;
  225. } else {
  226. text = responseOBJ.msg.zh;
  227. }
  228. Swal.fire({
  229. title: title,
  230. icon: 'error',
  231. text: text,
  232. confirmButtonColor: '#3085d6',
  233. });
  234. }
  235. }
  236. };
  237. var data = renderXHR_data(dataOBJ)
  238. console.log(data)
  239. result = xhr.send(objstr);
  240. });
  241. const slide_button = document.querySelector('#send_slide');
  242. $("#send_slide").click(function () {
  243. slide_button.setAttribute('disabled', '');
  244. setTimeout(function () {
  245. slide_button.removeAttribute('disabled')
  246. }, 4000);
  247. avatar = $('.avatar').val();
  248. var step;
  249. multiLang = 0
  250. if ($('#multiLang').prop("checked")) {multiLang = 1;}
  251. dataOBJ = {'slide_url':$('#slide_raw_url').val(),"avatar": avatar,"multiLang":multiLang, "client_id": client_id }
  252. objstr = JSON.stringify(dataOBJ);
  253. jwt_token = get_jwt_token()
  254. var xhr = new XMLHttpRequest();
  255. xhr.open("POST", "https://www.choozmo.com:8887/make_anchor_video_gSlide");
  256. xhr.setRequestHeader("accept", "application/json");
  257. xhr.setRequestHeader("Authorization","Bearer "+jwt_token);
  258. xhr.setRequestHeader("Content-Type", "application/json");
  259. xhr.onreadystatechange = function () {
  260. if (xhr.readyState === 4) {
  261. let title = "資料已送出";
  262. let text = '資料已傳送,請耐心等候';
  263. if (lang == 'en') { // 英文版訊息
  264. title = "Submitted Successfully!";
  265. text = 'We are working on your video. You will get notfication in line group when your video is done.';
  266. }
  267. Swal.fire({
  268. title: title,
  269. icon: 'success',
  270. text: text,
  271. confirmButtonColor: '#3085d6',
  272. });
  273. }
  274. };
  275. var data = renderXHR_data(dataOBJ)
  276. console.log(data)
  277. result = xhr.send(objstr);
  278. });
  279. $(".gen_avatar").click(function () {
  280. dataOBJ = { "imgurl": $('.img_src').val() }
  281. objstr = JSON.stringify(dataOBJ);
  282. console.log(dataOBJ)
  283. //alert('資料已送出! 請耐心等候')
  284. $.ajax({
  285. url: 'https://www.choozmo.com:8887/swapFace',
  286. dataType: 'json', // 預期從server接收的資料型態
  287. contentType: 'application/json; charset=utf-8', // 要送到server的資料型態
  288. type: 'post',
  289. data: objstr,
  290. success: function (suc_data) {
  291. alert(suc_data.msg)
  292. },
  293. //data:JSON.stringify({n1:"12",n2:"22"}),
  294. error: function (error) {
  295. console.error(error)
  296. }
  297. });
  298. });
  299. var loaded_data = ''
  300. function openNav() {
  301. document.getElementById("mySidenav").style.width = "250px";
  302. document.querySelector('.loader').style.display = "block";
  303. let jwt_token = get_jwt_token();
  304. axios({
  305. method: 'post',
  306. url: 'https://www.choozmo.com:8887/history_input',
  307. headers: {
  308. 'accept': 'application/json',
  309. 'Authorization': `Bearer ${jwt_token}`
  310. }
  311. }).then(res => {
  312. console.log(res.data);
  313. loaded_data = res.data;
  314. for (var obj of loaded_data) {
  315. var historyList = document.querySelector('.historyList');
  316. var list = document.createElement('li');
  317. list.id = obj.id;
  318. // div-imgfr
  319. var divImgfr = document.createElement('div');
  320. divImgfr.classList.add('item_imgfr');
  321. var img = document.createElement('img');
  322. img.setAttribute('src', obj['image_urls'][0]);
  323. divImgfr.appendChild(img);
  324. // div-content
  325. var contentBox = document.createElement('div');
  326. contentBox.classList.add('content-box');
  327. var boxTitle = document.createElement('p');
  328. boxTitle.classList.add('box-title');
  329. boxTitle.textContent = obj.name;
  330. boxTitle.id = obj.id;
  331. console.log(loaded_data);
  332. boxTitle.setAttribute('onclick', `direct(${obj.id})`);
  333. var boxLink = document.createElement('span');
  334. boxLink.classList.add('box-link');
  335. boxLink.setAttribute("data-url", obj.link);
  336. boxLink.setAttribute('onclick', 'view()');
  337. boxLink.innerHTML = '<i class="fas fa-play-circle me-1"></i>觀看影片';
  338. contentBox.appendChild(boxTitle);
  339. contentBox.appendChild(boxLink);
  340. list.classList.add("historyList-item");
  341. list.setAttribute('onclick', `direct(${obj.id})`);
  342. list.appendChild(divImgfr);
  343. list.appendChild(contentBox);
  344. historyList.appendChild(list);
  345. }
  346. document.querySelector('.loader').style.display = "none";
  347. }).catch(err => {
  348. console.log(err);
  349. });
  350. }
  351. function closeNav() {
  352. document.getElementById("mySidenav").style.width = "250px";
  353. }
  354. function view() {
  355. event.stopPropagation();
  356. console.log(event.target);
  357. if (event.target.nodeName === 'I') {
  358. return;
  359. } else {
  360. window.open(`http://${event.target.dataset.url}`, '_blank');
  361. }
  362. }
  363. function renderXHR_data(jsonObj) {
  364. XHRstring = ''
  365. for (const [key, value] of Object.entries(jsonObj)) {
  366. console.log(value)
  367. if (typeof (value) == "object") {
  368. XHRstring += (key+'=['+value.join(',')+']&')
  369. }
  370. else {
  371. XHRstring += (key + '=' + value + '&')
  372. }
  373. }
  374. XHRstring = XHRstring.substring(0, XHRstring.length - 1);
  375. return XHRstring
  376. }
  377. function get_jwt_token(){
  378. jwt_raw = document.cookie.split(';').filter(s=>s.includes('jwt_token'))[0];
  379. if(!jwt_raw) {return}
  380. return jwt_raw.split('=')[1];
  381. }
  382. function direct(id) {
  383. location.href = `make_video.html?id=${id}`;
  384. }
  385. function load_data(tid, loaded_data, draft = false) {
  386. if(!tid) {
  387. return;
  388. }
  389. var title = document.getElementById("title");
  390. var linker = document.getElementById("linker");
  391. myModal.hide();
  392. if(!draft) {
  393. linker.setAttribute('href', `http://${loaded_data.find(item => item.id == tid).link}`)
  394. linker.setAttribute('target', '_blank')
  395. $("#linker").html(`http://${loaded_data.find(item => item.id == tid).link}`)
  396. $("#linker").show();
  397. $(".linker__box").show();
  398. }
  399. console.log(loaded_data);
  400. let historyItem = loaded_data.filter(item => item.id == tid)[0];
  401. console.log(historyItem.title);
  402. if(draft) {
  403. $(".title_new").val(historyItem.title);
  404. $("#avatar").val(historyItem.avatar);
  405. } else {
  406. $(".title_new").val(loaded_data.find(item => item.id == tid).name);
  407. $("#avatar").val(historyItem.avatar);
  408. }
  409. let txtlength = historyItem.text_content.length;
  410. let imglength = historyItem.image_urls.length;
  411. subtitleInputs.innerHTML = '';
  412. imgInputs.innerHTML = '';
  413. for (let i = 0; i < txtlength; i++) {
  414. var txtinput = document.createElement("input");
  415. txtinput.setAttribute('type', 'text');
  416. txtinput.setAttribute('name', `t${i + 1}`);
  417. txtinput.value = historyItem.text_content[i];
  418. txtinput.setAttribute('placeholder', `${i + 1}`);
  419. txtinput.classList.add('txtsrc', `txtsrc${i + 1}`)
  420. subtitleInputs.appendChild(txtinput);
  421. }
  422. for (let i = 0; i < imglength; i++) {
  423. var imginput = document.createElement("input");
  424. imginput.setAttribute('type', 'text');
  425. imginput.setAttribute('name', `m${i + 1}`);
  426. imginput.classList.add('imgsrc', `imgsrc${i + 1}`);
  427. imginput.value = historyItem.image_urls[i];
  428. imginput.setAttribute('placeholder', `${i + 1}`);
  429. imgInputs.appendChild(imginput);
  430. var imgupload = document.createElement("input");
  431. imgupload.setAttribute('id', `img${i + 1}`);
  432. imgupload.setAttribute('type', `file`);
  433. imgupload.classList.add('img_uploader', 'img_up');
  434. imgInputs.appendChild(imgupload);
  435. var imguploadlabel = document.createElement("label");
  436. imguploadlabel.setAttribute('for', `img${i + 1}`);
  437. imguploadlabel.classList.add('upload-btn');
  438. imguploadlabel.textContent = '上傳檔案';
  439. if(lang == 'en') {
  440. imguploadlabel.textContent = 'Upload';
  441. }
  442. imguploadlabel.setAttribute('set-lan', `html:upload_file`);
  443. imgInputs.appendChild(imguploadlabel);
  444. }
  445. setTimeout(() => {
  446. JsLoadingOverlay.hide();
  447. }, 0);
  448. }
  449. getpathId();
  450. function getMode() {
  451. let id = window.location.search.split('?').pop();
  452. let mode = id.split('=')[0];
  453. if(mode == 'draftid') {
  454. /* JsLoadingOverlay.show({
  455. "overlayBackgroundColor": "#FFFFFF",
  456. "overlayOpacity": "0.9",
  457. "spinnerIcon": "ball-circus",
  458. "spinnerColor": "#B9DDF3",
  459. "spinnerSize": "2x",
  460. "overlayIDName": "overlay",
  461. "spinnerIDName": "spinner",
  462. "offsetX": 0,
  463. "offsetY": 0,
  464. "containerID": null,
  465. "lockScroll": false,
  466. "overlayZIndex": 9998,
  467. "spinnerZIndex": 9999
  468. }); */
  469. getDraftData();
  470. } else if(mode == 'id') {
  471. /* JsLoadingOverlay.show({
  472. "overlayBackgroundColor": "#FFFFFF",
  473. "overlayOpacity": "0.9",
  474. "spinnerIcon": "ball-circus",
  475. "spinnerColor": "#B9DDF3",
  476. "spinnerSize": "2x",
  477. "overlayIDName": "overlay",
  478. "spinnerIDName": "spinner",
  479. "offsetX": 0,
  480. "offsetY": 0,
  481. "containerID": null,
  482. "lockScroll": false,
  483. "overlayZIndex": 9998,
  484. "spinnerZIndex": 9999
  485. }); */
  486. getData();
  487. }
  488. return id.split('=')[1];
  489. //load_data(id);
  490. }
  491. getMode();
  492. function getpathId(){
  493. let id = window.location.search.split('?').pop();
  494. return id.split('=')[1];
  495. }
  496. function getData() {
  497. let jwt_token = get_jwt_token();
  498. axios({
  499. method: 'post',
  500. url: 'https://www.choozmo.com:8887/history_input',
  501. headers: {
  502. 'accept': 'application/json',
  503. 'Authorization': `Bearer ${jwt_token}`
  504. }
  505. }).then(res => {
  506. loaded_data = res.data;
  507. console.log(loaded_data);
  508. const id = getpathId();
  509. load_data(id, loaded_data);
  510. }).catch(err => {
  511. console.log(err);
  512. });
  513. }
  514. var subtitleInputs = document.querySelector(".subtitle-inputs");
  515. var imgInputs = document.querySelector(".img-inputs");
  516. let length = 5;
  517. function initial() {
  518. for (let i = 0; i < length; i++) {
  519. rendertxtBlock(i + 1);
  520. renderimgBlock(i + 1);
  521. }
  522. console.log(document.querySelectorAll(".txtsrc").length + 1);
  523. }
  524. initial();
  525. var addbtn = document.querySelector(".add");
  526. var addimgbtn = document.querySelector(".addimg");
  527. addbtn.addEventListener('click', addtxtBlock);
  528. addimgbtn.addEventListener('click', addimgBlock);
  529. function addtxtBlock() {
  530. let newIdx = document.querySelectorAll(".txtsrc").length + 1;
  531. rendertxtBlock(newIdx);
  532. }
  533. function addimgBlock() {
  534. let newimgIdx = document.querySelectorAll(".imgsrc").length + 1;
  535. renderimgBlock(newimgIdx);
  536. }
  537. function rendertxtBlock(i) {
  538. var txtinput = document.createElement("input");
  539. txtinput.setAttribute('type', 'text');
  540. txtinput.setAttribute('name', `t${i}`);
  541. txtinput.value = "";
  542. txtinput.setAttribute('placeholder', `${i}`);
  543. txtinput.classList.add('txtsrc', `txtsrc${i}`)
  544. subtitleInputs.appendChild(txtinput);
  545. }
  546. function renderimgBlock(i) {
  547. var imginput = document.createElement("input");
  548. imginput.setAttribute('type', 'text');
  549. imginput.setAttribute('name', `m${i}`);
  550. imginput.classList.add('imgsrc', `imgsrc${i}`);
  551. imginput.value = "";
  552. imginput.setAttribute('placeholder', `${i}`);
  553. imgInputs.appendChild(imginput);
  554. var imgupload = document.createElement("input");
  555. imgupload.setAttribute('id', `img${i}`);
  556. imgupload.setAttribute('type', `file`);
  557. imgupload.classList.add('img_uploader', 'img_up');
  558. imgInputs.appendChild(imgupload);
  559. var imguploadlabel = document.createElement("label");
  560. imguploadlabel.setAttribute('for', `img${i}`);
  561. imguploadlabel.classList.add('upload-btn');
  562. imguploadlabel.textContent = '上傳檔案';
  563. if(lang == 'en') {
  564. imguploadlabel.textContent = 'Upload';
  565. }
  566. imguploadlabel.setAttribute('set-lan', `html:upload_file`);
  567. imgInputs.appendChild(imguploadlabel);
  568. $('input[type=file]').on('change', prepareUpload);
  569. }
  570. $('.owl-carousel').owlCarousel({
  571. loop: true,
  572. margin: 10,
  573. nav: false,
  574. mouseDrag: true,
  575. touchDrag: true,
  576. smartSpeed: 1000,
  577. autoplay: true,
  578. autoplayTimeout: 8000,
  579. autoplayHoverPause: false,
  580. responsive: {
  581. 0: {
  582. items: 1
  583. },
  584. 600: {
  585. items: 2
  586. },
  587. 1000: {
  588. items: 4
  589. }
  590. }
  591. });
  592. $('.draft-btn').click(() => {
  593. let text = '儲存中...';
  594. if (lang == 'en') {
  595. text = 'Saving...';
  596. }
  597. $('.draft-btn').text(text);
  598. let jwt_token = get_jwt_token();
  599. let avatar = $('.avatar').val();
  600. let name_title = $('.title_new').val();
  601. let txtARR = [];
  602. let imgARR = [];
  603. var step;
  604. let contentIdx = document.querySelectorAll(".txtsrc").length;
  605. for (let i = 1; i < (contentIdx + 1); i++) {
  606. if ($(`.txtsrc${i}`).val() != "") {
  607. txtARR.push($(`.txtsrc${i}`).val())
  608. }
  609. }
  610. let imgIdx = document.querySelectorAll(".imgsrc").length;
  611. for (let i = 1; i < (imgIdx + 1); i++) {
  612. if ($(`.imgsrc${i}`).val() != "") {
  613. imgARR.push($(`.imgsrc${i}`).val())
  614. }
  615. }
  616. let multiLang = 0
  617. if ($('#multiLang').prop("checked")) {multiLang = 1;}
  618. let id = window.location.search.split('?').pop();
  619. let mode = id.split('=')[0];
  620. if(mode == 'draftid') {
  621. id = Number(id.split('=')[1]);
  622. } else {
  623. id = -1;
  624. }
  625. let dataOBJ = { "id": id, "title": name_title, "text_content": txtARR, "image_urls": imgARR, "avatar": avatar,"multiLang":multiLang }
  626. console.log(dataOBJ);
  627. axios({
  628. method: 'post',
  629. url: 'https://www.choozmo.com:8887/save_draft',
  630. headers: {
  631. 'accept': 'application/json',
  632. 'Authorization': `Bearer ${jwt_token}`,
  633. 'Content-Type': 'application/json',
  634. },
  635. data: dataOBJ
  636. }).then(res => {
  637. console.log(res.data);
  638. $('.draft-btn').text('存為草稿');
  639. let title = "儲存完成";
  640. if(lang == 'en') {
  641. title = 'Saved!'
  642. $('.draft-btn').text('Save as Draft');
  643. }
  644. Swal.fire({
  645. title: title,
  646. icon: 'success',
  647. confirmButtonColor: '#3085d6',
  648. });
  649. }).catch(err => {
  650. console.log(err);
  651. });
  652. });
  653. function getDraftData() {
  654. let token = get_jwt_token();
  655. axios({
  656. method: 'post',
  657. url: 'https://www.choozmo.com:8887/draft_list',
  658. headers: {
  659. 'accept': 'application/json',
  660. 'Authorization': `Bearer ${token}`
  661. }
  662. }).then(res => {
  663. console.log(res.data);
  664. let result = [...res.data];
  665. let id = getpathId();
  666. load_data(id, result, true);
  667. }).catch(err => {
  668. console.log(err);
  669. });
  670. }
  671. $(".copy").click(function(){
  672. const copyStr = $(this).next().text();
  673. copyToClipboard(copyStr);
  674. });
  675. const copyToClipboard = str => {
  676. const el = document.createElement('textarea');
  677. el.value = str;
  678. el.setAttribute('readonly', '');
  679. el.style.position = 'absolute';
  680. el.style.left = '-9999px';
  681. document.body.appendChild(el);
  682. el.select();
  683. document.execCommand('copy');
  684. document.body.removeChild(el);
  685. Swal.fire({
  686. toast: true,
  687. icon: 'success',
  688. position: 'top-end',
  689. title: 'Link copied!',
  690. timerProgressBar: true,
  691. showConfirmButton: false,
  692. timer: 1000,
  693. backdrop: false
  694. })
  695. };