Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ReviewForm = {};
  2. jQuery(function ($) {
  3.     ReviewForm = {
  4.         lastClickedCriteriaId: null,
  5.         previousAverageRating: null,
  6.         handleCriteriaRatingSliders: function (prepopulated) {
  7.             $(".criteria-rating-slider").each(function () {
  8.                 var elem = $(this),
  9.                     id = elem.attr("id").substr(16);
  10.                 ReviewForm.toggleCriteriaCommentIndicator($(this));
  11.                 elem.slider({
  12.                     startValue: 0,
  13.                     min: 0,
  14.                     max: 5,
  15.                     step: 1,
  16.                     animate: true,
  17.                     orientation: "horizontal",
  18.                     value: prepopulated ? $("#criteria-rating-" + id).val() : 0,
  19.                     slide: function (e, ui) {
  20.                         ReviewForm.updateCriteriaRatingDisplay(ui.value, id)
  21.                     },
  22.                     change: function (e,
  23.                     ui) {
  24.                         ReviewForm.drawClippy(elem, ui.value)
  25.                     }
  26.                 })
  27.             })
  28.         },
  29.         handleOverallRatingSlider: function (prepopulated) {
  30.             $("#overall-slider").slider({
  31.                 startValue: 0,
  32.                 min: 0,
  33.                 max: 10,
  34.                 step: 1,
  35.                 animate: true,
  36.                 orientation: "horizontal",
  37.                 value: prepopulated ? $("#overall-rating").val() : 0,
  38.                 slide: function (e, ui) {
  39.                     $("#overall-rating-label").addClass("set");
  40.                     ReviewForm.updateOverallRatingDisplay(ui.value, false)
  41.                 }
  42.             })
  43.         },
  44.         updateCriteriaRatingDisplay: function (rating, id) {
  45.             $("#criteria-rating-" + id).val(rating ? rating : "");
  46.             $("#criteria-" + id + " li").each(function (index) {
  47.                 if (index < rating) $(this).addClass("fill");
  48.                 else $(this).removeClass("fill")
  49.             });
  50.             var text = color = "";
  51.             switch (rating) {
  52.                 case 1:
  53.                     text = $("#review-form-criteria-labels").data("1");
  54.                     color = "red";
  55.                     break;
  56.                 case 2:
  57.                     text = $("#review-form-criteria-labels").data("2");
  58.                     color = "red";
  59.                     break;
  60.                 case 3:
  61.                     text = $("#review-form-criteria-labels").data("3");
  62.                     color = "yellow";
  63.                     break;
  64.                 case 4:
  65.                     text = $("#review-form-criteria-labels").data("4");
  66.                     color = "light-green";
  67.                     break;
  68.                 case 5:
  69.                     text = $("#review-form-criteria-labels").data("5");
  70.                     color = "green";
  71.                     break;
  72.                 default:
  73.                     color = "";
  74.                     text = $("#review-form-criteria-labels").data("0")
  75.             }
  76.             $("#criteria-display-" + id + " span").removeClass().addClass(color).text(text)
  77.         },
  78.         updateOverallRatingDisplay: function (rating, manual) {
  79.             var ratingText;
  80.             if (isNaN(rating)) rating = 0;
  81.             $("#overall-rating").val(rating).trigger("change");
  82.             if (manual) $("#overall-slider").slider("value", rating);
  83.             $("#overall-rating-container li").each(function (index) {
  84.                 if (index < rating) $(this).addClass("fill");
  85.                 else $(this).removeClass("fill")
  86.             });
  87.             if (rating === 0) rating = ratingText = "N/A";
  88.             else {
  89.                 rating = rating.toString();
  90.                 ratingText = rating + " / 10"
  91.             }
  92.             $("#overall-display span").removeClass().text(ratingText).addClass(function () {
  93.                 if (rating > 0 && rating < 6) return "red";
  94.                 else if (rating >= 6 && rating < 8) return "yellow";
  95.                 else if (rating >= 8 && rating < 9) return "light-green";
  96.                 else if (rating >= 9) return "green";
  97.                 else return "none"
  98.             })
  99.         },
  100.         calculateReviewAverageRating: function () {
  101.             var total = 0,
  102.                 items = $(".criteria-rating-value:not([value=]):not([value=0])").length,
  103.                 avg = 0;
  104.             $(".criteria-rating-value:not([value=])").each(function () {
  105.                 var rating = $(this).val();
  106.                 if (rating != "none") total += parseInt(rating)
  107.             });
  108.             avg = total / items;
  109.             avg = Math.round(avg / 0.5) * 0.5;
  110.             if (avg < 0.5 && avg > 0) avg = 0.5;
  111.             var avgStr = avg.toString();
  112.             if (avgStr.length == 1) avgStr += ".0";
  113.             ReviewForm.previousAverageRating = avgStr.replace(/\./i, "");
  114.             $("#average-rating").removeClass().addClass("rating-" + ReviewForm.previousAverageRating);
  115.             return avg
  116.         },
  117.         drawClippy: function (elem, value) {
  118.             var name = elem.parent().prev().text().toLowerCase(),
  119.                 id = elem.attr("id").substr(16),
  120.                 pos = parseInt(elem.data("criteriaPosition")),
  121.                 total = $("#review-form-criteria-list dt").length,
  122.                 rowOffset = elem.parent().position(),
  123.                 adjustedTop = rowOffset.top - 36 + "px";
  124.             if (value != 0) {
  125.                 if (ReviewForm.lastClickedCriteriaId) {
  126.                     $("#criteria-comment-" + ReviewForm.lastClickedCriteriaId).hide();
  127.                     ReviewForm.toggleCriteriaCommentIndicator($("#criteria-slider-" + ReviewForm.lastClickedCriteriaId))
  128.                 }
  129.                 ReviewForm.lastClickedCriteriaId = id;
  130.                 $("#criteria-comment-" + id).show();
  131.                 if (!$(".clippy").hasClass("active")) $(".clippy").css("top", adjustedTop).animate({
  132.                     opacity: 1,
  133.                     left: "-15px"
  134.                 }, 250, "swing").css("display", "block").addClass("active");
  135.                 else if ($(".clippy").hasClass("active")) $(".clippy").animate({
  136.                     top: adjustedTop
  137.                 }, 220, "swing");
  138.                 $(".selected-criteria").text(name);
  139.                 $(".selected-value").text(value);
  140.                 $(".criteria-comment-textarea").focus()
  141.             } else if ($(".clippy").hasClass("active")) {
  142.                 $(".clippy").removeClass("active").animate({
  143.                     opacity: 0,
  144.                     left: 0
  145.                 }, 140);
  146.                 $("#criteria-" + id + " .criteria-comment-icon").css("display", "none")
  147.             }
  148.             var averageRating = ReviewForm.calculateReviewAverageRating();
  149.             if (!$("#overall-rating-label").hasClass("set")) ReviewForm.updateOverallRatingDisplay(averageRating * 2, true)
  150.         },
  151.         hideClippy: function () {
  152.             if ($("div.clippy-wrapper textarea:visible").next("span").hasClass("negative")) return true;
  153.             $(".clippy").removeClass("active").fadeOut(140);
  154.             setTimeout(function () {
  155.                 $(".clippy").css({
  156.                     opacity: 0,
  157.                     left: 0
  158.                 })
  159.             }, 120);
  160.             $(".criteria-rating-slider").each(function () {
  161.                 ReviewForm.toggleCriteriaCommentIndicator($(this))
  162.             })
  163.         },
  164.         toggleCriteriaCommentIndicator: function (elem) {
  165.             var id = elem.attr("id").substr(16),
  166.                 comment = $("#criteria-comment-" + id + " textarea").val().length;
  167.             if (comment && $("#criteria-rating-" + id).val() >= 1) $("#criteria-" + id + " .criteria-comment-icon").css("display", "block");
  168.             else $("#criteria-" + id + " .criteria-comment-icon").css("display", "none")
  169.         }
  170.     };
  171.     $(document).on("click", ".write-review", function () {
  172.         var elem = $(this);
  173.         if (elem.hasClass("review-form-loading")) return false;
  174.         if (!$("#product-relationship-have.checked, #product-relationship-had.checked").length) {
  175.             gdgt.notify.error($(this).data("productList"));
  176.             return false
  177.         } else elem.addClass("review-form-loading");
  178.         var reviewFormContainer = $('<div id="publisher-page"><div id="publisher-page-content"></div></div>');
  179.         $.get("/a/write_review/", {
  180.             product_id: $(this).data("productId")
  181.         }, function (response) {
  182.             if (!response || response == " ") {
  183.                 gdgt.notify.error();
  184.                 return
  185.             }
  186.             elem.removeClass("review-form-loading");
  187.             reviewFormContainer.prependTo("#product-reviews");
  188.             $(".back-to-reviews").insertBefore("#publisher-page").css("display", "inline-block");
  189.             $("#publisher-page-content").append(response);
  190.             $("section.lead, #stacks, #other-products").hide();
  191.             $("#product-reviews #publisher-page").nextAll().hide();
  192.             ReviewForm.handleCriteriaRatingSliders(false);
  193.             ReviewForm.handleOverallRatingSlider();
  194.             $(".rewrite-trigger").rewrite();
  195.             $("#detailed-review-text").resize();
  196.             _kmq.push(["record", "Clicked to write a new user review", {
  197.                 "origin": "Product reviews sub-page"
  198.             }])
  199.         });
  200.         leavePageAlert = true;
  201.         return false
  202.     });
  203.     $(document).on("click", ".edit-review", function () {
  204.         var link = $(this);
  205.         if (!$("#product-review").find("#publisher-page").length) {
  206.             var reviewFormContainer = $('<div id="publisher-page"><div id="publisher-page-content"></div></div>');
  207.             $.get(link.data("href"), function (response) {
  208.                 if (!response || response == "") gdgt.notify.error();
  209.                 else {
  210.                     reviewFormContainer.prependTo("#product-review");
  211.                     $(".back-to-reviews").insertBefore("#publisher-page").css("display", "inline-block");
  212.                     $("#publisher-page-content").append(response);
  213.                     $(".col-left, .col-right").hide();
  214.                     ReviewForm.handleCriteriaRatingSliders(true);
  215.                     ReviewForm.handleOverallRatingSlider(true);
  216.                     $("#overall-rating-label").addClass("set");
  217.                     ReviewForm.calculateReviewAverageRating();
  218.                     var rteAllowed = false;
  219.                     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;
  220.                     if ($.browser.mozilla && parseInt($.browser.version, 10) < 4) rteAllowed = false;
  221.                     if (rteAllowed) {
  222.                         reviewFormContainer.find(".rewrite-trigger").rewrite();
  223.                         reviewFormContainer.find(".rewrite-content").eq(0).click()
  224.                     } else {
  225.                         var elem = $("#detailed-review-text"),
  226.                             text = elem.val();
  227.                         text = text.replace(/<br[^>]*>/gi, "\n");
  228.                         text = text.replace(/<(?:ol|ul)>/gi, "\n\n");
  229.                         text = text.replace(/<li>/gi, "* ");
  230.                         text = text.replace(/<\/(?:ol|ul|li)>/gi, "\n");
  231.                         text = text.replace(/<[a-z!\/?][^>]*>/gi, "");
  232.                         elem.val(text)
  233.                     }
  234.                     $("#detailed-review-text").resize();
  235.                     $("#detailed-review-text").trigger("keyup");
  236.                     leavePageAlert = true;
  237.                     _kmq.push(["record", "Clicked to edit user review"])
  238.                 }
  239.             })
  240.         } else {
  241.             $(".col-left, .col-right").hide();
  242.             $("#publisher-page").show();
  243.             $(".back-to-reviews").css("display", "inline-block");
  244.             leavePageAlert = true
  245.         }
  246.         return false
  247.     });
  248.     $(document).on("click", "#product-reviews .review-cancel, #product-reviews .back-to-reviews", function (e) {
  249.         e.preventDefault();
  250.         var link = $(this);
  251.         $("#cancel-review-dialog").dialog({
  252.             width: 607,
  253.             modal: true,
  254.             draggable: false,
  255.             resizable: false,
  256.             open: function (e, ui) {
  257.                 var dialogElem = $(this);
  258.                 $(document).on("click", ".ui-dialog .approve-dialog", function (e) {
  259.                     e.preventDefault();
  260.                     $(".product-review-preview").remove();
  261.                     $("section.lead, #stacks, #other-products").show();
  262.                     $("#product-reviews #publisher-page").nextAll().show();
  263.                     $("#publisher-page").remove();
  264.                     $(".back-to-reviews").hide();
  265.                     dialogElem.dialog("close");
  266.                     leavePageAlert = false;
  267.                     _kmq.push(["record", "Exited review form on product reviews sub-page"])
  268.                 });
  269.                 $(document).on("click", ".ui-widget-overlay, .ui-dialog .cancel-delete, .ui-dialog .cancel-dialog", function (e) {
  270.                     e.preventDefault();
  271.                     dialogElem.dialog("close")
  272.                 })
  273.             },
  274.             close: function (e, ui) {
  275.                 $(this).dialog("destroy")
  276.             },
  277.             show: {
  278.                 effect: "fade",
  279.                 duration: 200
  280.             },
  281.             hide: {
  282.                 effect: "fade",
  283.                 duration: 160
  284.             }
  285.         })
  286.     });
  287.     $(document).on("click focus", "#detailed-review-text", function () {
  288.         ReviewForm.hideClippy()
  289.     });
  290.     $(window).mousedown(function (e) {
  291.         if ($(".clippy").is(":visible")) {
  292.             var detectClickOnCard = $(e.target).parents("#review-form-criteria").length;
  293.             if (!detectClickOnCard) ReviewForm.hideClippy()
  294.         }
  295.     });
  296.     $(document).keyup(function (e) {
  297.         if ($(".clippy").is(":visible") && e.keyCode == 27) ReviewForm.hideClippy()
  298.     });
  299.     $(document).on("click", "#review-form-criteria-list dt", function () {
  300.         var elem = $(this).next().find(".criteria-rating-slider"),
  301.             value = $(this).next().find(".criteria-rating-value").val();
  302.         ReviewForm.drawClippy(elem, value);
  303.         _kmq.push(["record", "Showed Clippy on criteria name click"])
  304.     });
  305.     $(document).on("click", ".criteria-comment-icon, .comment-error", function () {
  306.         var elem = $(this).parent().find(".criteria-rating-slider"),
  307.             value = $(this).parent().find(".criteria-rating-value").val();
  308.         ReviewForm.drawClippy(elem, value);
  309.         _kmq.push(["record", "Showed Clippy on criteria comment indicator click"])
  310.     });
  311.     $(document).on("focus keyup", ".criteria-comment-textarea", function () {
  312.         Core.countCharacters($(this), $("#" + $(this).data("counterId")), $(".review-submit, .review-preview"),
  313.         160)
  314.     })
  315. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement