script_util.js 22 KB

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