script_util2.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // Check Route
  2. checkRoute();
  3. // let card;
  4. $('.loading').hide();
  5. let userBasics = JSON.parse(localStorage.getItem('user_profile')) || [];
  6. console.log(userBasics);
  7. if(userBasics.length >0 && typeof(userBasics) !== 'string'){
  8. $('.userName').html(`<h2 class="user-name text-white mt-lg-3 mt-xl-1 fw-bold">Hi ${userBasics.user_info.userName}</h2>`);
  9. }
  10. function checkRoute() {
  11. let jwt_token = get_jwt_token();
  12. // if(jwt_token == undefined) {
  13. // window.location.replace("login.html");
  14. // }
  15. axios({
  16. method: 'post',
  17. url: 'https://www.choozmo.com:8887/user_profile',
  18. headers: {
  19. 'accept': 'text/html',
  20. 'Authorization': `Bearer ${jwt_token}`
  21. }
  22. }).then(res => {
  23. console.log(res.data);
  24. if(res.status !== 200) {
  25. window.location.replace("login.html");
  26. }
  27. var userName='';
  28. userName+=`<h2 class="user-name text-white mt-lg-3 mt-xl-1 fw-bold">Hi ${res.data.user_info.userName}</h2>`;
  29. $('.userName').html(userName);
  30. }).catch(err => {
  31. console.log(err);
  32. window.location.replace("login.html");
  33. });
  34. }
  35. // Nav btn control
  36. const btnLoginPage = document.querySelector('.btn-login');
  37. const btnUserProfile = document.querySelector('.btn-userProfile');
  38. const btnLogout = document.querySelector('.btn-logout');
  39. function loginControl() {
  40. btnLoginPage.style.display = 'none';
  41. btnLogout.style.display = 'block';
  42. btnUserProfile.style.display = 'block';
  43. }
  44. loginControl();
  45. // Check lan
  46. let lan = localStorage.getItem('lan');
  47. $('.dropdown-toggle').click(() => {
  48. lan = localStorage.getItem('lan');
  49. checkLan();
  50. });
  51. checkLan();
  52. function checkLan() {
  53. if(lan == 'en') {
  54. $('.btn-makev').css('display', 'none');
  55. $('.btn-makelong').css('display', 'none');
  56. $('.title_new').attr('placeholder', 'Title')
  57. } else {
  58. $('.btn-makev').css('display', 'block');
  59. $('.btn-makelong').css('display', 'block');
  60. }
  61. }
  62. // Get avatars
  63. function getAvatar() {
  64. let jwt_token = get_jwt_token();
  65. /* if(jwt_token == undefined) {
  66. window.location.replace("login.html");
  67. } */
  68. axios({
  69. method: 'post',
  70. url: 'https://www.choozmo.com:8887/get_avatar_by_role',
  71. headers: {
  72. 'accept': 'text/html',
  73. 'Authorization': `Bearer ${jwt_token}`
  74. }
  75. }).then(res => {
  76. console.log(res.data);
  77. let result = [...res.data];
  78. let str = '';
  79. let carStr = '';
  80. for(let i = 0; i < result.length; i++) {
  81. str += `<option value="${result[i].num}">${result[i].name}</option>`
  82. }
  83. for(let i = 0; i < result.length; i++) {
  84. carStr += `<div class="card item col" data-avatar="${result[i].name}" data-img="${result[i].name.toLowerCase()}">
  85. <div class="imgfr"><img src="static/img/${result[i].name.toLowerCase()}.webp" class="card-img-top" alt="..."></div>
  86. <div class="card-body">
  87. <h5 class="card-title">${result[i].name.toLowerCase()}</h5>
  88. </div>
  89. </div>`;
  90. }
  91. $('#avatar').html(`<option set-lan="html:p_choose_character" value="請選擇人物" selected="selected" disabled>>請選擇人物</option>${str}`);
  92. $('.avatar-cards').html(carStr);
  93. //card = document.getElementsByClassName('card');
  94. console.log(card);
  95. //addCardListener(card);
  96. //avatarChange();
  97. }).catch(err => {
  98. console.log(err);
  99. });
  100. }
  101. // getAvatar();
  102. // Card
  103. const card = document.querySelectorAll('.avatar-cards .card');
  104. var client_id = Date.now()
  105. var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
  106. var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
  107. return new bootstrap.Tooltip(tooltipTriggerEl)
  108. });
  109. var myModal = new bootstrap.Modal(document.getElementById('history'), {
  110. keyboard: false
  111. })
  112. var avatarModal = new bootstrap.Modal(document.getElementById('avatarmega'), {
  113. keyboard: false
  114. })
  115. var draftsModal = new bootstrap.Modal(document.getElementById('draftModal'), {
  116. keyboard: false
  117. })
  118. var modalImg = document.querySelector("#avatarmega .modal-img");
  119. var modalTitle = document.querySelector("#avatarmega .modal-title");
  120. var avatarSelector = document.getElementById("avatar");
  121. // avatarSelector.addEventListener('change', avatarChange);
  122. // function addCardListener() {
  123. // for (let i = 0; i < card.length; i++) {
  124. // card[i].addEventListener('click', openavatarModel);
  125. // }
  126. // }
  127. // addCardListener()
  128. // avatarChange()
  129. function avatarChange() {
  130. var value = avatarSelector.options[avatarSelector.selectedIndex].text;
  131. for (let i = 0; i < card.length; i++) {
  132. card[i].classList.remove('active');
  133. if (card[i].dataset.avatar == value) {
  134. card[i].classList.add('active');
  135. }
  136. }
  137. }
  138. $('input[name=AvatarVal').on('change', function () {
  139. $('input[name=AvatarVal]').not(this).prop('checked', false);
  140. $('input[name=AvatarVal]').not(this).parent().removeClass('active');
  141. });
  142. $(document).on("click", ".avatar-checkbox", function (event) {
  143. $(this).parent().toggleClass('active');
  144. });
  145. // function openavatarModel() {
  146. // console.log(this.dataset.img);
  147. // modalImg.setAttribute("src", `static/img/${this.dataset.img}.webp`);
  148. // modalTitle.textContent = `${this.dataset.avatar}`;
  149. // avatarModal.show();
  150. // }
  151. // Drafts
  152. function getDraft() {
  153. JsLoadingOverlay.show({
  154. "overlayBackgroundColor": "#FFFFFF",
  155. "overlayOpacity": "1",
  156. "spinnerIcon": "ball-circus",
  157. "spinnerColor": "#B9DDF3",
  158. "spinnerSize": "1x",
  159. "overlayIDName": "overlay",
  160. "spinnerIDName": "spinner",
  161. "offsetX": 0,
  162. "offsetY": 0,
  163. "containerID": "draft-table",
  164. "lockScroll": false,
  165. "overlayZIndex": 9998,
  166. "spinnerZIndex": 9999
  167. });
  168. let token = get_jwt_token();
  169. axios({
  170. method: 'post',
  171. url: 'https://www.choozmo.com:8887/draft_list',
  172. headers: {
  173. 'accept': 'application/json',
  174. 'Authorization': `Bearer ${token}`
  175. }
  176. }).then(res => {
  177. console.log(res.data);
  178. let result = [...res.data];
  179. let str = '';
  180. let draftStr = '';
  181. let nodraftTxt = '目前沒有草稿喔';
  182. if(lan == 'en') {
  183. titleTxt = 'Video Title';
  184. editTxt = 'Edit';
  185. nodraftTxt = 'no_draft';
  186. }
  187. if(result.length > 0){
  188. for(let i = 0;i < result.length; i++) {
  189. draftStr += `<tr>
  190. <td>${result[i].title}</td>
  191. <td>
  192. <span class="me-md-3 draft-content-icon" id="${result[i].id}" onclick="gotoDraft(${result[i].id})">
  193. <i class="fas fa-edit"></i>
  194. </span>
  195. <span class="ms-md-3 draft-content-icon draft-content-delete" id="${result[i].id}" onclick="deleteDraft(${result[i].id})">
  196. <i class="fas fa-trash-alt"></i>
  197. </span>
  198. </td>
  199. </tr>`
  200. }
  201. let titleTxt = '標題';
  202. let editTxt = '編輯';
  203. str = `<table class="table text-center">
  204. <thead>
  205. <tr>
  206. <th scope="col" set-lan="html:video_title">${titleTxt}</th>
  207. <th class="px-0" scope="col" set-lan="html:draft_edit">${editTxt}</th>
  208. </tr>
  209. </thead>
  210. <tbody class="draft-content">${draftStr}</tbody>
  211. </table>`
  212. $('.draft-table .card').html(str);
  213. JsLoadingOverlay.hide();
  214. } else {
  215. str = `<div>
  216. <h5 set-lan="html:no_draft">${nodraftTxt}</h5>
  217. <img src="static/img/undraw_void_3ggu.svg" width="80">
  218. </div>`;
  219. $('.draft-table .card').html(str);
  220. JsLoadingOverlay.hide();
  221. }
  222. }).catch(err => {
  223. console.log(err);
  224. });
  225. }
  226. getDraft();
  227. function openDrafts() {
  228. draftsModal.show()
  229. document.querySelector('#draftModal .loader').style.display = "block";
  230. $('#draftModal #draft-table').show();
  231. getDraft();
  232. document.querySelector('#draftModal .loader').style.display = "none";
  233. }
  234. function gotoDraft (id) {
  235. location.href = `make_video2.html?draftid=${id}`;
  236. }
  237. function deleteDraft(id) {
  238. let token = get_jwt_token();
  239. JsLoadingOverlay.show({
  240. "overlayBackgroundColor": "#FFFFFF",
  241. "overlayOpacity": "1",
  242. "spinnerIcon": "ball-circus",
  243. "spinnerColor": "#B9DDF3",
  244. "spinnerSize": "1x",
  245. "overlayIDName": "overlay",
  246. "spinnerIDName": "spinner",
  247. "offsetX": 0,
  248. "offsetY": 0,
  249. "containerID": "draft-table",
  250. "lockScroll": false,
  251. "overlayZIndex": 9998,
  252. "spinnerZIndex": 9999
  253. });
  254. axios({
  255. method: 'post',
  256. url: 'https://www.choozmo.com:8887/del_draft',
  257. headers: {
  258. 'accept': 'application/json',
  259. 'Authorization': `Bearer ${token}`,
  260. 'Content-Type': 'application/json'
  261. },
  262. data: { "id": id }
  263. }).then(res => {
  264. console.log(res.data);
  265. //$('.draft-content-delete').html('<i class="fas fa-trash-alt"></i>');
  266. JsLoadingOverlay.hide();
  267. getDraft();
  268. draftsModal.show();
  269. avatarModal.hide();
  270. }).catch(err => {
  271. console.log(err);
  272. });
  273. }
  274. // Upload
  275. $('input[type=file]').on('change', prepareUpload);
  276. $('.upload-btn').change(prepareUpload);
  277. function prepareUpload(event) {
  278. files = event.target.files;
  279. $(this).prev().val('');
  280. var data = new FormData();
  281. //data.append('file', $('.img_up1').prop('files')[0]);
  282. data.append('file', files[0]);
  283. // append other variables to data if you want: data.append('field_name_x', field_value_x);
  284. $(this).next().text('');
  285. $(this).next().html('<img src="static/img/Spinner-1s-181px.gif">');
  286. $.ajax({
  287. type: 'POST',
  288. processData: false, // important
  289. contentType: false, // important
  290. data: data,
  291. url: 'https://www.choozmo.com:8887/uploadfile',
  292. dataType: 'json',
  293. success: function (jsonData) {
  294. event.target.previousSibling.value = jsonData.msg;
  295. console.log(jsonData.msg);
  296. $(this).prev().val(jsonData.msg);
  297. if(jsonData.msg.zh) {
  298. $(this).prev().val(jsonData.msg.zh);
  299. event.target.previousSibling.value = jsonData.msg.zh;
  300. }
  301. const nextEl = event.target.nextSibling;
  302. nextEl.innerHTML = '';
  303. // nextEl.textContent = '上傳檔案';
  304. nextEl.style.backgroundImage = `url(http://${jsonData.msg})`;
  305. //console.log($(this).next());
  306. //$(this).next().html('上傳檔案');
  307. //$(this).next().text('上傳檔案');
  308. $(this).html('');
  309. },
  310. error: function (error) {
  311. const nextEl = event.target.nextSibling;
  312. nextEl.style.backgroundImage = `url(./img/upload_img.png)`;
  313. $(this).html('');
  314. alert('圖片錯誤');
  315. }
  316. });
  317. }
  318. const button = document.querySelector('.next');
  319. // Submit
  320. $(".next").click(function () {
  321. button.setAttribute('disabled', '');
  322. setTimeout(function () {
  323. button.removeAttribute('disabled')
  324. }, 15000);
  325. $('.loading').show();
  326. var Avatar_value
  327. var Avatar_value2
  328. $('input[name="AvatarVal"]:checked').each(function(){//遍歷每一個名字為interest的核取方塊,其中選中的執行函式
  329. Avatar_value=$(this).val();//將選中的值新增到陣列chk_value中
  330. });
  331. Avatar_value2=parseInt(Avatar_value);
  332. var avatar= Avatar_value2
  333. console.log(avatar);
  334. if(isNaN(avatar)){
  335. Swal.fire({
  336. // title: title,
  337. icon: 'error',
  338. text: '請選擇主播',
  339. confirmButtonColor: '#3085d6',
  340. });
  341. }
  342. // avatar = $('.avatar').val();
  343. name_title = $('.title_new').val();
  344. txtARR = [];
  345. imgARR = [];
  346. var step;
  347. let contentIdx = document.querySelectorAll(".txtsrc").length;
  348. for (let i = 1; i < (contentIdx + 1); i++) {
  349. if ($(`.txtsrc`).eq(i-1).val() != "") {
  350. txtARR.push($(`.txtsrc`).eq(i-1).val())
  351. }
  352. }
  353. let imgIdx = document.querySelectorAll(".imgsrc").length;
  354. for (let i = 1; i < (imgIdx + 1); i++) {
  355. if ($(`.imgsrc`).eq(i-1).val() != "") {
  356. imgARR.push($(`.imgsrc`).eq(i-1).val())
  357. }
  358. }
  359. multiLang = 0;
  360. if ($('#multiLang').prop("checked")) {multiLang = 1;}
  361. dataOBJ = { "name": name_title, "text_content": txtARR, "image_urls": imgARR, "avatar": avatar,"multiLang":multiLang, "client_id": client_id }
  362. objstr = JSON.stringify(dataOBJ);
  363. console.log(dataOBJ)
  364. jwt_token = get_jwt_token()
  365. var xhr = new XMLHttpRequest();
  366. xhr.open("POST", "https://www.choozmo.com:8887/make_anchor_video");
  367. xhr.setRequestHeader("accept", "application/json");
  368. xhr.setRequestHeader("Authorization","Bearer "+jwt_token)
  369. xhr.setRequestHeader("Content-Type", "application/json");
  370. xhr.onreadystatechange = function () {
  371. if (xhr.readyState === 4) {
  372. responseOBJ = JSON.parse(xhr.responseText)
  373. console.log(responseOBJ.msg);
  374. $('.loading').hide();
  375. //let title = "資料已送出";
  376. let text;
  377. if(lan == 'en') {
  378. //title = 'Submitted Successfully!';
  379. text = responseOBJ.msg.eng;
  380. } else {
  381. text = responseOBJ.msg.zh;
  382. }
  383. Swal.fire({
  384. // title: title,
  385. icon: 'info',
  386. text: text,
  387. confirmButtonColor: '#3085d6',
  388. });
  389. } else {
  390. $('.loading').hide();
  391. }
  392. };
  393. var data = renderXHR_data(dataOBJ)
  394. console.log(data)
  395. result = xhr.send(objstr);
  396. });
  397. // Gen avatars
  398. $(".gen_avatar").click(function () {
  399. dataOBJ = { "imgurl": $('.img_src').val() }
  400. objstr = JSON.stringify(dataOBJ);
  401. console.log(dataOBJ)
  402. //alert('資料已送出! 請耐心等候')
  403. $.ajax({
  404. url: 'https://www.choozmo.com:8887/swapFace',
  405. dataType: 'json', // 預期從server接收的資料型態
  406. contentType: 'application/json; charset=utf-8', // 要送到server的資料型態
  407. type: 'post',
  408. data: objstr,
  409. success: function (suc_data) {
  410. alert(suc_data.msg)
  411. },
  412. //data:JSON.stringify({n1:"12",n2:"22"}),
  413. error: function (error) {
  414. console.error(error)
  415. }
  416. });
  417. });
  418. var loaded_data = ''
  419. function openNav() {
  420. document.getElementById("mySidenav").style.width = "250px";
  421. document.querySelector('.loader').style.display = "block";
  422. let jwt_token = get_jwt_token();
  423. axios({
  424. method: 'post',
  425. url: 'https://www.choozmo.com:8887/history_input',
  426. headers: {
  427. 'accept': 'application/json',
  428. 'Authorization': `Bearer ${jwt_token}`
  429. }
  430. }).then(res => {
  431. console.log(res.data);
  432. loaded_data = res.data;
  433. var historyList = document.querySelector('.historyList');
  434. historyList.innerHTML = '';
  435. for (var obj of loaded_data) {
  436. var list = document.createElement('li');
  437. list.id = obj.id;
  438. // div-imgfr
  439. var divImgfr = document.createElement('div');
  440. divImgfr.classList.add('item_imgfr');
  441. var img = document.createElement('img');
  442. img.setAttribute('src', obj['image_urls'][0]);
  443. divImgfr.appendChild(img);
  444. // div-content
  445. var contentBox = document.createElement('div');
  446. contentBox.classList.add('content-box');
  447. var boxTitle = document.createElement('p');
  448. boxTitle.classList.add('box-title');
  449. boxTitle.textContent = obj.name;
  450. boxTitle.id = obj.id;
  451. console.log(loaded_data);
  452. boxTitle.setAttribute('onclick', `direct(${obj.id})`);
  453. var boxLink = document.createElement('span');
  454. boxLink.classList.add('box-link');
  455. boxLink.setAttribute("data-url", obj.link);
  456. boxLink.setAttribute('onclick', 'view()');
  457. boxLink.innerHTML = '<i class="fas fa-play-circle me-1"></i>觀看影片';
  458. contentBox.appendChild(boxTitle);
  459. contentBox.appendChild(boxLink);
  460. list.classList.add("historyList-item");
  461. list.setAttribute('onclick', `direct(${obj.id})`);
  462. list.appendChild(divImgfr);
  463. list.appendChild(contentBox);
  464. historyList.appendChild(list);
  465. }
  466. document.querySelector('.loader').style.display = "none";
  467. }).catch(err => {
  468. console.log(err);
  469. });
  470. }
  471. function closeNav() {
  472. document.getElementById("mySidenav").style.width = "250px";
  473. }
  474. function view() {
  475. event.stopPropagation();
  476. console.log(event.target);
  477. if (event.target.nodeName === 'I') {
  478. return;
  479. } else {
  480. window.open(`http://${event.target.dataset.url}`, '_blank');
  481. }
  482. }
  483. function renderXHR_data(jsonObj) {
  484. XHRstring = ''
  485. for (const [key, value] of Object.entries(jsonObj)) {
  486. console.log(value)
  487. if (typeof (value) == "object") {
  488. XHRstring += (key+'=['+value.join(',')+']&')
  489. }
  490. else {
  491. XHRstring += (key + '=' + value + '&')
  492. }
  493. }
  494. XHRstring = XHRstring.substring(0, XHRstring.length - 1);
  495. return XHRstring
  496. }
  497. function get_jwt_token(){
  498. jwt_raw = document.cookie.split(';').filter(s=>s.includes('jwt_token'))[0];
  499. if(!jwt_raw) {return}
  500. return jwt_raw.split('=')[1];
  501. }
  502. function direct(id) {
  503. location.href = `make_video2.html?id=${id}`;
  504. }
  505. function load_data(tid, loaded_data, draft = false) {
  506. if(!tid) {
  507. return;
  508. }
  509. var title = document.getElementById("title");
  510. var linker = document.getElementById("linker");
  511. myModal.hide();
  512. if(!draft) {
  513. linker.setAttribute('href', `http://${loaded_data.find(item => item.id == tid).link}`)
  514. linker.setAttribute('target', '_blank')
  515. $("#linker").html(`http://${loaded_data.find(item => item.id == tid).link}`)
  516. $("#linker").show();
  517. $(".linker__box").show();
  518. }
  519. console.log(loaded_data);
  520. let historyItem = loaded_data.filter(item => item.id == tid)[0];
  521. if(draft) {
  522. $(".title_new").val(historyItem.title);
  523. $("#avatar").val(historyItem.avatar);
  524. } else {
  525. $(".title_new").val(loaded_data.find(item => item.id == tid).name);
  526. $("#avatar").val(historyItem.avatar);
  527. }
  528. let txtlength = historyItem.text_content.length;
  529. let imglength = historyItem.image_urls.length;
  530. let flag = txtlength >= imglength ? txtlength : imglength;
  531. //subtitleInputs.innerHTML = '';
  532. // imgInputs.innerHTML = '';
  533. $('.img-inputTest').html('');
  534. for (let i = 0; i < flag; i++) {
  535. var input_sec = document.createElement('div');
  536. var img_box = document.createElement('div');
  537. input_sec.classList.add('input-sec', `input-sec${i+1}`, 'p-3', 'mb-3', 'text-end');
  538. img_box.classList.add('img-box');
  539. // text input
  540. var txtinput = document.createElement("input");
  541. txtinput.setAttribute('type', 'text');
  542. txtinput.setAttribute('name', `t${i+1}`);
  543. txtinput.classList.add('txtsrc', `txtsrc${i+1}`);
  544. txtinput.value = historyItem.text_content[i];
  545. txtinput.setAttribute('placeholder', `${i+1}`);
  546. input_sec.appendChild(txtinput);
  547. var removeSpan = document.createElement("span");
  548. removeSpan.classList.add('remove-btn');
  549. removeSpan.setAttribute('data-num', `${i}`);
  550. removeSpan.textContent = '刪除';
  551. removeSpan.setAttribute('data-bs-toggle', `modal`);
  552. removeSpan.setAttribute('data-bs-target', `#deleteSecModal`);
  553. input_sec.appendChild(removeSpan);
  554. // img url input
  555. var imginput = document.createElement("input");
  556. imginput.setAttribute('type', 'text');
  557. imginput.setAttribute('name', `m${i+1}`);
  558. imginput.classList.add('imgsrc', `imgsrc${i+1}`);
  559. imginput.value = historyItem.image_urls[i];
  560. if(!historyItem.image_urls[i]) {
  561. imginput.value = "";
  562. }
  563. imginput.setAttribute('placeholder', '輸入圖片網址或上傳圖片');
  564. if(lan == 'en') {
  565. imginput.setAttribute('placeholder', 'Type in image link or upload image');
  566. }
  567. img_box.appendChild(imginput);
  568. // img upload input
  569. var imgupload = document.createElement("input");
  570. imgupload.setAttribute('id', `img${i+1}`);
  571. imgupload.setAttribute('type', `file`);
  572. imgupload.classList.add('img_uploader', 'img_up');
  573. imgupload.style.display = "none";
  574. img_box.appendChild(imgupload);
  575. // img upload label
  576. var imguploadlabel = document.createElement("label");
  577. imguploadlabel.setAttribute('for', `img${i+1}`);
  578. imguploadlabel.classList.add('upload-btn');
  579. // imguploadlabel.textContent = '上傳圖片';
  580. imguploadlabel.setAttribute('set-lan', `html:upload_file`);
  581. if(!historyItem.image_urls[i]) {
  582. imguploadlabel.style.backgroundImage = `url(static/img/upload_img.png)`;
  583. } else {
  584. imguploadlabel.style.backgroundImage = `url(${historyItem.image_urls[i]})`;
  585. }
  586. img_box.appendChild(imguploadlabel);
  587. imgupload.addEventListener('change', prepareUpload);
  588. input_sec.appendChild(img_box);
  589. document.querySelector('.img-inputTest').append(input_sec);
  590. }
  591. addRemoveFunc()
  592. }
  593. getpathId();
  594. function getMode() {
  595. let id = window.location.search.split('?').pop();
  596. let mode = id.split('=')[0];
  597. if(mode == 'draftid') {
  598. /* JsLoadingOverlay.show({
  599. "overlayBackgroundColor": "#FFFFFF",
  600. "overlayOpacity": "0.9",
  601. "spinnerIcon": "ball-circus",
  602. "spinnerColor": "#B9DDF3",
  603. "spinnerSize": "2x",
  604. "overlayIDName": "overlay",
  605. "spinnerIDName": "spinner",
  606. "offsetX": 0,
  607. "offsetY": 0,
  608. "containerID": null,
  609. "lockScroll": false,
  610. "overlayZIndex": 9998,
  611. "spinnerZIndex": 9999
  612. }); */
  613. getDraftData();
  614. } else if(mode == 'id') {
  615. /* JsLoadingOverlay.show({
  616. "overlayBackgroundColor": "#FFFFFF",
  617. "overlayOpacity": "0.9",
  618. "spinnerIcon": "ball-circus",
  619. "spinnerColor": "#B9DDF3",
  620. "spinnerSize": "2x",
  621. "overlayIDName": "overlay",
  622. "spinnerIDName": "spinner",
  623. "offsetX": 0,
  624. "offsetY": 0,
  625. "containerID": null,
  626. "lockScroll": false,
  627. "overlayZIndex": 9998,
  628. "spinnerZIndex": 9999
  629. }); */
  630. getData();
  631. }
  632. return id.split('=')[1];
  633. //load_data(id);
  634. }
  635. getMode();
  636. function getpathId(){
  637. let id = window.location.search.split('?').pop();
  638. return id.split('=')[1];
  639. }
  640. function getData() {
  641. let jwt_token = get_jwt_token();
  642. axios({
  643. method: 'post',
  644. url: 'https://www.choozmo.com:8887/history_input',
  645. headers: {
  646. 'accept': 'application/json',
  647. 'Authorization': `Bearer ${jwt_token}`
  648. }
  649. }).then(res => {
  650. loaded_data = res.data;
  651. console.log(loaded_data);
  652. const id = getpathId();
  653. load_data(id, loaded_data);
  654. }).catch(err => {
  655. console.log(err);
  656. });
  657. }
  658. let length = 5;
  659. function initial() {
  660. for (let i = 0; i < length; i++) {
  661. renderInputSec(i+1) ;
  662. }
  663. addRemoveFunc()
  664. console.log(document.querySelectorAll(".txtsrc").length + 1);
  665. }
  666. initial();
  667. // var addbtn = document.querySelector(".add");
  668. // var addimgbtn = document.querySelector(".addimg");
  669. // addbtn.addEventListener('click', addtxtBlock);
  670. // addimgbtn.addEventListener('click', addimgBlock);
  671. function addtxtBlock() {
  672. let newIdx = document.querySelectorAll(".txtsrc").length + 1;
  673. rendertxtBlock(newIdx);
  674. }
  675. function addimgBlock() {
  676. let newimgIdx = document.querySelectorAll(".imgsrc").length + 1;
  677. renderimgBlock(newimgIdx);
  678. }
  679. function rendertxtBlock(i) {
  680. var txtinput = document.createElement("input");
  681. txtinput.setAttribute('type', 'text');
  682. txtinput.setAttribute('name', `t${i}`);
  683. txtinput.value = "";
  684. txtinput.setAttribute('placeholder', `${i}`);
  685. txtinput.classList.add('txtsrc', `txtsrc${i}`)
  686. subtitleInputs.appendChild(txtinput);
  687. }
  688. function renderimgBlock(i) {
  689. var imginput = document.createElement("input");
  690. imginput.setAttribute('type', 'text');
  691. imginput.setAttribute('name', `m${i}`);
  692. imginput.classList.add('imgsrc', `imgsrc${i}`);
  693. imginput.value = "";
  694. imginput.setAttribute('placeholder', `${i}`);
  695. imgInputs.appendChild(imginput);
  696. var imgupload = document.createElement("input");
  697. imgupload.setAttribute('id', `img${i}`);
  698. imgupload.setAttribute('type', `file`);
  699. imgupload.classList.add('img_uploader', 'img_up');
  700. imgInputs.appendChild(imgupload);
  701. var imguploadlabel = document.createElement("label");
  702. imguploadlabel.setAttribute('for', `img${i}`);
  703. imguploadlabel.classList.add('upload-btn');
  704. imguploadlabel.textContent = '上傳檔案';
  705. if(lan == 'en') {
  706. imguploadlabel.textContent = 'Upload';
  707. }
  708. imguploadlabel.setAttribute('set-lan', `html:upload_file`);
  709. imgInputs.appendChild(imguploadlabel);
  710. $('input[type=file]').on('change', prepareUpload);
  711. }
  712. var addsecbtn = document.querySelector(".addsec");
  713. addsecbtn.addEventListener('click', addsecBlock);
  714. function addsecBlock() {
  715. let newsecIdx = document.querySelectorAll(".input-sec").length + 1;
  716. renderInputSec(newsecIdx);
  717. addRemoveFunc()
  718. }
  719. function renderInputSec(i) {
  720. var input_sec = document.createElement('div');
  721. var img_box = document.createElement('div');
  722. input_sec.classList.add('input-sec', `input-sec${i}`, 'p-3', 'mb-3', 'text-end');
  723. img_box.classList.add('img-box');
  724. // text input
  725. var txtinput = document.createElement("input");
  726. txtinput.setAttribute('type', 'text');
  727. txtinput.setAttribute('name', `t${i}`);
  728. txtinput.classList.add('txtsrc', `txtsrc${i}`);
  729. txtinput.value = "";
  730. txtinput.setAttribute('placeholder', `台詞`);
  731. if(lan == 'en') {
  732. txtinput.setAttribute('placeholder', `Line`);
  733. }
  734. input_sec.appendChild(txtinput);
  735. // img url input
  736. var imginput = document.createElement("input");
  737. imginput.setAttribute('type', 'text');
  738. imginput.setAttribute('name', `m${i}`);
  739. imginput.classList.add('imgsrc', `imgsrc${i}`);
  740. imginput.value = "";
  741. imginput.setAttribute('placeholder', '輸入圖片網址或上傳圖片');
  742. if(lan == 'en') {
  743. imginput.setAttribute('placeholder', 'Type in image link or upload image');
  744. }
  745. img_box.appendChild(imginput);
  746. // img upload input
  747. var imgupload = document.createElement("input");
  748. imgupload.setAttribute('id', `img${i}`);
  749. imgupload.setAttribute('type', `file`);
  750. imgupload.classList.add('img_uploader', 'img_up');
  751. imgupload.style.display = "none";
  752. img_box.appendChild(imgupload);
  753. // img upload label
  754. var imguploadlabel = document.createElement("label");
  755. imguploadlabel.setAttribute('for', `img${i}`);
  756. imguploadlabel.classList.add('upload-btn');
  757. // imguploadlabel.textContent = '上傳圖片';
  758. imguploadlabel.setAttribute('set-lan', `html:upload_file`);
  759. img_box.appendChild(imguploadlabel);
  760. imgupload.addEventListener('change', prepareUpload);
  761. var removeSpan = document.createElement("span");
  762. removeSpan.classList.add('remove-btn');
  763. removeSpan.setAttribute('data-num', `${i}`);
  764. removeSpan.textContent = '刪除';
  765. removeSpan.setAttribute('data-bs-toggle', `modal`);
  766. removeSpan.setAttribute('data-bs-target', `#deleteSecModal`);
  767. input_sec.appendChild(removeSpan);
  768. input_sec.appendChild(img_box);
  769. document.querySelector('.img-inputTest').append(input_sec);
  770. }
  771. $('.owl-carousel').owlCarousel({
  772. loop: true,
  773. margin: 10,
  774. nav: false,
  775. mouseDrag: true,
  776. touchDrag: true,
  777. smartSpeed: 1000,
  778. autoplay: true,
  779. autoplayTimeout: 8000,
  780. autoplayHoverPause: false,
  781. responsive: {
  782. 0: {
  783. items: 1
  784. },
  785. 600: {
  786. items: 2
  787. },
  788. 1000: {
  789. items: 4
  790. }
  791. }
  792. });
  793. // Handle draft
  794. $('.draft-btn').click(() => {
  795. let text = '儲存中...';
  796. if (lan == 'en') {
  797. text = 'Saving...';
  798. }
  799. $('.draft-btn').text(text);
  800. let jwt_token = get_jwt_token();
  801. let name_title = $('.title_new').val();
  802. let txtARR = [];
  803. let imgARR = [];
  804. var step;
  805. var Avatar_value;
  806. var Avatar_value2;
  807. $('input[name="AvatarVal"]:checked').each(function(){//遍歷每一個名字為interest的核取方塊,其中選中的執行函式
  808. Avatar_value=$(this).val();//將選中的值新增到陣列chk_value中
  809. });
  810. Avatar_value2=parseInt(Avatar_value);
  811. var avatar = Avatar_value2;
  812. console.log(avatar);
  813. if(!avatar) {
  814. avatar = 7;
  815. }
  816. let contentIdx = document.querySelectorAll(".txtsrc").length;
  817. for (let i = 1; i < (contentIdx + 1); i++) {
  818. if ($(`.txtsrc`).eq(i-1).val() != "") {
  819. txtARR.push($(`.txtsrc`).eq(i-1).val())
  820. }
  821. }
  822. let imgIdx = document.querySelectorAll(".imgsrc").length;
  823. for (let i = 1; i < (imgIdx + 1); i++) {
  824. if ($(`.imgsrc`).eq(i-1).val() != "") {
  825. imgARR.push($(`.imgsrc`).eq(i-1).val())
  826. }
  827. }
  828. let multiLang = 0
  829. if ($('#multiLang').prop("checked")) {multiLang = 1;}
  830. let id = window.location.search.split('?').pop();
  831. let mode = id.split('=')[0];
  832. if(mode == 'draftid') {
  833. id = Number(id.split('=')[1]);
  834. } else {
  835. id = -1;
  836. }
  837. let dataOBJ = { "id": id, "title": name_title, "text_content": txtARR, "image_urls": imgARR, "avatar": avatar,"multiLang":multiLang }
  838. console.log(dataOBJ);
  839. axios({
  840. method: 'post',
  841. url: 'https://www.choozmo.com:8887/save_draft',
  842. headers: {
  843. 'accept': 'application/json',
  844. 'Authorization': `Bearer ${jwt_token}`,
  845. 'Content-Type': 'application/json',
  846. },
  847. data: dataOBJ
  848. }).then(res => {
  849. console.log(res.data);
  850. $('.draft-btn').text('存為草稿');
  851. let title = "儲存完成";
  852. if(lan == 'en') {
  853. title = 'Saved!'
  854. $('.draft-btn').text('Save as Draft');
  855. }
  856. Swal.fire({
  857. title: title,
  858. icon: 'success',
  859. confirmButtonColor: '#3085d6',
  860. });
  861. }).catch(err => {
  862. console.log(err);
  863. });
  864. });
  865. function getDraftData() {
  866. let token = get_jwt_token();
  867. axios({
  868. method: 'post',
  869. url: 'https://www.choozmo.com:8887/draft_list',
  870. headers: {
  871. 'accept': 'application/json',
  872. 'Authorization': `Bearer ${token}`
  873. }
  874. }).then(res => {
  875. console.log(res.data);
  876. let result = [...res.data];
  877. let id = getpathId();
  878. load_data(id, result, true);
  879. }).catch(err => {
  880. console.log(err);
  881. });
  882. }
  883. // Copy to clipboard
  884. $(".copy").click(function(){
  885. const copyStr = $(this).next().text();
  886. copyToClipboard(copyStr);
  887. });
  888. const copyToClipboard = str => {
  889. const el = document.createElement('textarea');
  890. el.value = str;
  891. el.setAttribute('readonly', '');
  892. el.style.position = 'absolute';
  893. el.style.left = '-9999px';
  894. document.body.appendChild(el);
  895. el.select();
  896. document.execCommand('copy');
  897. document.body.removeChild(el);
  898. Swal.fire({
  899. toast: true,
  900. icon: 'success',
  901. position: 'top-end',
  902. title: 'Link copied!',
  903. timerProgressBar: true,
  904. showConfirmButton: false,
  905. timer: 1000,
  906. backdrop: false
  907. })
  908. };
  909. $("#lang-manu").hide();
  910. $( "#changeLanguage" ).click(function() {
  911. $("#lang-manu").slideToggle();
  912. $("#lang-arrow").toggleClass("arrowdoup");
  913. });
  914. function addRemoveFunc() {
  915. const testRemove = document.querySelectorAll('.remove-btn');
  916. [ ...testRemove ].forEach((item) => {
  917. item.addEventListener('click', testRemoving)
  918. });
  919. }
  920. function testRemoving(e) {
  921. console.log(e.target.parentElement)
  922. let el = e.target.parentElement
  923. $('.deleteSec').click(function(){
  924. el.remove();
  925. })
  926. }