spa.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.closest('.pop-out, .pop-out *')) {
  47. const card = event.target.closest('.card');
  48. card.classList.toggle('popped-out');
  49. if (card.classList.contains('popped-out')) {
  50. // 設置彈出狀態的樣式
  51. card.style.position = 'absolute';
  52. card.style.zIndex = '3'; // 確保彈出卡片在其他卡片上方,但在添加圖片模態窗口下方
  53. card.style.left = '50%'; // 可以調整位置以適應需求
  54. card.style.top = '50%';
  55. card.style.transform = 'translate(-50%, -50%)'; // 居中
  56. } else {
  57. // 復原到正常狀態的樣式
  58. card.style.position = 'absolute';
  59. card.style.zIndex = '2';
  60. }
  61. }
  62. // 根據點擊的是哪個按鈕,執行相應的操作
  63. if (event.target.matches('.delete, .delete *')) { // 匹配刪除按鈕或其內部的元素
  64. pckry.remove(card);
  65. card.remove();
  66. pckry.layout();
  67. } else if (event.target.matches('.zoom-out, .zoom-out *')) { // 匹配縮小按鈕或其內部的元素
  68. // ...縮小操作...
  69. const currentSize = parseInt(card.classList[1].split('-')[2]);
  70. const newSize = Math.max(currentSize - 1, 1); // 確保尺寸不會小於 1
  71. // 更新卡片的尺寸類別
  72. card.className = card.className.replace(`card-size-${currentSize}`, `card-size-${newSize}`);
  73. pckry.layout(); // 通知 Packery 重新佈局
  74. } else if (event.target.matches('.zoom-in, .zoom-in *')) { // 匹配放大按鈕或其內部的元素
  75. // ...放大操作...
  76. const currentSize = parseInt(card.classList[1].split('-')[2]);
  77. const newSize = Math.min(currentSize + 1, 4); // 確保尺寸不會大於 4
  78. // 更新卡片的尺寸類別
  79. card.className = card.className.replace(`card-size-${currentSize}`, `card-size-${newSize}`);
  80. pckry.layout(); // 通知 Packery 重新佈局
  81. } else if (event.target.matches('.move-left, .move-left *')) { // 匹配向左移動按鈕或其內部的元素
  82. // ...向左移動操作...
  83. const previousCard = card.previousElementSibling;
  84. if (previousCard) {
  85. // 將卡片移動到前一個卡片之前
  86. cardholder.insertBefore(card, previousCard);
  87. pckry.reloadItems(); // 重新加載 Packery 的項目
  88. pckry.layout(); // 重新佈局
  89. }
  90. } else if (event.target.matches('.move-right, .move-right *')) { // 匹配向右移動按鈕或其內部的元素
  91. // ...向右移動操作...
  92. const nextCard = card.nextElementSibling;
  93. if (nextCard) {
  94. // 將卡片移動到下一個卡片之後
  95. cardholder.insertBefore(nextCard, card);
  96. pckry.reloadItems(); // 重新加載 Packery 的項目
  97. pckry.layout(); // 重新佈局
  98. }
  99. }
  100. });
  101. }
  102. function showAddImageForm() {
  103. var modal = document.getElementById('addImageModal');
  104. modal.style.display = "block";
  105. var closeButton = document.querySelector('.close');
  106. closeButton.onclick = function () {
  107. modal.style.display = "none";
  108. }
  109. // 當用戶點擊模態視窗以外的地方,關閉它
  110. window.onclick = function (event) {
  111. if (event.target == modal) {
  112. modal.style.display = "none";
  113. }
  114. }
  115. }
  116. function makeAllCardsDraggable() {
  117. // 對現有的每個卡片使用 Draggabilly
  118. var draggableElems = pckry.getItemElements();
  119. for (var i = 0, len = draggableElems.length; i < len; i++) {
  120. var draggableElem = draggableElems[i];
  121. var draggie = new Draggabilly(draggableElem);
  122. pckry.bindDraggabillyEvents(draggie);
  123. }
  124. }
  125. function hideAddImageForm() {
  126. var modal = document.getElementById('addImageModal');
  127. modal.style.display = "none";
  128. }
  129. function addImage(event) {
  130. handleAddImage(event, true);
  131. }
  132. function addMoreImage(event) {
  133. handleAddImage(event, false);
  134. }
  135. function handleAddImage(event, closeModal) {
  136. event.preventDefault();
  137. let imageUrl = document.getElementById('newPicUrl').value.trim();
  138. if (!imageUrl) {
  139. console.log('URL is empty. No action taken.');
  140. return;
  141. }
  142. if (!imageUrl.startsWith('http://') && !imageUrl.startsWith('https://')) {
  143. imageUrl = 'https://' + imageUrl;
  144. console.log('URL corrected to: ', imageUrl);
  145. }
  146. addImageWithUrl(imageUrl);
  147. document.getElementById('newPicUrl').value = '';
  148. if (closeModal) {
  149. hideAddImageForm()
  150. }
  151. }
  152. function clearAllCards() {
  153. // 移除所有 .card 元素
  154. var cards = document.querySelectorAll('.card');
  155. cards.forEach(function (card) {
  156. pckry.remove(card); // 通知 Packery 移除元素
  157. card.remove(); // 從 DOM 中移除元素
  158. });
  159. // 通知 Packery 重新佈局剩下的元素
  160. pckry.layout();
  161. }
  162. function addMeme() {
  163. getRandomMemeGif(function (gifUrl) {
  164. // 這裡我們直接調用 addImage 函數,將 Giphy 圖片 URL 作為參數
  165. addImageWithUrl(gifUrl);
  166. });
  167. }
  168. function seedCards() {
  169. imageSeeds.forEach(seed => {
  170. addImageWithUrl(seed.url);
  171. });
  172. }
  173. // 撤銷/重做功能的實現可以根據你的需求設計
  174. // 將 YOUR_API_KEY 替換成你的 Giphy API 密鑰
  175. function getRandomMemeGif(callback) {
  176. const apiKey = 'SHyO3XJY0R71LuTQlx2SlsRuvTrKBjxD';
  177. const apiUrl = `https://api.giphy.com/v1/gifs/random?api_key=${apiKey}&tag=meme`;
  178. fetch(apiUrl)
  179. .then(response => response.json())
  180. .then(data => {
  181. const gifUrl = data.data.images.original.url;
  182. if (callback) callback(gifUrl);
  183. })
  184. .catch(error => {
  185. console.error('Error fetching random meme gif:', error);
  186. });
  187. }
  188. function addImageWithUrl(imageUrl) {
  189. const card = document.createElement('div');
  190. card.className = 'card';
  191. // 隨機選擇一個大小
  192. const sizeClass = 'card-size-' + (Math.floor(Math.random() * 4) + 1);
  193. card.classList.add(sizeClass);
  194. // 創建按鈕容器
  195. const buttonsContainer = document.createElement('div');
  196. buttonsContainer.className = 'card-buttons';
  197. // 創建按鈕組
  198. const buttonGroupVertical = document.createElement('div');
  199. buttonGroupVertical.className = 'button-group vertical';
  200. const buttonGroupHorizontal = document.createElement('div');
  201. buttonGroupHorizontal.className = 'button-group horizontal';
  202. // 創建刪除按鈕
  203. const deleteBtn = document.createElement('button');
  204. deleteBtn.className = 'button delete';
  205. deleteBtn.innerHTML = '<i class="ti ti-trash"></i>';
  206. // 創建放大按鈕
  207. const zoomInBtn = document.createElement('button');
  208. zoomInBtn.className = 'button zoom-in';
  209. zoomInBtn.innerHTML = '<i class="ti ti-arrows-maximize"></i>';
  210. // 創建縮小按鈕
  211. const zoomOutBtn = document.createElement('button');
  212. zoomOutBtn.className = 'button zoom-out';
  213. zoomOutBtn.innerHTML = '<i class="ti ti-arrows-minimize"></i>';
  214. // 向按鈕組中添加按鈕
  215. buttonGroupVertical.appendChild(deleteBtn);
  216. buttonGroupVertical.appendChild(zoomInBtn);
  217. buttonGroupVertical.appendChild(zoomOutBtn);
  218. // 創建向左移動按鈕
  219. const moveLeftBtn = document.createElement('button');
  220. moveLeftBtn.className = 'button move-left';
  221. moveLeftBtn.innerHTML = '<i class="ti ti-arrow-left"></i>';
  222. // 創建向右移動按鈕
  223. const moveRightBtn = document.createElement('button');
  224. moveRightBtn.className = 'button move-right';
  225. moveRightBtn.innerHTML = '<i class="ti ti-arrow-right"></i>';
  226. // 向按鈕組中添加按鈕
  227. buttonGroupHorizontal.appendChild(moveLeftBtn);
  228. buttonGroupHorizontal.appendChild(moveRightBtn);
  229. // 向總按鈕容器添加按鈕組
  230. buttonsContainer.appendChild(buttonGroupVertical);
  231. buttonsContainer.appendChild(buttonGroupHorizontal);
  232. // 設置按鈕組的位置
  233. buttonsContainer.style.justifyContent = 'space-between';
  234. buttonsContainer.style.position = 'absolute';
  235. buttonsContainer.style.top = '0';
  236. buttonsContainer.style.left = '0';
  237. buttonsContainer.style.zIndex = '1';
  238. // 創建彈出按鈕
  239. const popOutBtn = document.createElement('button');
  240. popOutBtn.className = 'button pop-out';
  241. popOutBtn.innerHTML = '<i class="ti ti-layers-subtract"></i>'; // 假設這是彈出圖示
  242. buttonGroupVertical.appendChild(popOutBtn); // 添加到垂直按鈕組
  243. // 創建圖片元素
  244. const img = document.createElement('img');
  245. img.src = imageUrl;
  246. img.alt = 'Mood Image';
  247. img.className = 'mood-image';
  248. img.style.position = 'relative';
  249. img.style.zIndex = '2';
  250. // 當圖片加載完成後,將圖片和按鈕容器添加到卡片
  251. img.onload = function () {
  252. card.appendChild(img);
  253. card.appendChild(buttonsContainer);
  254. // 將卡片添加到卡片容器中
  255. const cardholder = document.querySelector('.cardholder');
  256. cardholder.appendChild(card);
  257. // 通知 Packery 新增了卡片並重新布局
  258. pckry.appended(card);
  259. pckry.layout();
  260. };
  261. }