script_long.js 18 KB

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