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

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

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

Сводка

Подробности

Свойства страницы
Содержимое
... ... @@ -144,19 +144,24 @@
144 144  
145 145  ----
146 146  
147 -{{velocity}}
148 -#set($likesKey = $doc.fullName)
147 + {{velocity}}#set($likesKey = $doc.fullName)
148 +#set($likesDocRef = $services.model.resolveDocument("XWiki.LikesStorage"))
149 149  #set($likesDoc = $xwiki.getDocument("XWiki.LikesStorage"))
150 150  #set($likesObject = $likesDoc.getObject("XWiki.LikesStorageClass", "key", $likesKey))
151 151  
152 +## Создание объекта, если ещё нет
152 152  #if(!$likesObject)
153 - #set($likesObject = $likesDoc.newObject("XWiki.LikesStorageClass"))
154 - $likesObject.set("key", $likesKey)
155 - $likesObject.set("value", 0)
154 + #set($likesDoc = $xwiki.getDocument("XWiki.LikesStorage"))
155 + #set($obj = $likesDoc.newObject("XWiki.LikesStorageClass"))
156 + $obj.set("key", $likesKey)
157 + $obj.set("value", 0)
156 156   $xwiki.saveDocument($likesDoc)
159 + #set($likes = 0)
160 +#else
161 + #set($likes = $likesObject.get("value"))
157 157  #end
158 158  
159 -#set($likes = $likesObject.get("value"))
164 +## Проверка, ставил ли пользователь лайк через cookie
160 160  #set($liked = $request.getCookie("liked-$likesKey"))
161 161  #set($userLiked = false)
162 162  #if("$!liked" == "true")
... ... @@ -163,15 +163,15 @@
163 163   #set($userLiked = true)
164 164  #end
165 165  
171 +## HTML-блок
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: 20px; background: none; border: none;" title="Нравится" aria-label="Лайк">
174 + <button id="like-button" style="cursor:pointer; font-size: 20px; background: none; border: none;" title="Нравится">
169 169   <span id="heart" style="color:#e74c3c;">❤️</span>
170 170   </button>
171 171   <span id="like-count" style="font-size:18px;">$likes</span>
172 172   <span style="font-size:14px; color: #555;">Нравится статья?</span>
173 173  </div>
174 -
175 175  <script>
176 176   document.addEventListener('DOMContentLoaded', function () {
177 177   const key = "$likesKey";
... ... @@ -197,14 +197,17 @@
197 197  
198 198   likeButton.addEventListener('click', function () {
199 199   if (getCookie("liked-" + key) !== "true") {
200 - fetch(window.location.href + "?like=1", { method: 'GET' })
201 - .then(() => {
202 - let current = parseInt(likeCount.innerText);
203 - likeCount.innerText = current + 1;
204 - setCookie("liked-" + key, "true", 365);
205 - likeButton.disabled = true;
206 - likeButton.style.opacity = 0.6;
207 - });
205 + fetch(location.href, {
206 + method: 'POST',
207 + headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
208 + body: 'like=1&xredirect=' + encodeURIComponent(window.location.pathname)
209 + }).then(() => {
210 + let current = parseInt(likeCount.innerText);
211 + likeCount.innerText = current + 1;
212 + setCookie("liked-" + key, "true", 365);
213 + likeButton.disabled = true;
214 + likeButton.style.opacity = 0.6;
215 + });
208 208   }
209 209   });
210 210   });
... ... @@ -211,11 +211,67 @@
211 211  </script>
212 212  {{/html}}
213 213  
222 +## Серверная часть для учёта лайка
214 214  #if($request.getParameter("like") == "1")
215 - #set($likes = $likesObject.get("value"))
216 - $likesObject.set("value", $math.add($likes, 1))
224 + #set($likesDoc = $xwiki.getDocument("XWiki.LikesStorage"))
225 + #set($likesObject = $likesDoc.getObject("XWiki.LikesStorageClass", "key", $likesKey))
226 + #set($count = $likesObject.get("value"))
227 + $likesObject.set("value", $math.add($count, 1))
217 217   $xwiki.saveDocument($likesDoc)
218 -#end
219 -{{/velocity}}
229 +#end{{/velocity}}
220 220  
231 +
232 +$xwiki.saveDocument($likesDoc)
233 +
234 +
235 +{{html clean="false"}}
236 +<div id="like-container" style="margin-top:20px; display:flex; align-items:center; gap:10px;">
237 +<button id="like-button" style="cursor:pointer; font-size: 20px; background: none; border: none;" title="Нравится">
238 +<span id="heart" style="color:#e74c3c;">❤️</span>
239 +</button>
240 +<span id="like-count" style="font-size:18px;">0</span>
241 +<span style="font-size:14px; color: #555;">Нравится статья?</span>
242 +</div>
243 +<script>
244 +document.addEventListener('DOMContentLoaded', function () {
245 +const key = "Main.Кадровик.Документы.Загрузить документ .WebHome";
246 +const likeButton = document.getElementById('like-button');
247 +const likeCount = document.getElementById('like-count');
248 +
249 +function setCookie(name, value, days) {
250 +const d = new Date();
251 +d.setTime(d.getTime() + (days*24*60*60*1000));
252 +document.cookie = name + "=" + value + "; expires=" + d.toUTCString() + "; path=/";
253 +}
254 +
255 +function getCookie(name) {
256 +const value = `; ${document.cookie}`;
257 +const parts = value.split(`; ${name}=`);
258 +if (parts.length === 2) return parts.pop().split(';').shift();
259 +}
260 +
261 +if (getCookie("liked-" + key) === "true") {
262 +likeButton.disabled = true;
263 +likeButton.style.opacity = 0.6;
264 +}
265 +
266 +likeButton.addEventListener('click', function () {
267 +if (getCookie("liked-" + key) !== "true") {
268 +fetch(location.href, {
269 +method: 'POST',
270 +headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
271 +body: 'like=1&xredirect=' + encodeURIComponent(window.location.pathname)
272 +}).then(() => {
273 +let current = parseInt(likeCount.innerText);
274 +likeCount.innerText = current + 1;
275 +setCookie("liked-" + key, "true", 365);
276 +likeButton.disabled = true;
277 +likeButton.style.opacity = 0.6;
278 +});
279 +}
280 +});
281 +});
282 +</script>
283 +{{/html}}
284 +
221 221