(function (window, document, $) {
window.RatingDialog = function (page) {
var self = this;
self.show = function (options) {
options = options || {};
options.header = options.header || "Rate and Review";
var html = '
';
$(page).append(html);
var popup = $('#popupRatingDialog').popup().trigger('create').on("popupafteropen", function () {
$('#txtRatingDialogTitle', this).focus();
}).popup("open").on("popupafterclose", function () {
$('form', this).off("submit");
$(this).off("popupafterclose").remove();
});
$('form', popup).on('submit', function () {
if (options.callback) {
var review = {
id: options.id,
rating: $('#txtRatingDialogRating', this).val(),
title: $('#txtRatingDialogTitle', this).val(),
recommend: $('#txtRatingDialogRecommend', this).checked(),
review: $('#txtRatingDialogReview', this).val(),
};
if (review.rating < 3) {
if (!review.title) {
$('#errorMsg', this).html("Please give reason for low rating").show();
$('#txtRatingDialogTitle', this).focus();
return false;
}
}
if (!review.recommend) {
if (!review.title) {
$('#errorMsg', this).html("Please give reason for not recommending").show();
$('#txtRatingDialogTitle', this).focus();
return false;
}
}
options.callback(review);
} else console.log("No callback function provided");
return false;
});
};
self.close = function () {
$('#popupRatingDialog', page).popup("close");
};
};
window.RatingHelpers = {
ratePackage: function (link) {
var id = link.getAttribute('data-id');
var name = link.getAttribute('data-name');
var rating = link.getAttribute('data-rating');
var dialog = new RatingDialog($.mobile.activePage);
dialog.show({
header: "Rate and review " + name,
id: id,
rating: rating,
callback: function (review) {
console.log(review);
dialog.close();
ApiClient.createPackageReview(review).done(function () {
Dashboard.alert({
message: "Thank you for your review",
title: "Thank You"
});
});
}
});
},
getStoreRatingHtml: function (rating, id, name, noLinks) {
var html = "";
if (!rating) rating = 0;
for (var i = 1; i <= 5; i++) {
var title = noLinks ? rating + " stars" : "Rate " + i + (i > 1 ? " stars" : " star");
html += noLinks ? "" : "
";
if (rating <= i - 1) {
html += "";
} else if (rating < i) {
html += "";
} else {
html += "";
}
html += noLinks ? "" : "";
}
html += "
";
return html;
}
};
})(window, document, jQuery);