script_eng.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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-4 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. var client_id = Date.now();
  42. var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
  43. var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
  44. return new bootstrap.Tooltip(tooltipTriggerEl)
  45. });
  46. var myModal = new bootstrap.Modal(document.getElementById('history'), {
  47. keyboard: false
  48. })
  49. var avatarModal = new bootstrap.Modal(document.getElementById('avatarmega'), {
  50. keyboard: false
  51. })
  52. var modalImg = document.querySelector("#avatarmega .modal-img");
  53. var modalTitle = document.querySelector("#avatarmega .modal-title");
  54. var avatarSelector = document.getElementById("avatar");
  55. var card = document.getElementsByClassName('card');
  56. card = [...card];
  57. avatarSelector.addEventListener('change', avatarChange);
  58. avatarChange();
  59. function addCardListener() {
  60. for (let i = 0; i < card.length; i++) {
  61. card[i].addEventListener('click', openavatarModel);
  62. }
  63. }
  64. addCardListener();
  65. function avatarChange() {
  66. var value = avatarSelector.options[avatarSelector.selectedIndex].text;
  67. $('.owl-carousel').trigger('to.owl.carousel', avatarSelector.selectedIndex);
  68. for (let i = 0; i < card.length; i++) {
  69. card[i].classList.remove('active');
  70. if (card[i].dataset.avatar == value) {
  71. card[i].classList.add('active');
  72. }
  73. }
  74. }
  75. function openavatarModel() {
  76. console.log(this.dataset.img);
  77. modalImg.setAttribute("src", `static/img/${this.dataset.img}.webp`);
  78. modalTitle.textContent = `${this.dataset.avatar}`;
  79. avatarModal.show();
  80. }
  81. $('input[type=file]').on('change', prepareUpload);
  82. function prepareUpload(event) {
  83. files = event.target.files;
  84. var data = new FormData();
  85. //data.append('file', $('.img_up1').prop('files')[0]);
  86. data.append('file', files[0]);
  87. // append other variables to data if you want: data.append('field_name_x', field_value_x);
  88. $(this).next().text('');
  89. $(this).next().html('<img src="static/img/Spinner-1s-181px.gif">');
  90. $.ajax({
  91. type: 'POST',
  92. processData: false, // important
  93. contentType: false, // important
  94. data: data,
  95. url: 'https://www.choozmo.com:8887/uploadfile',
  96. dataType: 'json',
  97. success: function (jsonData) {
  98. event.target.previousSibling.value = jsonData.msg;
  99. $(this).prev().val(jsonData.msg);
  100. event.target.nextSibling.innerHTML = '';
  101. event.target.nextSibling.textContent = 'Upload';
  102. //console.log($(this).next());
  103. //$(this).next().html('上傳檔案');
  104. //$(this).next().text('上傳檔案');
  105. },
  106. error: function (error) {
  107. event.target.nextSibling.innerHTML = '';
  108. event.target.nextSibling.textContent = '上傳檔案';
  109. alert('圖片錯誤');
  110. }
  111. });
  112. }
  113. const button = document.querySelector('.next');
  114. $(".next").click(function () {
  115. button.setAttribute('disabled', '');
  116. setTimeout(function () {
  117. button.removeAttribute('disabled')
  118. }, 8000);
  119. avatar = $('.avatar').val();
  120. name_title = $('.title_new').val();
  121. txtARR = [];
  122. imgARR = [];
  123. sublineARR = [];
  124. var step;
  125. let contentIdx = document.querySelectorAll(".txtsrc").length;
  126. for (let i = 1; i < (contentIdx + 1); i++) {
  127. if ($(`.txtsrc${i}`).val() != "") {
  128. txtARR.push($(`.txtsrc${i}`).val())
  129. }
  130. }
  131. let sublineIdx = document.querySelectorAll(".subsrc").length;
  132. for (let i = 1; i < (sublineIdx + 1); i++) {
  133. if ($(`.subsrc${i}`).val() != "") {
  134. sublineARR.push($(`.subsrc${i}`).val())
  135. }
  136. }
  137. let imgIdx = document.querySelectorAll(".imgsrc").length;
  138. for (let i = 1; i < (imgIdx + 1); i++) {
  139. if ($(`.imgsrc${i}`).val() != "") {
  140. imgARR.push($(`.imgsrc${i}`).val())
  141. }
  142. }
  143. dataOBJ = { "name": name_title, "text_content": txtARR, "image_urls": imgARR, "sub_titles": sublineARR, "avatar": avatar };
  144. objstr = JSON.stringify(dataOBJ);
  145. console.log(dataOBJ);
  146. jwt_token = get_jwt_token();
  147. var xhr = new XMLHttpRequest();
  148. xhr.open("POST", "https://www.choozmo.com:8887/make_anchor_video_eng");
  149. xhr.setRequestHeader("accept", "application/json");
  150. xhr.setRequestHeader("Authorization","Bearer "+jwt_token)
  151. xhr.setRequestHeader("Content-Type", "application/json");
  152. xhr.onreadystatechange = function () {
  153. if (xhr.readyState === 4) {
  154. responseOBJ = JSON.parse(xhr.responseText);
  155. if (responseOBJ.msg=='ok')
  156. {
  157. Swal.fire({
  158. title: "Data Submitted Successfully!",
  159. icon: 'success',
  160. text: 'We are working on your video! It might take minutes to get your video.',
  161. confirmButtonColor: '#3085d6',
  162. });
  163. }
  164. else{
  165. Swal.fire({
  166. title: "Oops! Some error occurred!",
  167. icon: 'error',
  168. text: responseOBJ.msg,
  169. confirmButtonColor: '#3085d6',
  170. });
  171. }
  172. }
  173. };
  174. var data = renderXHR_data(dataOBJ)
  175. console.log(data)
  176. result = xhr.send(objstr);
  177. });
  178. var loaded_data = ''
  179. function openNav() {
  180. document.getElementById("mySidenav").style.width = "250px";
  181. document.querySelector('.loader').style.display = "block";
  182. let jwt_token = get_jwt_token();
  183. axios({
  184. method: 'post',
  185. url: 'https://www.choozmo.com:8887/history_input',
  186. headers: {
  187. 'accept': 'application/json',
  188. 'Authorization': `Bearer ${jwt_token}`
  189. }
  190. }).then(res => {
  191. console.log(res.data);
  192. loaded_data = res.data;
  193. var historyList = document.querySelector('.historyList');
  194. historyList.innerHTML = '';
  195. for (var obj of loaded_data) {
  196. var list = document.createElement('li');
  197. list.id = obj.id;
  198. // div-imgfr
  199. var divImgfr = document.createElement('div');
  200. divImgfr.classList.add('item_imgfr');
  201. var img = document.createElement('img');
  202. img.setAttribute('src', obj['image_urls'][0]);
  203. divImgfr.appendChild(img);
  204. // div-content
  205. var contentBox = document.createElement('div');
  206. contentBox.classList.add('content-box');
  207. var boxTitle = document.createElement('p');
  208. boxTitle.classList.add('box-title');
  209. boxTitle.textContent = obj.name;
  210. boxTitle.id = obj.id;
  211. boxTitle.setAttribute('onclick', 'load_data()');
  212. var boxLink = document.createElement('span');
  213. boxLink.classList.add('box-link');
  214. boxLink.setAttribute("data-url", obj.link);
  215. boxLink.setAttribute('onclick', 'view()');
  216. boxLink.innerHTML = '<i class="fas fa-play-circle me-1"></i>觀看影片';
  217. contentBox.appendChild(boxTitle);
  218. contentBox.appendChild(boxLink);
  219. list.classList.add("historyList-item");
  220. list.setAttribute('onclick', 'load_data()');
  221. list.appendChild(divImgfr);
  222. list.appendChild(contentBox);
  223. historyList.appendChild(list);
  224. }
  225. document.querySelector('.loader').style.display = "none";
  226. }).catch(err => {
  227. console.log(err);
  228. });
  229. }
  230. function closeNav() {
  231. document.getElementById("mySidenav").style.width = "250px";
  232. }
  233. function view() {
  234. event.stopPropagation();
  235. console.log(event.target);
  236. if (event.target.nodeName === 'I') {
  237. return;
  238. } else {
  239. window.open(`http://${event.target.dataset.url}`, '_blank');
  240. }
  241. }
  242. function renderXHR_data(jsonObj) {
  243. XHRstring = ''
  244. for (const [key, value] of Object.entries(jsonObj)) {
  245. console.log(value)
  246. if (typeof (value) == "object") {
  247. XHRstring += (key+'=['+value.join(',')+']&')
  248. }
  249. else {
  250. XHRstring += (key + '=' + value + '&')
  251. }
  252. }
  253. XHRstring = XHRstring.substring(0, XHRstring.length - 1);
  254. return XHRstring
  255. }
  256. function get_jwt_token(){
  257. jwt_raw = document.cookie.split(';').filter(s=>s.includes('jwt_token'))[0];
  258. if(!jwt_raw) {return}
  259. return jwt_raw.split('=')[1];
  260. }
  261. function load_data() {
  262. var title = document.getElementById("title");
  263. var linker = document.getElementById("linker");
  264. myModal.hide()
  265. tid = event.srcElement.id
  266. console.log(tid);
  267. linker.setAttribute('href', `http://${loaded_data.find(item => item.id == tid).link}`)
  268. linker.setAttribute('target', '_blank')
  269. $("#linker").html(`http://${loaded_data.find(item => item.id == tid).link}`)
  270. $("#linker").show();
  271. $(".linker__box").show();
  272. let historyItem = loaded_data.filter(item => item.id == tid)[0];
  273. console.log(historyItem);
  274. $(".title_new").val(loaded_data.find(item => item.id == tid).name);
  275. let txtlength = historyItem.text_content.length;
  276. let imglength = historyItem.image_urls.length;
  277. let sublinelength = historyItem.sub_titles.length;
  278. subtitleInputs.innerHTML = '';
  279. imgInputs.innerHTML = '';
  280. sublineInputs.innerHTML = '';
  281. for (let i = 0; i < txtlength; i++) {
  282. var txtinput = document.createElement("input");
  283. txtinput.setAttribute('type', 'text');
  284. txtinput.setAttribute('name', `t${i + 1}`);
  285. txtinput.value = historyItem.text_content[i];
  286. txtinput.setAttribute('placeholder', `${i + 1}`);
  287. txtinput.classList.add('txtsrc', `txtsrc${i + 1}`)
  288. subtitleInputs.appendChild(txtinput);
  289. }
  290. for (let i = 0; i < sublinelength; i++) {
  291. var txtinput = document.createElement("input");
  292. txtinput.setAttribute('type', 'text');
  293. txtinput.setAttribute('name', `s${i + 1}`);
  294. txtinput.value = historyItem.sub_titles[i];
  295. txtinput.setAttribute('placeholder', `${i + 1}`);
  296. txtinput.classList.add('subsrc', `subsrc${i + 1}`)
  297. sublineInputs.appendChild(txtinput);
  298. }
  299. for (let i = 0; i < imglength; i++) {
  300. var imginput = document.createElement("input");
  301. imginput.setAttribute('type', 'text');
  302. imginput.setAttribute('name', `m${i + 1}`);
  303. imginput.classList.add('imgsrc', `imgsrc${i + 1}`);
  304. imginput.value = historyItem.image_urls[i];
  305. imginput.setAttribute('placeholder', `${i + 1}`);
  306. imgInputs.appendChild(imginput);
  307. var imgupload = document.createElement("input");
  308. imgupload.setAttribute('id', `img${i + 1}`);
  309. imgupload.setAttribute('type', `file`);
  310. imgupload.classList.add('img_uploader', 'img_up');
  311. imgInputs.appendChild(imgupload);
  312. var imguploadlabel = document.createElement("label");
  313. imguploadlabel.setAttribute('for', `img${i + 1}`);
  314. imguploadlabel.classList.add('upload-btn');
  315. imguploadlabel.textContent = 'Upload';
  316. imgInputs.appendChild(imguploadlabel);
  317. }
  318. }
  319. var subtitleInputs = document.querySelector(".subtitle-inputs");
  320. var sublineInputs = document.querySelector(".subline-inputs");
  321. var imgInputs = document.querySelector(".img-inputs");
  322. let length = 5;
  323. function initial() {
  324. for (let i = 0; i < length; i++) {
  325. rendertxtBlock(i + 1);
  326. renderimgBlock(i + 1);
  327. renderSublineBlock(i +1);
  328. }
  329. console.log(document.querySelectorAll(".txtsrc").length + 1);
  330. }
  331. initial();
  332. var addbtn = document.querySelector(".add");
  333. var addimgbtn = document.querySelector(".addimg");
  334. var addSubline = document.querySelector('.add-subline');
  335. addbtn.addEventListener('click', addtxtBlock);
  336. addimgbtn.addEventListener('click', addimgBlock);
  337. addSubline.addEventListener('click', addsubBlock);
  338. function addtxtBlock() {
  339. let newIdx = document.querySelectorAll(".txtsrc").length + 1;
  340. rendertxtBlock(newIdx);
  341. }
  342. function addsubBlock() {
  343. let newIdx = document.querySelectorAll(".subsrc").length + 1;
  344. renderSublineBlock(newIdx);
  345. }
  346. function addimgBlock() {
  347. let newimgIdx = document.querySelectorAll(".imgsrc").length + 1;
  348. renderimgBlock(newimgIdx);
  349. }
  350. function rendertxtBlock(i) {
  351. var txtinput = document.createElement("input");
  352. txtinput.setAttribute('type', 'text');
  353. txtinput.setAttribute('name', `t${i}`);
  354. txtinput.value = "";
  355. txtinput.setAttribute('placeholder', `${i}`);
  356. txtinput.classList.add('txtsrc', `txtsrc${i}`)
  357. subtitleInputs.appendChild(txtinput);
  358. }
  359. function renderSublineBlock(i) {
  360. var txtinput = document.createElement("input");
  361. txtinput.setAttribute('type', 'text');
  362. txtinput.setAttribute('name', `s${i}`);
  363. txtinput.value = "";
  364. txtinput.setAttribute('placeholder', `${i}`);
  365. txtinput.classList.add('subsrc', `subsrc${i}`)
  366. sublineInputs.appendChild(txtinput);
  367. }
  368. function renderimgBlock(i) {
  369. var imginput = document.createElement("input");
  370. imginput.setAttribute('type', 'text');
  371. imginput.setAttribute('name', `m${i}`);
  372. imginput.classList.add('imgsrc', `imgsrc${i}`);
  373. imginput.value = "";
  374. imginput.setAttribute('placeholder', `${i}`);
  375. imgInputs.appendChild(imginput);
  376. var imgupload = document.createElement("input");
  377. imgupload.setAttribute('id', `img${i}`);
  378. imgupload.setAttribute('type', `file`);
  379. imgupload.classList.add('img_uploader', 'img_up');
  380. imgInputs.appendChild(imgupload);
  381. var imguploadlabel = document.createElement("label");
  382. imguploadlabel.setAttribute('for', `img${i}`);
  383. imguploadlabel.classList.add('upload-btn');
  384. imguploadlabel.textContent = 'Upload';
  385. imgInputs.appendChild(imguploadlabel);
  386. $('input[type=file]').on('change', prepareUpload);
  387. }