script_msg.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. //alert('資料已送出! 請耐心等候')
  101. $.ajax({
  102. url: 'http://www.choozmo.com:8888/make_anchor_video_v2',
  103. //url: 'http://www.choozmo.com:8888/qqreq',
  104. dataType : 'json', // 預期從server接收的資料型態
  105. contentType : 'application/json; charset=utf-8', // 要送到server的資料型態
  106. type: 'post',
  107. data: objstr,
  108. success: function(suc_data) {
  109. Swal.fire({
  110. title: "資料已送出",
  111. icon: 'success',
  112. text: `${suc_data.msg}`,
  113. confirmButtonColor: '#3085d6',
  114. });
  115. },
  116. //data:JSON.stringify({n1:"12",n2:"22"}),
  117. error: function (error) {
  118. console.error(error)
  119. }
  120. });
  121. });
  122. $(".gen_avatar").click(function () {
  123. dataOBJ = { "imgurl": $('.img_src').val() }
  124. objstr = JSON.stringify(dataOBJ);
  125. console.log(dataOBJ)
  126. //alert('資料已送出! 請耐心等候')
  127. $.ajax({
  128. url: 'http://www.choozmo.com:8888/swapFace',
  129. dataType: 'json', // 預期從server接收的資料型態
  130. contentType: 'application/json; charset=utf-8', // 要送到server的資料型態
  131. type: 'post',
  132. data: objstr,
  133. success: function (suc_data) {
  134. alert(suc_data.msg)
  135. },
  136. //data:JSON.stringify({n1:"12",n2:"22"}),
  137. error: function (error) {
  138. console.error(error)
  139. }
  140. });
  141. });
  142. var loaded_data = ''
  143. function openNav() {
  144. document.getElementById("mySidenav").style.width = "250px";
  145. document.querySelector('.loader').style.display = "block";
  146. $.get("http://www.choozmo.com:8888/history_input", function (data, status) {
  147. console.log(data)
  148. loaded_data = data
  149. for (var obj of data) {
  150. var historyList = document.querySelector('.historyList');
  151. var list = document.createElement('li');
  152. list.id = obj.id;
  153. // div-imgfr
  154. var divImgfr = document.createElement('div');
  155. divImgfr.classList.add('item_imgfr');
  156. var img = document.createElement('img');
  157. img.setAttribute('src', obj['image_urls'][0]);
  158. divImgfr.appendChild(img);
  159. // div-content
  160. var contentBox = document.createElement('div');
  161. contentBox.classList.add('content-box');
  162. var boxTitle = document.createElement('p');
  163. boxTitle.classList.add('box-title');
  164. boxTitle.textContent = obj.name;
  165. boxTitle.id = obj.id;
  166. boxTitle.setAttribute('onclick', 'load_data()');
  167. var boxLink = document.createElement('span');
  168. boxLink.classList.add('box-link');
  169. boxLink.setAttribute("data-url", obj.link);
  170. boxLink.setAttribute('onclick', 'view()');
  171. boxLink.innerHTML = '<i class="fas fa-play-circle me-1"></i>觀看影片';
  172. contentBox.appendChild(boxTitle);
  173. contentBox.appendChild(boxLink);
  174. list.classList.add("historyList-item");
  175. list.setAttribute('onclick', 'load_data()');
  176. list.appendChild(divImgfr);
  177. list.appendChild(contentBox);
  178. historyList.appendChild(list);
  179. }
  180. document.querySelector('.loader').style.display = "none";
  181. });
  182. }
  183. function closeNav() {
  184. document.getElementById("mySidenav").style.width = "250px";
  185. }
  186. function view() {
  187. event.stopPropagation();
  188. console.log(event.target);
  189. if(event.target.nodeName === 'I') {
  190. return;
  191. } else {
  192. window.open(`http://${event.target.dataset.url}`, '_blank');
  193. }
  194. }
  195. function load_data() {
  196. var title = document.getElementById("title");
  197. var linker = document.getElementById("linker");
  198. myModal.hide()
  199. tid = event.srcElement.id
  200. console.log(tid);
  201. linker.setAttribute('href', `http://${loaded_data.find(item => item.id == tid).link}`)
  202. linker.setAttribute('target', '_blank')
  203. $("#linker").html(`http://${loaded_data.find(item => item.id == tid).link}`)
  204. $("#linker").show();
  205. $(".linker__box").show();
  206. let historyItem = loaded_data.filter(item => item.id == tid)[0];
  207. console.log(historyItem);
  208. $(".title_new").val(loaded_data.find(item => item.id == tid).name);
  209. let txtlength = historyItem.text_content.length;
  210. let imglength = historyItem.image_urls.length;
  211. subtitleInputs.innerHTML = '';
  212. imgInputs.innerHTML = '';
  213. for (let i = 0; i < txtlength ; i++) {
  214. var txtinput = document.createElement("input");
  215. txtinput.setAttribute('type', 'text');
  216. txtinput.setAttribute('name', `t${i+1}`);
  217. txtinput.value = historyItem.text_content[i];
  218. txtinput.setAttribute('placeholder', `${i+1}`);
  219. txtinput.classList.add('txtsrc',`txtsrc${i+1}`)
  220. subtitleInputs.appendChild(txtinput);
  221. }
  222. for (let i = 0; i < imglength; i++) {
  223. var imginput = document.createElement("input");
  224. imginput.setAttribute('type', 'text');
  225. imginput.setAttribute('name', `m${i+1}`);
  226. imginput.classList.add('imgsrc', `imgsrc${i+1}`);
  227. imginput.value = historyItem.image_urls[i];
  228. imginput.setAttribute('placeholder', `${i+1}`);
  229. imgInputs.appendChild(imginput);
  230. var imgupload = document.createElement("input");
  231. imgupload.setAttribute('id', `img${i+1}`);
  232. imgupload.setAttribute('type', `file`);
  233. imgupload.classList.add('img_uploader', 'img_up');
  234. imgInputs.appendChild(imgupload);
  235. var imguploadlabel = document.createElement("label");
  236. imguploadlabel.setAttribute('for', `img${i+1}`);
  237. imguploadlabel.classList.add('upload-btn');
  238. imguploadlabel.textContent = '上傳檔案';
  239. imgInputs.appendChild(imguploadlabel);
  240. }
  241. }
  242. var subtitleInputs = document.querySelector(".subtitle-inputs");
  243. var imgInputs = document.querySelector(".img-inputs");
  244. let length = 5;
  245. function initial() {
  246. for(let i = 0;i < length; i++) {
  247. rendertxtBlock(i+1);
  248. renderimgBlock(i+1);
  249. }
  250. console.log(document.querySelectorAll(".txtsrc").length + 1);
  251. }
  252. initial();
  253. var addbtn = document.querySelector(".add");
  254. var addimgbtn = document.querySelector(".addimg");
  255. addbtn.addEventListener('click', addtxtBlock);
  256. addimgbtn.addEventListener('click', addimgBlock);
  257. function addtxtBlock() {
  258. let newIdx = document.querySelectorAll(".txtsrc").length + 1;
  259. rendertxtBlock(newIdx);
  260. }
  261. function addimgBlock() {
  262. let newimgIdx = document.querySelectorAll(".imgsrc").length + 1;
  263. renderimgBlock(newimgIdx);
  264. }
  265. function rendertxtBlock(i){
  266. var txtinput = document.createElement("input");
  267. txtinput.setAttribute('type', 'text');
  268. txtinput.setAttribute('name', `t${i}`);
  269. txtinput.value = "";
  270. txtinput.setAttribute('placeholder', `${i}`);
  271. txtinput.classList.add('txtsrc',`txtsrc${i}`)
  272. subtitleInputs.appendChild(txtinput);
  273. }
  274. function renderimgBlock(i) {
  275. var imginput = document.createElement("input");
  276. imginput.setAttribute('type', 'text');
  277. imginput.setAttribute('name', `m${i}`);
  278. imginput.classList.add('imgsrc', `imgsrc${i}`);
  279. imginput.value = "";
  280. imginput.setAttribute('placeholder', `${i}`);
  281. imgInputs.appendChild(imginput);
  282. var imgupload = document.createElement("input");
  283. imgupload.setAttribute('id', `img${i}`);
  284. imgupload.setAttribute('type', `file`);
  285. imgupload.classList.add('img_uploader', 'img_up');
  286. imgInputs.appendChild(imgupload);
  287. var imguploadlabel = document.createElement("label");
  288. imguploadlabel.setAttribute('for', `img${i}`);
  289. imguploadlabel.classList.add('upload-btn');
  290. imguploadlabel.textContent = '上傳檔案';
  291. imgInputs.appendChild(imguploadlabel);
  292. $('input[type=file]').on('change', prepareUpload);
  293. }
  294. $('.owl-carousel').owlCarousel({
  295. loop:true,
  296. margin:10,
  297. nav: false,
  298. mouseDrag: true,
  299. touchDrag: true,
  300. smartSpeed: 1000,
  301. autoplay: true,
  302. autoplayTimeout: 8000,
  303. autoplayHoverPause: false,
  304. responsive:{
  305. 0:{
  306. items:1
  307. },
  308. 600:{
  309. items:2
  310. },
  311. 1000:{
  312. items:4
  313. }
  314. }
  315. })