Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ReviewForm = {};
- jQuery(function ($) {
- ReviewForm = {
- lastClickedCriteriaId: null,
- previousAverageRating: null,
- handleCriteriaRatingSliders: function (prepopulated) {
- $(".criteria-rating-slider").each(function () {
- var elem = $(this),
- id = elem.attr("id").substr(16);
- ReviewForm.toggleCriteriaCommentIndicator($(this));
- elem.slider({
- startValue: 0,
- min: 0,
- max: 5,
- step: 1,
- animate: true,
- orientation: "horizontal",
- value: prepopulated ? $("#criteria-rating-" + id).val() : 0,
- slide: function (e, ui) {
- ReviewForm.updateCriteriaRatingDisplay(ui.value, id)
- },
- change: function (e,
- ui) {
- ReviewForm.drawClippy(elem, ui.value)
- }
- })
- })
- },
- handleOverallRatingSlider: function (prepopulated) {
- $("#overall-slider").slider({
- startValue: 0,
- min: 0,
- max: 10,
- step: 1,
- animate: true,
- orientation: "horizontal",
- value: prepopulated ? $("#overall-rating").val() : 0,
- slide: function (e, ui) {
- $("#overall-rating-label").addClass("set");
- ReviewForm.updateOverallRatingDisplay(ui.value, false)
- }
- })
- },
- updateCriteriaRatingDisplay: function (rating, id) {
- $("#criteria-rating-" + id).val(rating ? rating : "");
- $("#criteria-" + id + " li").each(function (index) {
- if (index < rating) $(this).addClass("fill");
- else $(this).removeClass("fill")
- });
- var text = color = "";
- switch (rating) {
- case 1:
- text = $("#review-form-criteria-labels").data("1");
- color = "red";
- break;
- case 2:
- text = $("#review-form-criteria-labels").data("2");
- color = "red";
- break;
- case 3:
- text = $("#review-form-criteria-labels").data("3");
- color = "yellow";
- break;
- case 4:
- text = $("#review-form-criteria-labels").data("4");
- color = "light-green";
- break;
- case 5:
- text = $("#review-form-criteria-labels").data("5");
- color = "green";
- break;
- default:
- color = "";
- text = $("#review-form-criteria-labels").data("0")
- }
- $("#criteria-display-" + id + " span").removeClass().addClass(color).text(text)
- },
- updateOverallRatingDisplay: function (rating, manual) {
- var ratingText;
- if (isNaN(rating)) rating = 0;
- $("#overall-rating").val(rating).trigger("change");
- if (manual) $("#overall-slider").slider("value", rating);
- $("#overall-rating-container li").each(function (index) {
- if (index < rating) $(this).addClass("fill");
- else $(this).removeClass("fill")
- });
- if (rating === 0) rating = ratingText = "N/A";
- else {
- rating = rating.toString();
- ratingText = rating + " / 10"
- }
- $("#overall-display span").removeClass().text(ratingText).addClass(function () {
- if (rating > 0 && rating < 6) return "red";
- else if (rating >= 6 && rating < 8) return "yellow";
- else if (rating >= 8 && rating < 9) return "light-green";
- else if (rating >= 9) return "green";
- else return "none"
- })
- },
- calculateReviewAverageRating: function () {
- var total = 0,
- items = $(".criteria-rating-value:not([value=]):not([value=0])").length,
- avg = 0;
- $(".criteria-rating-value:not([value=])").each(function () {
- var rating = $(this).val();
- if (rating != "none") total += parseInt(rating)
- });
- avg = total / items;
- avg = Math.round(avg / 0.5) * 0.5;
- if (avg < 0.5 && avg > 0) avg = 0.5;
- var avgStr = avg.toString();
- if (avgStr.length == 1) avgStr += ".0";
- ReviewForm.previousAverageRating = avgStr.replace(/\./i, "");
- $("#average-rating").removeClass().addClass("rating-" + ReviewForm.previousAverageRating);
- return avg
- },
- drawClippy: function (elem, value) {
- var name = elem.parent().prev().text().toLowerCase(),
- id = elem.attr("id").substr(16),
- pos = parseInt(elem.data("criteriaPosition")),
- total = $("#review-form-criteria-list dt").length,
- rowOffset = elem.parent().position(),
- adjustedTop = rowOffset.top - 36 + "px";
- if (value != 0) {
- if (ReviewForm.lastClickedCriteriaId) {
- $("#criteria-comment-" + ReviewForm.lastClickedCriteriaId).hide();
- ReviewForm.toggleCriteriaCommentIndicator($("#criteria-slider-" + ReviewForm.lastClickedCriteriaId))
- }
- ReviewForm.lastClickedCriteriaId = id;
- $("#criteria-comment-" + id).show();
- if (!$(".clippy").hasClass("active")) $(".clippy").css("top", adjustedTop).animate({
- opacity: 1,
- left: "-15px"
- }, 250, "swing").css("display", "block").addClass("active");
- else if ($(".clippy").hasClass("active")) $(".clippy").animate({
- top: adjustedTop
- }, 220, "swing");
- $(".selected-criteria").text(name);
- $(".selected-value").text(value);
- $(".criteria-comment-textarea").focus()
- } else if ($(".clippy").hasClass("active")) {
- $(".clippy").removeClass("active").animate({
- opacity: 0,
- left: 0
- }, 140);
- $("#criteria-" + id + " .criteria-comment-icon").css("display", "none")
- }
- var averageRating = ReviewForm.calculateReviewAverageRating();
- if (!$("#overall-rating-label").hasClass("set")) ReviewForm.updateOverallRatingDisplay(averageRating * 2, true)
- },
- hideClippy: function () {
- if ($("div.clippy-wrapper textarea:visible").next("span").hasClass("negative")) return true;
- $(".clippy").removeClass("active").fadeOut(140);
- setTimeout(function () {
- $(".clippy").css({
- opacity: 0,
- left: 0
- })
- }, 120);
- $(".criteria-rating-slider").each(function () {
- ReviewForm.toggleCriteriaCommentIndicator($(this))
- })
- },
- toggleCriteriaCommentIndicator: function (elem) {
- var id = elem.attr("id").substr(16),
- comment = $("#criteria-comment-" + id + " textarea").val().length;
- if (comment && $("#criteria-rating-" + id).val() >= 1) $("#criteria-" + id + " .criteria-comment-icon").css("display", "block");
- else $("#criteria-" + id + " .criteria-comment-icon").css("display", "none")
- }
- };
- $(document).on("click", ".write-review", function () {
- var elem = $(this);
- if (elem.hasClass("review-form-loading")) return false;
- if (!$("#product-relationship-have.checked, #product-relationship-had.checked").length) {
- gdgt.notify.error($(this).data("productList"));
- return false
- } else elem.addClass("review-form-loading");
- var reviewFormContainer = $('<div id="publisher-page"><div id="publisher-page-content"></div></div>');
- $.get("/a/write_review/", {
- product_id: $(this).data("productId")
- }, function (response) {
- if (!response || response == " ") {
- gdgt.notify.error();
- return
- }
- elem.removeClass("review-form-loading");
- reviewFormContainer.prependTo("#product-reviews");
- $(".back-to-reviews").insertBefore("#publisher-page").css("display", "inline-block");
- $("#publisher-page-content").append(response);
- $("section.lead, #stacks, #other-products").hide();
- $("#product-reviews #publisher-page").nextAll().hide();
- ReviewForm.handleCriteriaRatingSliders(false);
- ReviewForm.handleOverallRatingSlider();
- $(".rewrite-trigger").rewrite();
- $("#detailed-review-text").resize();
- _kmq.push(["record", "Clicked to write a new user review", {
- "origin": "Product reviews sub-page"
- }])
- });
- leavePageAlert = true;
- return false
- });
- $(document).on("click", ".edit-review", function () {
- var link = $(this);
- if (!$("#product-review").find("#publisher-page").length) {
- var reviewFormContainer = $('<div id="publisher-page"><div id="publisher-page-content"></div></div>');
- $.get(link.data("href"), function (response) {
- if (!response || response == "") gdgt.notify.error();
- else {
- reviewFormContainer.prependTo("#product-review");
- $(".back-to-reviews").insertBefore("#publisher-page").css("display", "inline-block");
- $("#publisher-page-content").append(response);
- $(".col-left, .col-right").hide();
- ReviewForm.handleCriteriaRatingSliders(true);
- ReviewForm.handleOverallRatingSlider(true);
- $("#overall-rating-label").addClass("set");
- ReviewForm.calculateReviewAverageRating();
- var rteAllowed = false;
- if (!navigator.userAgent.match(/iPhone/i) && !navigator.userAgent.match(/iPad/i) && !navigator.userAgent.match(/iPod/i) && !navigator.userAgent.match(/Android/i) && !navigator.userAgent.match(/webOS/i) && !navigator.userAgent.match(/MSIE (\d+\.\d+)/) && !navigator.userAgent.match(/Opera/i)) var rteAllowed = true;
- if ($.browser.mozilla && parseInt($.browser.version, 10) < 4) rteAllowed = false;
- if (rteAllowed) {
- reviewFormContainer.find(".rewrite-trigger").rewrite();
- reviewFormContainer.find(".rewrite-content").eq(0).click()
- } else {
- var elem = $("#detailed-review-text"),
- text = elem.val();
- text = text.replace(/<br[^>]*>/gi, "\n");
- text = text.replace(/<(?:ol|ul)>/gi, "\n\n");
- text = text.replace(/<li>/gi, "* ");
- text = text.replace(/<\/(?:ol|ul|li)>/gi, "\n");
- text = text.replace(/<[a-z!\/?][^>]*>/gi, "");
- elem.val(text)
- }
- $("#detailed-review-text").resize();
- $("#detailed-review-text").trigger("keyup");
- leavePageAlert = true;
- _kmq.push(["record", "Clicked to edit user review"])
- }
- })
- } else {
- $(".col-left, .col-right").hide();
- $("#publisher-page").show();
- $(".back-to-reviews").css("display", "inline-block");
- leavePageAlert = true
- }
- return false
- });
- $(document).on("click", "#product-reviews .review-cancel, #product-reviews .back-to-reviews", function (e) {
- e.preventDefault();
- var link = $(this);
- $("#cancel-review-dialog").dialog({
- width: 607,
- modal: true,
- draggable: false,
- resizable: false,
- open: function (e, ui) {
- var dialogElem = $(this);
- $(document).on("click", ".ui-dialog .approve-dialog", function (e) {
- e.preventDefault();
- $(".product-review-preview").remove();
- $("section.lead, #stacks, #other-products").show();
- $("#product-reviews #publisher-page").nextAll().show();
- $("#publisher-page").remove();
- $(".back-to-reviews").hide();
- dialogElem.dialog("close");
- leavePageAlert = false;
- _kmq.push(["record", "Exited review form on product reviews sub-page"])
- });
- $(document).on("click", ".ui-widget-overlay, .ui-dialog .cancel-delete, .ui-dialog .cancel-dialog", function (e) {
- e.preventDefault();
- dialogElem.dialog("close")
- })
- },
- close: function (e, ui) {
- $(this).dialog("destroy")
- },
- show: {
- effect: "fade",
- duration: 200
- },
- hide: {
- effect: "fade",
- duration: 160
- }
- })
- });
- $(document).on("click focus", "#detailed-review-text", function () {
- ReviewForm.hideClippy()
- });
- $(window).mousedown(function (e) {
- if ($(".clippy").is(":visible")) {
- var detectClickOnCard = $(e.target).parents("#review-form-criteria").length;
- if (!detectClickOnCard) ReviewForm.hideClippy()
- }
- });
- $(document).keyup(function (e) {
- if ($(".clippy").is(":visible") && e.keyCode == 27) ReviewForm.hideClippy()
- });
- $(document).on("click", "#review-form-criteria-list dt", function () {
- var elem = $(this).next().find(".criteria-rating-slider"),
- value = $(this).next().find(".criteria-rating-value").val();
- ReviewForm.drawClippy(elem, value);
- _kmq.push(["record", "Showed Clippy on criteria name click"])
- });
- $(document).on("click", ".criteria-comment-icon, .comment-error", function () {
- var elem = $(this).parent().find(".criteria-rating-slider"),
- value = $(this).parent().find(".criteria-rating-value").val();
- ReviewForm.drawClippy(elem, value);
- _kmq.push(["record", "Showed Clippy on criteria comment indicator click"])
- });
- $(document).on("focus keyup", ".criteria-comment-textarea", function () {
- Core.countCharacters($(this), $("#" + $(this).data("counterId")), $(".review-submit, .review-preview"),
- 160)
- })
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement