script_util.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. var client_id = Date.now()
  2. var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))
  3. var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
  4. return new bootstrap.Tooltip(tooltipTriggerEl)
  5. });
  6. var myModal = new bootstrap.Modal(document.getElementById('history'), {
  7. keyboard: false
  8. })
  9. var avatarModal = new bootstrap.Modal(document.getElementById('avatarmega'), {
  10. keyboard: false
  11. })
  12. var modalImg = document.querySelector("#avatarmega .modal-img");
  13. var modalTitle = document.querySelector("#avatarmega .modal-title");
  14. var avatarSelector = document.getElementById("avatar");
  15. var card = document.getElementsByClassName('card');
  16. card = [...card];
  17. avatarSelector.addEventListener('change', avatarChange);
  18. avatarChange();
  19. function addCardListener() {
  20. for (let i = 0; i < card.length; i++) {
  21. card[i].addEventListener('click', openavatarModel);
  22. }
  23. }
  24. addCardListener();
  25. function avatarChange() {
  26. var value = avatarSelector.options[avatarSelector.selectedIndex].text;
  27. $('.owl-carousel').trigger('to.owl.carousel', avatarSelector.selectedIndex);
  28. console.log(value);
  29. for (let i = 0; i < card.length; i++) {
  30. card[i].classList.remove('active');
  31. if (card[i].dataset.avatar == value) {
  32. card[i].classList.add('active');
  33. }
  34. }
  35. }
  36. function openavatarModel() {
  37. console.log(this.dataset.img);
  38. modalImg.setAttribute("src", `static/img/${this.dataset.img}.webp`);
  39. modalTitle.textContent = `${this.dataset.avatar}`;
  40. avatarModal.show();
  41. }
  42. $('input[type=file]').on('change', prepareUpload);
  43. function prepareUpload(event) {
  44. files = event.target.files;
  45. var data = new FormData();
  46. //data.append('file', $('.img_up1').prop('files')[0]);
  47. data.append('file', files[0]);
  48. // append other variables to data if you want: data.append('field_name_x', field_value_x);
  49. $(this).next().text('');
  50. $(this).next().html('<img src="static/img/Spinner-1s-181px.gif">');
  51. $.ajax({
  52. type: 'POST',
  53. processData: false, // important
  54. contentType: false, // important
  55. data: data,
  56. url: 'http://www.choozmo.com:8888/uploadfile',
  57. dataType: 'json',
  58. success: function (jsonData) {
  59. event.target.previousSibling.value = jsonData.msg;
  60. $(this).prev().val(jsonData.msg);
  61. event.target.nextSibling.innerHTML = '';
  62. event.target.nextSibling.textContent = '上傳檔案';
  63. //console.log($(this).next());
  64. //$(this).next().html('上傳檔案');
  65. //$(this).next().text('上傳檔案');
  66. },
  67. error: function (error) {
  68. event.target.nextSibling.innerHTML = '';
  69. event.target.nextSibling.textContent = '上傳檔案';
  70. alert('圖片錯誤');
  71. }
  72. });
  73. }
  74. const button = document.querySelector('.next');
  75. $(".next").click(function () {
  76. button.setAttribute('disabled', '');
  77. setTimeout(function () {
  78. button.removeAttribute('disabled')
  79. }, 4000);
  80. avatar = $('.avatar').val();
  81. name_title = $('.title_new').val();
  82. txtARR = [];
  83. imgARR = [];
  84. var step;
  85. let contentIdx = document.querySelectorAll(".txtsrc").length;
  86. for (let i = 1; i < (contentIdx + 1); i++) {
  87. if ($(`.txtsrc${i}`).val() != "") {
  88. txtARR.push($(`.txtsrc${i}`).val())
  89. }
  90. }
  91. let imgIdx = document.querySelectorAll(".imgsrc").length;
  92. for (let i = 1; i < (imgIdx + 1); i++) {
  93. if ($(`.imgsrc${i}`).val() != "") {
  94. imgARR.push($(`.imgsrc${i}`).val())
  95. }
  96. }
  97. dataOBJ = { "name": name_title, "text_content": txtARR, "image_urls": imgARR, "avatar": avatar, "client_id": client_id }
  98. objstr = JSON.stringify(dataOBJ);
  99. console.log(dataOBJ)
  100. jwt_token = get_jwt_token()
  101. var xhr = new XMLHttpRequest();
  102. xhr.open("POST", "http://192.168.1.106:8888/make_anchor_video");
  103. xhr.setRequestHeader("accept", "application/json");
  104. xhr.setRequestHeader("Authorization","Bearer "+jwt_token)
  105. xhr.setRequestHeader("Content-Type", "application/json");
  106. xhr.onreadystatechange = function () {
  107. if (xhr.readyState === 4) {
  108. Swal.fire({
  109. title: "資料已送出",
  110. icon: 'success',
  111. text: '資料已傳送,請耐心等候',
  112. confirmButtonColor: '#3085d6',
  113. });
  114. }
  115. };
  116. var data = renderXHR_data(dataOBJ)
  117. console.log(data)
  118. result = xhr.send(objstr);
  119. });
  120. $(".gen_avatar").click(function () {
  121. dataOBJ = { "imgurl": $('.img_src').val() }
  122. objstr = JSON.stringify(dataOBJ);
  123. console.log(dataOBJ)
  124. //alert('資料已送出! 請耐心等候')
  125. $.ajax({
  126. url: 'http://www.choozmo.com:8888/swapFace',
  127. dataType: 'json', // 預期從server接收的資料型態
  128. contentType: 'application/json; charset=utf-8', // 要送到server的資料型態
  129. type: 'post',
  130. data: objstr,
  131. success: function (suc_data) {
  132. alert(suc_data.msg)
  133. },
  134. //data:JSON.stringify({n1:"12",n2:"22"}),
  135. error: function (error) {
  136. console.error(error)
  137. }
  138. });
  139. });
  140. var loaded_data = ''
  141. function openNav() {
  142. document.getElementById("mySidenav").style.width = "250px";
  143. document.querySelector('.loader').style.display = "block";
  144. $.get("http://192.168.1.106:8888/history_input", function (data, status) {
  145. console.log(data)
  146. loaded_data = data
  147. for (var obj of data) {
  148. var historyList = document.querySelector('.historyList');
  149. var list = document.createElement('li');
  150. list.id = obj.id;
  151. // div-imgfr
  152. var divImgfr = document.createElement('div');
  153. divImgfr.classList.add('item_imgfr');
  154. var img = document.createElement('img');
  155. img.setAttribute('src', obj['image_urls'][0]);
  156. divImgfr.appendChild(img);
  157. // div-content
  158. var contentBox = document.createElement('div');
  159. contentBox.classList.add('content-box');
  160. var boxTitle = document.createElement('p');
  161. boxTitle.classList.add('box-title');
  162. boxTitle.textContent = obj.name;
  163. boxTitle.id = obj.id;
  164. boxTitle.setAttribute('onclick', 'load_data()');
  165. var boxLink = document.createElement('span');
  166. boxLink.classList.add('box-link');
  167. boxLink.setAttribute("data-url", obj.link);
  168. boxLink.setAttribute('onclick', 'view()');
  169. boxLink.innerHTML = '<i class="fas fa-play-circle me-1"></i>觀看影片';
  170. contentBox.appendChild(boxTitle);
  171. contentBox.appendChild(boxLink);
  172. list.classList.add("historyList-item");
  173. list.setAttribute('onclick', 'load_data()');
  174. list.appendChild(divImgfr);
  175. list.appendChild(contentBox);
  176. historyList.appendChild(list);
  177. }
  178. document.querySelector('.loader').style.display = "none";
  179. });
  180. }
  181. function closeNav() {
  182. document.getElementById("mySidenav").style.width = "250px";
  183. }
  184. function view() {
  185. event.stopPropagation();
  186. console.log(event.target);
  187. if (event.target.nodeName === 'I') {
  188. return;
  189. } else {
  190. window.open(`http://${event.target.dataset.url}`, '_blank');
  191. }
  192. }
  193. function renderXHR_data(jsonObj) {
  194. XHRstring = ''
  195. for (const [key, value] of Object.entries(jsonObj)) {
  196. console.log(value)
  197. if (typeof (value) == "object") {
  198. XHRstring += (key+'=['+value.join(',')+']&')
  199. }
  200. else {
  201. XHRstring += (key + '=' + value + '&')
  202. }
  203. }
  204. XHRstring = XHRstring.substring(0, XHRstring.length - 1);
  205. return XHRstring
  206. }
  207. function get_jwt_token(){
  208. jwt_raw = document.cookie.split(';').filter(s=>s.includes('jwt_token'))[0]
  209. return jwt_raw.split('=')[1]
  210. }
  211. function load_data() {
  212. var title = document.getElementById("title");
  213. var linker = document.getElementById("linker");
  214. myModal.hide()
  215. tid = event.srcElement.id
  216. console.log(tid);
  217. linker.setAttribute('href', `http://${loaded_data.find(item => item.id == tid).link}`)
  218. linker.setAttribute('target', '_blank')
  219. $("#linker").html(`http://${loaded_data.find(item => item.id == tid).link}`)
  220. $("#linker").show();
  221. $(".linker__box").show();
  222. let historyItem = loaded_data.filter(item => item.id == tid)[0];
  223. console.log(historyItem);
  224. $(".title_new").val(loaded_data.find(item => item.id == tid).name);
  225. let txtlength = historyItem.text_content.length;
  226. let imglength = historyItem.image_urls.length;
  227. subtitleInputs.innerHTML = '';
  228. imgInputs.innerHTML = '';
  229. for (let i = 0; i < txtlength; i++) {
  230. var txtinput = document.createElement("input");
  231. txtinput.setAttribute('type', 'text');
  232. txtinput.setAttribute('name', `t${i + 1}`);
  233. txtinput.value = historyItem.text_content[i];
  234. txtinput.setAttribute('placeholder', `${i + 1}`);
  235. txtinput.classList.add('txtsrc', `txtsrc${i + 1}`)
  236. subtitleInputs.appendChild(txtinput);
  237. }
  238. for (let i = 0; i < imglength; i++) {
  239. var imginput = document.createElement("input");
  240. imginput.setAttribute('type', 'text');
  241. imginput.setAttribute('name', `m${i + 1}`);
  242. imginput.classList.add('imgsrc', `imgsrc${i + 1}`);
  243. imginput.value = historyItem.image_urls[i];
  244. imginput.setAttribute('placeholder', `${i + 1}`);
  245. imgInputs.appendChild(imginput);
  246. var imgupload = document.createElement("input");
  247. imgupload.setAttribute('id', `img${i + 1}`);
  248. imgupload.setAttribute('type', `file`);
  249. imgupload.classList.add('img_uploader', 'img_up');
  250. imgInputs.appendChild(imgupload);
  251. var imguploadlabel = document.createElement("label");
  252. imguploadlabel.setAttribute('for', `img${i + 1}`);
  253. imguploadlabel.classList.add('upload-btn');
  254. imguploadlabel.textContent = '上傳檔案';
  255. imgInputs.appendChild(imguploadlabel);
  256. }
  257. }
  258. var subtitleInputs = document.querySelector(".subtitle-inputs");
  259. var imgInputs = document.querySelector(".img-inputs");
  260. let length = 5;
  261. function initial() {
  262. for (let i = 0; i < length; i++) {
  263. rendertxtBlock(i + 1);
  264. renderimgBlock(i + 1);
  265. }
  266. console.log(document.querySelectorAll(".txtsrc").length + 1);
  267. }
  268. initial();
  269. var addbtn = document.querySelector(".add");
  270. var addimgbtn = document.querySelector(".addimg");
  271. addbtn.addEventListener('click', addtxtBlock);
  272. addimgbtn.addEventListener('click', addimgBlock);
  273. function addtxtBlock() {
  274. let newIdx = document.querySelectorAll(".txtsrc").length + 1;
  275. rendertxtBlock(newIdx);
  276. }
  277. function addimgBlock() {
  278. let newimgIdx = document.querySelectorAll(".imgsrc").length + 1;
  279. renderimgBlock(newimgIdx);
  280. }
  281. function rendertxtBlock(i) {
  282. var txtinput = document.createElement("input");
  283. txtinput.setAttribute('type', 'text');
  284. txtinput.setAttribute('name', `t${i}`);
  285. txtinput.value = "";
  286. txtinput.setAttribute('placeholder', `${i}`);
  287. txtinput.classList.add('txtsrc', `txtsrc${i}`)
  288. subtitleInputs.appendChild(txtinput);
  289. }
  290. function renderimgBlock(i) {
  291. var imginput = document.createElement("input");
  292. imginput.setAttribute('type', 'text');
  293. imginput.setAttribute('name', `m${i}`);
  294. imginput.classList.add('imgsrc', `imgsrc${i}`);
  295. imginput.value = "";
  296. imginput.setAttribute('placeholder', `${i}`);
  297. imgInputs.appendChild(imginput);
  298. var imgupload = document.createElement("input");
  299. imgupload.setAttribute('id', `img${i}`);
  300. imgupload.setAttribute('type', `file`);
  301. imgupload.classList.add('img_uploader', 'img_up');
  302. imgInputs.appendChild(imgupload);
  303. var imguploadlabel = document.createElement("label");
  304. imguploadlabel.setAttribute('for', `img${i}`);
  305. imguploadlabel.classList.add('upload-btn');
  306. imguploadlabel.textContent = '上傳檔案';
  307. imgInputs.appendChild(imguploadlabel);
  308. $('input[type=file]').on('change', prepareUpload);
  309. }
  310. $('.owl-carousel').owlCarousel({
  311. loop: true,
  312. margin: 10,
  313. nav: false,
  314. mouseDrag: true,
  315. touchDrag: true,
  316. smartSpeed: 1000,
  317. autoplay: true,
  318. autoplayTimeout: 8000,
  319. autoplayHoverPause: false,
  320. responsive: {
  321. 0: {
  322. items: 1
  323. },
  324. 600: {
  325. items: 2
  326. },
  327. 1000: {
  328. items: 4
  329. }
  330. }
  331. })