Изменения документа Загрузить документ

Редактировал(а) HRlink 30.05.2025

От версии 79.1
отредактировано HRlink
на 30.05.2025
Изменить комментарий: К данной версии нет комментариев
К версии 78.1
отредактировано HRlink
на 30.05.2025
Изменить комментарий: К данной версии нет комментариев

Сводка

Подробности

Свойства страницы
Содержимое
... ... @@ -145,13 +145,13 @@
145 145  ----
146 146  
147 147  {{velocity}}
148 -#set($likesDocName = "XWiki.LikesStorage." + $doc.fullName.replaceAll("[^a-zA-Z0-9]", "_"))
149 -#set($likesDoc = $xwiki.getDocument($likesDocName))
150 -#set($likesObject = $likesDoc.getObject("XWiki.LikesStorageClass"))
148 +#set($likesKey = $doc.fullName)
149 +#set($likesDoc = $xwiki.getDocument("XWiki.LikesStorage"))
150 +#set($likesObject = $likesDoc.getObject("XWiki.LikesStorageClass", "key", $likesKey))
151 151  
152 152  #if(!$likesObject)
153 153   #set($likesObject = $likesDoc.newObject("XWiki.LikesStorageClass"))
154 - $likesObject.set("key", $doc.fullName)
154 + $likesObject.set("key", $likesKey)
155 155   $likesObject.set("value", 0)
156 156   $xwiki.saveDocument($likesDoc)
157 157  #end
... ... @@ -163,18 +163,25 @@
163 163  #end
164 164  
165 165  #set($likes = $likesObject.get("value"))
166 +#set($liked = $request.getCookie("liked-$likesKey"))
167 +#set($userLiked = false)
168 +#if("$!liked" == "true")
169 + #set($userLiked = true)
170 +#end
171 +
166 166  {{html clean="false"}}
167 167  <div id="like-container" style="margin-top:20px; display:flex; align-items:center; gap:10px;">
168 - <button id="like-button" style="cursor:pointer; font-size: 22px; background: none; border: none;" title="Лайк">
169 - 👍
174 + <button id="like-button" style="cursor:pointer; font-size: 20px; background: none; border: none;" title="Нравится">
175 + <span id="heart" style="color:#e74c3c;">❤️</span>
170 170   </button>
171 171   <span id="like-count" style="font-size:18px;">$likes</span>
172 - <span style="font-size:14px; color:#555;">Нравится статья?</span>
178 + <span style="font-size:14px; color: #555;">Нравится статья?</span>
173 173  </div>
174 174  <script>
175 175   document.addEventListener('DOMContentLoaded', function () {
176 - const key = "$doc.fullName";
182 + const key = "$likesKey";
177 177   const likeButton = document.getElementById('like-button');
184 + const likeCount = document.getElementById('like-count');
178 178  
179 179   function setCookie(name, value, days) {
180 180   const d = new Date();
... ... @@ -195,8 +195,14 @@
195 195  
196 196   likeButton.addEventListener('click', function () {
197 197   if (getCookie("liked-" + key) !== "true") {
198 - setCookie("liked-" + key, "true", 365);
199 - window.location.href = window.location.href + (window.location.href.indexOf('?') > -1 ? '&' : '?') + "like=1";
205 + fetch(window.location.href + "?like=1", { method: 'GET' })
206 + .then(() => {
207 + let current = parseInt(likeCount.innerText);
208 + likeCount.innerText = current + 1;
209 + setCookie("liked-" + key, "true", 365);
210 + likeButton.disabled = true;
211 + likeButton.style.opacity = 0.6;
212 + });
200 200   }
201 201   });
202 202   });