From 92fefa5476c773135d60e00fa805d7d248253c05 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 29 Jul 2022 11:37:49 -0400 Subject: [PATCH] Update comments.js to allow for de-selection of stars in cases where a user might accidentally click on a stars input and want to revert to no-selection. --- wire/modules/Fieldtype/FieldtypeComments/comments.js | 4 +++- wire/modules/Fieldtype/FieldtypeComments/comments.min.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeComments/comments.js b/wire/modules/Fieldtype/FieldtypeComments/comments.js index 4c7b8eb4..2125240b 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/comments.js +++ b/wire/modules/Fieldtype/FieldtypeComments/comments.js @@ -85,6 +85,8 @@ function CommentFormStars() { var value = parseInt(jQuery(this).attr('data-value')); var $parent = jQuery(this).parent(); var $input = $parent.prev('input'); + var valuePrev = parseInt($input.val()); + if(value === valuePrev) value = 0; // click on current value to unset $input.val(value).attr('value', value); // redundancy intended, val() not working on webkit mobile for some reason setStars($parent, value); $input.change(); @@ -283,4 +285,4 @@ function CommentFormInit() { */ jQuery(document).ready(function() { CommentFormInit(); -}); \ No newline at end of file +}); diff --git a/wire/modules/Fieldtype/FieldtypeComments/comments.min.js b/wire/modules/Fieldtype/FieldtypeComments/comments.min.js index 1e4f4f63..097f7ac7 100644 --- a/wire/modules/Fieldtype/FieldtypeComments/comments.min.js +++ b/wire/modules/Fieldtype/FieldtypeComments/comments.min.js @@ -1 +1 @@ -function CommentFormSetCookie(name,value,days){var cookieValue=name+"="+escape(value)+";path=/";if(days==null)days=0;if(days>0){var today=new Date;var expire=new Date;expire.setTime(today.getTime()+36e5*24*days);document.cookie=cookieValue+";expires="+expire.toGMTString()}else{document.cookie=cookieValue}}function CommentFormGetCookie(name){var regex=new RegExp("[; ]"+name+"=([^\\s;]*)");var match=(" "+document.cookie).match(regex);if(name&&match)return unescape(match[1]);return""}function CommentFormStars(){function decodeEntities(encodedString){if(encodedString.indexOf("&")==-1)return encodedString;var textarea=document.createElement("textarea");textarea.innerHTML=encodedString;return textarea.value}function setStars($parent,star){var onClass,offClass,starOn,starOff;onClass=$parent.attr("data-onclass");offClass=$parent.attr("data-offclass");starOn=$parent.attr("data-on");if(typeof onClass=="undefined")onClass="CommentStarOff";if(typeof offClass=="undefined")offClass="CommentStarOff";if(typeof starOn!="undefined"){starOff=$parent.attr("data-off");starOn=decodeEntities(starOn);starOff=decodeEntities(starOff)}else{starOn="";starOff=""}$parent.children("span").each(function(){var val=parseInt(jQuery(this).attr("data-value"));if(val<=star){if(starOn.length)jQuery(this).html(starOn);jQuery(this).addClass(onClass).removeClass(offClass)}else{if(starOff.length)jQuery(this).html(starOff);jQuery(this).removeClass(onClass).addClass(offClass)}})}jQuery(".CommentFormStars input").hide();jQuery(document).on("click",".CommentStarsInput span",function(e){var value=parseInt(jQuery(this).attr("data-value"));var $parent=jQuery(this).parent();var $input=$parent.prev("input");$input.val(value).attr("value",value);setStars($parent,value);$input.change();return false})}function CommentActionReplyClick(){var $this=jQuery(this);var $item=$this.closest(".CommentListItem");var $form=$this.parent().next("form.CommentForm");var commentID=$item.attr("data-comment");if($form.length==0){$form=$item.find(".CommentForm"+commentID)}if($form.length==0){$form=jQuery("#CommentForm form").clone().removeAttr("id");$form.addClass("CommentForm"+commentID);$form.hide().find(".CommentFormParent").val(commentID);var $formPlaceholder=$item.find("form:not(.CommentFormReply):eq(0)");if($formPlaceholder.length){$formPlaceholder.replaceWith($form)}else{$this.parent().after($form)}$form.addClass("CommentFormReply");if($form.is("form[hidden]")){$form.removeAttr("hidden")}else if(!$form.is(":visible")){$form.slideDown()}$form.trigger("CommentFormReplyAdd");$form.trigger("CommentFormReplyShow")}else if(!$form.is(":visible")){$form.slideDown();$form.trigger("CommentFormReplyShow")}else{$form.slideUp();$form.trigger("CommentFormReplyHide")}return false}function CommentActionRepliesClick(){var $this=jQuery(this);var href=$this.attr("href");var $list=$this.closest(".CommentListItem").find(href);if($list.is(":hidden")){$list.removeAttr("hidden")}else{$list.attr("hidden",true)}return false}function CommentFormSubmitClick(){var $this=jQuery(this);var $form=$this.closest("form.CommentForm");var $wrapStars=$form.find(".CommentFormStarsRequired");if($wrapStars.length){var stars=parseInt($wrapStars.find("input").val());if(!stars){alert($wrapStars.attr("data-note"));return false}}var cite=$form.find(".CommentFormCite input").val();var email=$form.find(".CommentFormEmail input").val();var $website=$form.find(".CommentFormWebsite input");var website=$website.length>0?$website.val():"";var $notify=$form.find(".CommentFormNotify :checked");var notify=$notify.length>0?$notify.val():"";if(cite.indexOf("|")>-1)cite="";if(email.indexOf("|")>-1)email="";if(website.indexOf("|")>-1)website="";var cookieValue=cite+"|"+email+"|"+website+"|"+notify;CommentFormSetCookie("CommentForm",cookieValue,0)}function CommentFormCookies(){var $form=jQuery("form.CommentForm");if(!$form.length)return;var cookieValue=CommentFormGetCookie("CommentForm");if(cookieValue.length<1)return;var values=cookieValue.split("|");$form.find(".CommentFormCite input").val(values[0]);$form.find(".CommentFormEmail input").val(values[1]);$form.find(".CommentFormWebsite input").val(values[2]);$form.find(".CommentFormNotify :input[value='"+values[3]+"']").prop("checked",true)}function CommentFormUpvoteDownvote(){var voting=false;jQuery(".CommentActionUpvote, .CommentActionDownvote").on("click",function(){if(voting)return false;voting=true;var $a=jQuery(this);jQuery.getJSON($a.attr("data-url"),function(data){if("success"in data){if(data.success){var $votes=$a.closest(".CommentVotes");$votes.find(".CommentUpvoteCnt").text(data.upvotes);$votes.find(".CommentDownvoteCnt").text(data.downvotes);$a.addClass("CommentVoted")}else if(data.message.length){alert(data.message)}}else{voting=false;return true}voting=false});return false})}function CommentFormInit(){jQuery(".CommentActionReply").on("click",CommentActionReplyClick);jQuery(".CommentActionReplies").on("click",CommentActionRepliesClick);jQuery(".CommentFormSubmit button").on("click",CommentFormSubmitClick);CommentFormCookies();CommentFormUpvoteDownvote();if(jQuery(".CommentStarsInput").length)CommentFormStars()}jQuery(document).ready(function(){CommentFormInit()}); \ No newline at end of file +function CommentFormSetCookie(name,value,days){var cookieValue=name+"="+escape(value)+";path=/";if(days==null)days=0;if(days>0){var today=new Date;var expire=new Date;expire.setTime(today.getTime()+36e5*24*days);document.cookie=cookieValue+";expires="+expire.toGMTString()}else{document.cookie=cookieValue}}function CommentFormGetCookie(name){var regex=new RegExp("[; ]"+name+"=([^\\s;]*)");var match=(" "+document.cookie).match(regex);if(name&&match)return unescape(match[1]);return""}function CommentFormStars(){function decodeEntities(encodedString){if(encodedString.indexOf("&")==-1)return encodedString;var textarea=document.createElement("textarea");textarea.innerHTML=encodedString;return textarea.value}function setStars($parent,star){var onClass,offClass,starOn,starOff;onClass=$parent.attr("data-onclass");offClass=$parent.attr("data-offclass");starOn=$parent.attr("data-on");if(typeof onClass=="undefined")onClass="CommentStarOff";if(typeof offClass=="undefined")offClass="CommentStarOff";if(typeof starOn!="undefined"){starOff=$parent.attr("data-off");starOn=decodeEntities(starOn);starOff=decodeEntities(starOff)}else{starOn="";starOff=""}$parent.children("span").each(function(){var val=parseInt(jQuery(this).attr("data-value"));if(val<=star){if(starOn.length)jQuery(this).html(starOn);jQuery(this).addClass(onClass).removeClass(offClass)}else{if(starOff.length)jQuery(this).html(starOff);jQuery(this).removeClass(onClass).addClass(offClass)}})}jQuery(".CommentFormStars input").hide();jQuery(document).on("click",".CommentStarsInput span",function(e){var value=parseInt(jQuery(this).attr("data-value"));var $parent=jQuery(this).parent();var $input=$parent.prev("input");var valuePrev=parseInt($input.val());if(value===valuePrev)value=0;$input.val(value).attr("value",value);setStars($parent,value);$input.change();return false})}function CommentActionReplyClick(){var $this=jQuery(this);var $item=$this.closest(".CommentListItem");var $form=$this.parent().next("form.CommentForm");var commentID=$item.attr("data-comment");if($form.length==0){$form=$item.find(".CommentForm"+commentID)}if($form.length==0){$form=jQuery("#CommentForm form").clone().removeAttr("id");$form.addClass("CommentForm"+commentID);$form.hide().find(".CommentFormParent").val(commentID);var $formPlaceholder=$item.find("form:not(.CommentFormReply):eq(0)");if($formPlaceholder.length){$formPlaceholder.replaceWith($form)}else{$this.parent().after($form)}$form.addClass("CommentFormReply");if($form.is("form[hidden]")){$form.removeAttr("hidden")}else if(!$form.is(":visible")){$form.slideDown()}$form.trigger("CommentFormReplyAdd");$form.trigger("CommentFormReplyShow")}else if(!$form.is(":visible")){$form.slideDown();$form.trigger("CommentFormReplyShow")}else{$form.slideUp();$form.trigger("CommentFormReplyHide")}return false}function CommentActionRepliesClick(){var $this=jQuery(this);var href=$this.attr("href");var $list=$this.closest(".CommentListItem").find(href);if($list.is(":hidden")){$list.removeAttr("hidden")}else{$list.attr("hidden",true)}return false}function CommentFormSubmitClick(){var $this=jQuery(this);var $form=$this.closest("form.CommentForm");var $wrapStars=$form.find(".CommentFormStarsRequired");if($wrapStars.length){var stars=parseInt($wrapStars.find("input").val());if(!stars){alert($wrapStars.attr("data-note"));return false}}var cite=$form.find(".CommentFormCite input").val();var email=$form.find(".CommentFormEmail input").val();var $website=$form.find(".CommentFormWebsite input");var website=$website.length>0?$website.val():"";var $notify=$form.find(".CommentFormNotify :checked");var notify=$notify.length>0?$notify.val():"";if(cite.indexOf("|")>-1)cite="";if(email.indexOf("|")>-1)email="";if(website.indexOf("|")>-1)website="";var cookieValue=cite+"|"+email+"|"+website+"|"+notify;CommentFormSetCookie("CommentForm",cookieValue,0)}function CommentFormCookies(){var $form=jQuery("form.CommentForm");if(!$form.length)return;var cookieValue=CommentFormGetCookie("CommentForm");if(cookieValue.length<1)return;var values=cookieValue.split("|");$form.find(".CommentFormCite input").val(values[0]);$form.find(".CommentFormEmail input").val(values[1]);$form.find(".CommentFormWebsite input").val(values[2]);$form.find(".CommentFormNotify :input[value='"+values[3]+"']").prop("checked",true)}function CommentFormUpvoteDownvote(){var voting=false;jQuery(".CommentActionUpvote, .CommentActionDownvote").on("click",function(){if(voting)return false;voting=true;var $a=jQuery(this);jQuery.getJSON($a.attr("data-url"),function(data){if("success"in data){if(data.success){var $votes=$a.closest(".CommentVotes");$votes.find(".CommentUpvoteCnt").text(data.upvotes);$votes.find(".CommentDownvoteCnt").text(data.downvotes);$a.addClass("CommentVoted")}else if(data.message.length){alert(data.message)}}else{voting=false;return true}voting=false});return false})}function CommentFormInit(){jQuery(".CommentActionReply").on("click",CommentActionReplyClick);jQuery(".CommentActionReplies").on("click",CommentActionRepliesClick);jQuery(".CommentFormSubmit button").on("click",CommentFormSubmitClick);CommentFormCookies();CommentFormUpvoteDownvote();if(jQuery(".CommentStarsInput").length)CommentFormStars()}jQuery(document).ready(function(){CommentFormInit()}); \ No newline at end of file