spa.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. handleAddImage(event, true);
  114. }
  115. function addMoreImage(event) {
  116. handleAddImage(event, false);
  117. }
  118. function handleAddImage(event, closeModal) {
  119. event.preventDefault();
  120. let imageUrl = document.getElementById('newPicUrl').value.trim();
  121. if (!imageUrl) {
  122. console.log('URL is empty. No action taken.');
  123. return;
  124. }
  125. if (!imageUrl.startsWith('http://') && !imageUrl.startsWith('https://')) {
  126. imageUrl = 'https://' + imageUrl;
  127. console.log('URL corrected to: ', imageUrl);
  128. }
  129. addImageWithUrl(imageUrl);
  130. document.getElementById('newPicUrl').value = '';
  131. if (closeModal) {
  132. hideAddImageForm()
  133. }
  134. }
  135. function clearAllCards() {
  136. // 移除所有 .card 元素
  137. var cards = document.querySelectorAll('.card');
  138. cards.forEach(function (card) {
  139. pckry.remove(card); // 通知 Packery 移除元素
  140. card.remove(); // 從 DOM 中移除元素
  141. });
  142. // 通知 Packery 重新佈局剩下的元素
  143. pckry.layout();
  144. }
  145. function addMeme() {
  146. getRandomMemeGif(function (gifUrl) {
  147. // 這裡我們直接調用 addImage 函數,將 Giphy 圖片 URL 作為參數
  148. addImageWithUrl(gifUrl);
  149. });
  150. }
  151. function seedCards() {
  152. imageSeeds.forEach(seed => {
  153. addImageWithUrl(seed.url);
  154. });
  155. }
  156. // 撤銷/重做功能的實現可以根據你的需求設計
  157. // 將 YOUR_API_KEY 替換成你的 Giphy API 密鑰
  158. function getRandomMemeGif(callback) {
  159. const apiKey = 'SHyO3XJY0R71LuTQlx2SlsRuvTrKBjxD';
  160. const apiUrl = `https://api.giphy.com/v1/gifs/random?api_key=${apiKey}&tag=meme`;
  161. fetch(apiUrl)
  162. .then(response => response.json())
  163. .then(data => {
  164. const gifUrl = data.data.images.original.url;
  165. if (callback) callback(gifUrl);
  166. })
  167. .catch(error => {
  168. console.error('Error fetching random meme gif:', error);
  169. });
  170. }
  171. function addImageWithUrl(imageUrl) {
  172. const card = document.createElement('div');
  173. card.className = 'card';
  174. // 隨機選擇一個大小
  175. const sizeClass = 'card-size-' + (Math.floor(Math.random() * 4) + 1);
  176. card.classList.add(sizeClass);
  177. // 創建按鈕容器
  178. const buttonsContainer = document.createElement('div');
  179. buttonsContainer.className = 'card-buttons';
  180. // 創建按鈕組
  181. const buttonGroupVertical = document.createElement('div');
  182. buttonGroupVertical.className = 'button-group vertical';
  183. const buttonGroupHorizontal = document.createElement('div');
  184. buttonGroupHorizontal.className = 'button-group horizontal';
  185. // 創建刪除按鈕
  186. const deleteBtn = document.createElement('button');
  187. deleteBtn.className = 'button delete';
  188. deleteBtn.innerHTML = '<i class="ti ti-trash"></i>';
  189. // 創建放大按鈕
  190. const zoomInBtn = document.createElement('button');
  191. zoomInBtn.className = 'button zoom-in';
  192. zoomInBtn.innerHTML = '<i class="ti ti-arrows-maximize"></i>';
  193. // 創建縮小按鈕
  194. const zoomOutBtn = document.createElement('button');
  195. zoomOutBtn.className = 'button zoom-out';
  196. zoomOutBtn.innerHTML = '<i class="ti ti-arrows-minimize"></i>';
  197. // 向按鈕組中添加按鈕
  198. buttonGroupVertical.appendChild(deleteBtn);
  199. buttonGroupVertical.appendChild(zoomInBtn);
  200. buttonGroupVertical.appendChild(zoomOutBtn);
  201. // 創建向左移動按鈕
  202. const moveLeftBtn = document.createElement('button');
  203. moveLeftBtn.className = 'button move-left';
  204. moveLeftBtn.innerHTML = '<i class="ti ti-arrow-left"></i>';
  205. // 創建向右移動按鈕
  206. const moveRightBtn = document.createElement('button');
  207. moveRightBtn.className = 'button move-right';
  208. moveRightBtn.innerHTML = '<i class="ti ti-arrow-right"></i>';
  209. // 向按鈕組中添加按鈕
  210. buttonGroupHorizontal.appendChild(moveLeftBtn);
  211. buttonGroupHorizontal.appendChild(moveRightBtn);
  212. // 向總按鈕容器添加按鈕組
  213. buttonsContainer.appendChild(buttonGroupVertical);
  214. buttonsContainer.appendChild(buttonGroupHorizontal);
  215. // 設置按鈕組的位置
  216. buttonsContainer.style.justifyContent = 'space-between';
  217. buttonsContainer.style.position = 'absolute';
  218. buttonsContainer.style.top = '0';
  219. buttonsContainer.style.left = '0';
  220. buttonsContainer.style.zIndex = '1';
  221. // 創建圖片元素
  222. const img = document.createElement('img');
  223. img.src = imageUrl;
  224. img.alt = 'Mood Image';
  225. img.className = 'mood-image';
  226. img.style.position = 'relative';
  227. img.style.zIndex = '2';
  228. // 當圖片加載完成後,將圖片和按鈕容器添加到卡片
  229. img.onload = function () {
  230. card.appendChild(img);
  231. card.appendChild(buttonsContainer);
  232. // 將卡片添加到卡片容器中
  233. const cardholder = document.querySelector('.cardholder');
  234. cardholder.appendChild(card);
  235. // 通知 Packery 新增了卡片並重新布局
  236. pckry.appended(card);
  237. pckry.layout();
  238. };
  239. }