spa.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. var pckry;
  2. document.addEventListener('DOMContentLoaded', (event) => {
  3. // 初始化函數
  4. init();
  5. });
  6. function init() {
  7. // 綁定事件
  8. document.querySelector('.buttonAddImage').addEventListener('click', showAddImageForm);
  9. document.querySelector('.buttonCancelAdd').addEventListener('click', hideAddImageForm);
  10. document.querySelector('.buttonApproveAdd').addEventListener('click', addImage);
  11. document.querySelector('.buttonApproveAddMore').addEventListener('click', addMoreImage);
  12. document.querySelector('.buttonClearCards').addEventListener('click', clearAllCards);
  13. document.querySelector('.buttonAddMeme').addEventListener('click', addMeme);
  14. document.querySelector('.buttonSeedCards').addEventListener('click', seedCards);
  15. var cardholder = document.querySelector('.cardholder');
  16. pckry = new Packery(cardholder, {
  17. // Packery 的選項
  18. itemSelector: '.card',
  19. // percentPosition: true, // 如果你想要使用百分比定位
  20. gutter: 1
  21. });
  22. // makeAllCardsDraggable();
  23. // 綁定卡片點擊事件
  24. cardholder.addEventListener('click', function (event) {
  25. const card = event.target.closest('.card');
  26. if (!card) return; // 如果點擊的不是卡片或卡片內的元素,則不進行任何操作
  27. // 如果點擊的是圖片或圖片的父元素(可能包括一些包裝圖片的容器)
  28. if (event.target === card.querySelector('.mood-image') || event.target.parentNode === card.querySelector('.mood-image')) {
  29. // 如果已經有選中的卡片,則取消選中
  30. const selectedCard = document.querySelector('.card-selected');
  31. if (selectedCard && selectedCard !== card) {
  32. selectedCard.classList.remove('card-selected');
  33. const buttons = selectedCard.querySelector('.card-buttons');
  34. if (buttons) {
  35. buttons.style.display = 'none'; // 隱藏按鈕
  36. }
  37. }
  38. // 切換當前卡片的選中狀態
  39. card.classList.toggle('card-selected');
  40. const currentButtons = card.querySelector('.card-buttons');
  41. if (currentButtons) {
  42. currentButtons.style.display = card.classList.contains('card-selected') ? 'flex' : 'none'; // 顯示或隱藏按鈕
  43. }
  44. }
  45. // 根據點擊的是哪個按鈕,執行相應的操作
  46. if (event.target.matches('.delete, .delete *')) { // 匹配刪除按鈕或其內部的元素
  47. pckry.remove(card);
  48. card.remove();
  49. pckry.layout();
  50. } else if (event.target.matches('.zoom-out, .zoom-out *')) { // 匹配縮小按鈕或其內部的元素
  51. // ...縮小操作...
  52. const currentSize = parseInt(card.classList[1].split('-')[2]);
  53. const newSize = Math.max(currentSize - 1, 1); // 確保尺寸不會小於 1
  54. // 更新卡片的尺寸類別
  55. card.className = card.className.replace(`card-size-${currentSize}`, `card-size-${newSize}`);
  56. pckry.layout(); // 通知 Packery 重新佈局
  57. } else if (event.target.matches('.zoom-in, .zoom-in *')) { // 匹配放大按鈕或其內部的元素
  58. // ...放大操作...
  59. const currentSize = parseInt(card.classList[1].split('-')[2]);
  60. const newSize = Math.min(currentSize + 1, 4); // 確保尺寸不會大於 4
  61. // 更新卡片的尺寸類別
  62. card.className = card.className.replace(`card-size-${currentSize}`, `card-size-${newSize}`);
  63. pckry.layout(); // 通知 Packery 重新佈局
  64. } else if (event.target.matches('.move-left, .move-left *')) { // 匹配向左移動按鈕或其內部的元素
  65. // ...向左移動操作...
  66. const previousCard = card.previousElementSibling;
  67. if (previousCard) {
  68. // 將卡片移動到前一個卡片之前
  69. cardholder.insertBefore(card, previousCard);
  70. pckry.reloadItems(); // 重新加載 Packery 的項目
  71. pckry.layout(); // 重新佈局
  72. }
  73. } else if (event.target.matches('.move-right, .move-right *')) { // 匹配向右移動按鈕或其內部的元素
  74. // ...向右移動操作...
  75. const nextCard = card.nextElementSibling;
  76. if (nextCard) {
  77. // 將卡片移動到下一個卡片之後
  78. cardholder.insertBefore(nextCard, card);
  79. pckry.reloadItems(); // 重新加載 Packery 的項目
  80. pckry.layout(); // 重新佈局
  81. }
  82. }
  83. });
  84. }
  85. function showAddImageForm() {
  86. var modal = document.getElementById('addImageModal');
  87. modal.style.display = "block";
  88. var closeButton = document.querySelector('.close');
  89. closeButton.onclick = function () {
  90. modal.style.display = "none";
  91. }
  92. // 當用戶點擊模態視窗以外的地方,關閉它
  93. window.onclick = function (event) {
  94. if (event.target == modal) {
  95. modal.style.display = "none";
  96. }
  97. }
  98. }
  99. function makeAllCardsDraggable() {
  100. // 對現有的每個卡片使用 Draggabilly
  101. var draggableElems = pckry.getItemElements();
  102. for (var i = 0, len = draggableElems.length; i < len; i++) {
  103. var draggableElem = draggableElems[i];
  104. var draggie = new Draggabilly(draggableElem);
  105. pckry.bindDraggabillyEvents(draggie);
  106. }
  107. }
  108. function hideAddImageForm() {
  109. var modal = document.getElementById('addImageModal');
  110. modal.style.display = "none";
  111. }
  112. function addImage(event) {
  113. event.preventDefault();
  114. let imageUrl = document.getElementById('newPicUrl').value.trim();
  115. if (!imageUrl) {
  116. // 如果 URL 是空的,直接返回並且不進行任何操作
  117. console.log('URL is empty. No action taken.');
  118. return;
  119. }
  120. if (!imageUrl.startsWith('http://') && !imageUrl.startsWith('https://')) {
  121. // 如果不是,則默默地為用戶添加 http://
  122. imageUrl = 'https://' + imageUrl;
  123. console.log('URL corrected to: ', imageUrl);
  124. }
  125. // 添加圖片
  126. addImageWithUrl(imageUrl);
  127. // 清空輸入欄位並隱藏添加圖片表單
  128. document.getElementById('newPicUrl').value = '';
  129. hideAddImageForm();
  130. }
  131. function addMoreImage(event) {
  132. event.preventDefault();
  133. let imageUrl = document.getElementById('newPicUrl').value.trim();
  134. if (!imageUrl) {
  135. console.log('URL is empty. No action taken.');
  136. return;
  137. }
  138. if (!imageUrl.startsWith('http://') && !imageUrl.startsWith('https://')) {
  139. imageUrl = 'http://' + imageUrl;
  140. console.log('URL corrected to: ', imageUrl);
  141. }
  142. addImageWithUrl(imageUrl);
  143. document.getElementById('newPicUrl').value = '';
  144. }
  145. function clearAllCards() {
  146. // 移除所有 .card 元素
  147. var cards = document.querySelectorAll('.card');
  148. cards.forEach(function (card) {
  149. pckry.remove(card); // 通知 Packery 移除元素
  150. card.remove(); // 從 DOM 中移除元素
  151. });
  152. // 通知 Packery 重新佈局剩下的元素
  153. pckry.layout();
  154. }
  155. function addMeme() {
  156. getRandomMemeGif(function (gifUrl) {
  157. // 這裡我們直接調用 addImage 函數,將 Giphy 圖片 URL 作為參數
  158. addImageWithUrl(gifUrl);
  159. });
  160. }
  161. function seedCards() {
  162. imageSeeds.forEach(seed => {
  163. addImageWithUrl(seed.url);
  164. });
  165. }
  166. // 撤銷/重做功能的實現可以根據你的需求設計
  167. // 將 YOUR_API_KEY 替換成你的 Giphy API 密鑰
  168. function getRandomMemeGif(callback) {
  169. const apiKey = 'SHyO3XJY0R71LuTQlx2SlsRuvTrKBjxD';
  170. const apiUrl = `https://api.giphy.com/v1/gifs/random?api_key=${apiKey}&tag=meme`;
  171. fetch(apiUrl)
  172. .then(response => response.json())
  173. .then(data => {
  174. const gifUrl = data.data.images.original.url;
  175. if (callback) callback(gifUrl);
  176. })
  177. .catch(error => {
  178. console.error('Error fetching random meme gif:', error);
  179. });
  180. }
  181. function addImageWithUrl(imageUrl) {
  182. const card = document.createElement('div');
  183. card.className = 'card';
  184. // 隨機選擇一個大小
  185. const sizeClass = 'card-size-' + (Math.floor(Math.random() * 4) + 1);
  186. card.classList.add(sizeClass);
  187. // 創建按鈕容器
  188. const buttonsContainer = document.createElement('div');
  189. buttonsContainer.className = 'card-buttons';
  190. // 創建按鈕組
  191. const buttonGroupVertical = document.createElement('div');
  192. buttonGroupVertical.className = 'button-group vertical';
  193. const buttonGroupHorizontal = document.createElement('div');
  194. buttonGroupHorizontal.className = 'button-group horizontal';
  195. // 創建刪除按鈕
  196. const deleteBtn = document.createElement('button');
  197. deleteBtn.className = 'button delete';
  198. deleteBtn.innerHTML = '<i class="ti ti-trash"></i>';
  199. // 創建放大按鈕
  200. const zoomInBtn = document.createElement('button');
  201. zoomInBtn.className = 'button zoom-in';
  202. zoomInBtn.innerHTML = '<i class="ti ti-arrows-maximize"></i>';
  203. // 創建縮小按鈕
  204. const zoomOutBtn = document.createElement('button');
  205. zoomOutBtn.className = 'button zoom-out';
  206. zoomOutBtn.innerHTML = '<i class="ti ti-arrows-minimize"></i>';
  207. // 向按鈕組中添加按鈕
  208. buttonGroupVertical.appendChild(deleteBtn);
  209. buttonGroupVertical.appendChild(zoomInBtn);
  210. buttonGroupVertical.appendChild(zoomOutBtn);
  211. // 創建向左移動按鈕
  212. const moveLeftBtn = document.createElement('button');
  213. moveLeftBtn.className = 'button move-left';
  214. moveLeftBtn.innerHTML = '<i class="ti ti-arrow-left"></i>';
  215. // 創建向右移動按鈕
  216. const moveRightBtn = document.createElement('button');
  217. moveRightBtn.className = 'button move-right';
  218. moveRightBtn.innerHTML = '<i class="ti ti-arrow-right"></i>';
  219. // 向按鈕組中添加按鈕
  220. buttonGroupHorizontal.appendChild(moveLeftBtn);
  221. buttonGroupHorizontal.appendChild(moveRightBtn);
  222. // 向總按鈕容器添加按鈕組
  223. buttonsContainer.appendChild(buttonGroupVertical);
  224. buttonsContainer.appendChild(buttonGroupHorizontal);
  225. // 設置按鈕組的位置
  226. buttonsContainer.style.justifyContent = 'space-between';
  227. buttonsContainer.style.position = 'absolute';
  228. buttonsContainer.style.top = '0';
  229. buttonsContainer.style.left = '0';
  230. buttonsContainer.style.zIndex = '1';
  231. // 創建圖片元素
  232. const img = document.createElement('img');
  233. img.src = imageUrl;
  234. img.alt = 'Mood Image';
  235. img.className = 'mood-image';
  236. img.style.position = 'relative';
  237. img.style.zIndex = '2';
  238. // 當圖片加載完成後,將圖片和按鈕容器添加到卡片
  239. img.onload = function () {
  240. card.appendChild(img);
  241. card.appendChild(buttonsContainer);
  242. // 將卡片添加到卡片容器中
  243. const cardholder = document.querySelector('.cardholder');
  244. cardholder.appendChild(card);
  245. // 通知 Packery 新增了卡片並重新布局
  246. pckry.appended(card);
  247. pckry.layout();
  248. };
  249. }