1/** 2 * kk Star Ratings 3 * @see https://github.com/kamalkhan/kk-star-ratings 4 */ 5 6"use strict"; 7 8(function (fn) { 9 if (document.readyState != "loading") { 10 return fn(); 11 } 12 13 document.addEventListener("DOMContentLoaded", fn); 14})(function kkStarRatings() { 15 var isBusy = false; 16 17 function post(data, successCallback, errorCallback) { 18 if (isBusy) { 19 return; 20 } 21 22 isBusy = true; 23 24 data = Object.assign( 25 { 26 nonce: kk_star_ratings.nonce, 27 action: kk_star_ratings.action, 28 }, 29 data 30 ); 31 32 var query = []; 33 for (var key in data) { 34 query.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key])); 35 } 36 37 var request = new XMLHttpRequest(); 38 39 request.open("POST", kk_star_ratings.endpoint, true); 40 41 request.onload = function () { 42 if (request.status >= 200 && request.status < 400) { 43 successCallback(request.responseText, request); 44 } else { 45 errorCallback(request.responseText, request); 46 } 47 }; 48 49 request.onloadend = function () { 50 isBusy = false; 51 }; 52 53 request.setRequestHeader( 54 "Content-type", 55 "application/x-www-form-urlencoded; charset=UTF-8" 56 ); 57 58 request.send(query.join("&")); 59 } 60 61 function htmlToEl(html) { 62 var div = document.createElement("div"); 63 div.innerHTML = html.trim(); 64 65 return div.firstChild; 66 } 67 68 function apply($el) { 69 function vote($star) { 70 var data = { 71 rating: $star.getAttribute("data-star"), 72 }; 73 74 var payload = JSON.parse($el.getAttribute("data-payload")); 75 76 for (var key in payload) { 77 data["payload[" + key + "]"] = payload[key]; 78 } 79 80 post( 81 data, 82 function (html) { 83 var $newEl = htmlToEl(html); 84 $el.parentNode.replaceChild($newEl, $el); 85 unmount(); 86 $el = null; 87 apply($newEl); 88 }, 89 console.error 90 ); 91 } 92 93 function onClick(e) { 94 e.preventDefault(); 95 vote(e.currentTarget); 96 } 97 98 var $stars = $el.querySelectorAll("[data-star]"); 99 100 function unmount() { 101 Array.prototype.forEach.call($stars, function ($star) { 102 $star.removeEventListener("click", onClick); 103 }); 104 } 105 106 function mount() { 107 Array.prototype.forEach.call($stars, function ($star) { 108 $star.addEventListener("click", onClick); 109 }); 110 } 111 112 mount(); 113 } 114 115 Array.prototype.forEach.call( 116 document.querySelectorAll(".kk-star-ratings"), 117 apply 118 ); 119});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.