Guest User

frontend.js

a guest
Jan 7th, 2016
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.59 KB | None | 0 0
  1. var mrp_data_callbacks = [];
  2.  
  3. jQuery(document).ready(function() {
  4.  
  5. // supporting different versions of Font Awesome icons
  6. var icon_classes = jQuery.parseJSON(mrp_frontend_data.icon_classes);
  7.  
  8. /**
  9. * Saves a rating
  10. */
  11. function saveRating(e) {
  12.  
  13. var ratingItems = [];
  14. var customFields = [];
  15.  
  16. var btnId = e.currentTarget.id; // btnType-ratingFormid-postId-sequence
  17. var parts = btnId.split("-");
  18. var ratingFormId = parts[1];
  19. var postId = parts[2];
  20. var sequence = parts[3];
  21.  
  22. var id = ratingFormId + '-' + postId +'-' + sequence;
  23.  
  24. var hiddenRatingEntryId = "#ratingEntryId-" + id;
  25. var ratingEntryId = jQuery(hiddenRatingEntryId);
  26.  
  27. // rating items - hidden inputs are used to find all rating items in the rating form
  28. jQuery('.rating-form input[type="hidden"].rating-item-' + id).each(function(index) {
  29.  
  30. var ratingItemId = jQuery(this).val();
  31.  
  32. // get values for rating items
  33. var element = jQuery('[name=rating-item-' + ratingItemId + '-' + sequence + ']');
  34. var value = null;
  35. if (jQuery(element).is(':radio')) {
  36. value = jQuery('input[type="radio"][name=rating-item-' + ratingItemId + '-' + sequence + ']:checked').val();
  37. } else if (jQuery(element).is('select')) {
  38. value = jQuery('select[name=rating-item-' +ratingItemId + '-' + sequence + '] :selected').val();
  39. } else {
  40. value = jQuery('input[type=hidden][name=rating-item-' + ratingItemId + '-' + sequence + ']').val();
  41. }
  42.  
  43. var ratingItem = { 'id' : ratingItemId, 'value' : value };
  44. ratingItems[index] = ratingItem;
  45.  
  46. });
  47.  
  48. var title = jQuery('#mrp-title-' + sequence);
  49. var name = jQuery('#mrp-name-' + sequence);
  50. var email = jQuery('#mrp-email-' + sequence);
  51. var comment = jQuery('#mrp-comment-' + sequence);
  52.  
  53. // custom fields - hidden inputs are used to find all custom fields in the rating form
  54. jQuery('.rating-form input[type="hidden"].custom-field-' + id).each(function(index) {
  55.  
  56. var customFieldId = jQuery(this).val();
  57.  
  58. // get values for rating items
  59. var element = jQuery('[name=custom-field-' + customFieldId + '-' + sequence + ']');
  60. var value = null;
  61. var type = null;
  62. if (jQuery(element).is('textarea')) {
  63. value = jQuery('textarea[name=custom-field-' + customFieldId + '-' + sequence + ']').val();
  64. type = 'textarea';
  65. } else {
  66. value = jQuery('input[name=custom-field-' + customFieldId + '-' + sequence + ']').val();
  67. type = 'input';
  68. }
  69.  
  70. var customField = { 'id' : customFieldId, 'value' : value, 'type' : type };
  71. customFields[index] = customField;
  72.  
  73. });
  74.  
  75. var data = {
  76. action : "save_rating",
  77. nonce : mrp_frontend_data.ajax_nonce,
  78. ratingItems : ratingItems,
  79. title : (title != undefined) ? title.val() : '',
  80. name : (name != undefined) ? name.val() : '',
  81. email : (email != undefined) ? email.val() : '',
  82. comment : (comment != undefined) ? comment.val() : '',
  83. customFields : customFields,
  84. postId : postId,
  85. ratingFormId : ratingFormId,
  86. ratingEntryId : (ratingEntryId != undefined) ? ratingEntryId.val() : '',
  87. sequence : sequence,
  88. };
  89.  
  90. jQuery(mrp_data_callbacks).each( function(index) {
  91. if (typeof mrp_data_callbacks[index] == 'function') {
  92. data = mrp_data_callbacks[index].call(this, id, data);
  93. }
  94. });
  95.  
  96. var spinnerId = 'mrp-spinner-' + id;
  97.  
  98. jQuery('<i style="margin-left: 10px;" id="' + spinnerId + '" class="' + icon_classes.spinner + '"></i>').insertAfter('input#' + btnId);
  99.  
  100. jQuery.post(mrp_frontend_data.ajax_url, data, function(response) {
  101. handle_rating_form_submit_response(response);
  102. });
  103. }
  104.  
  105. /**
  106. * Deletes a rating
  107. */
  108. function deleteRating(e) {
  109.  
  110. var btnId = e.currentTarget.id; // btnType-ratingFormid-postId-sequence
  111. var parts = btnId.split("-");
  112. var ratingFormId = parts[1];
  113. var postId = parts[2];
  114. var sequence = parts[3];
  115.  
  116. var id = ratingFormId + '-' + postId +'-' + sequence;
  117.  
  118. var hiddenRatingEntryId = "#ratingEntryId-" + id;
  119. var ratingEntryId = jQuery(hiddenRatingEntryId);
  120.  
  121. var data = {
  122. action : "delete_rating",
  123. nonce : mrp_frontend_data.ajax_nonce,
  124. postId : postId,
  125. ratingFormId : ratingFormId,
  126. ratingEntryId : (ratingEntryId != undefined) ? ratingEntryId.val() : '',
  127. sequence : sequence
  128. };
  129.  
  130. jQuery(mrp_data_callbacks).each( function(index) {
  131. if (typeof mrp_data_callbacks[index] == 'function') {
  132. data = mrp_data_callbacks[index].call(this, id, data);
  133. }
  134. });
  135.  
  136. var spinnerId = 'mrp-spinner-' + id;
  137.  
  138. jQuery('<i style="margin-left: 10px;" id="' + spinnerId + '"class="' + icon_classes.spinner + '"></i>').insertAfter('input#saveBtn-' + id);
  139.  
  140. jQuery.post(mrp_frontend_data.ajax_url, data, function(response) {
  141. handle_rating_form_submit_response(response);
  142. });
  143. }
  144.  
  145. /**
  146. * Handles rating form submit response
  147. */
  148. function handle_rating_form_submit_response(response) {
  149.  
  150. var jsonResponse = jQuery.parseJSON(response);
  151.  
  152. var id = jsonResponse.data.rating_form_id + "-" + jsonResponse.data.post_id + "-" + jsonResponse.data.sequence;
  153. var ratingForm = jQuery("form#rating-form-" + id);
  154.  
  155. if ( jQuery(".user-ratings-dashboard-list table tr.rating-info-" + id).length ) {
  156.  
  157. //jQuery(".user-ratings-dashboard-list table tr.rating-edit-" + id).hide( "slow", function() {
  158. jQuery(".user-ratings-dashboard-list table tr.rating-edit-" + id).remove();
  159. //});
  160.  
  161. jQuery("#edit-" + id).css( "display", "inline-block" );
  162. jQuery("#cancel-" + id).css( "display", "none" );
  163.  
  164. unbindRatingFormEvents(id);
  165.  
  166. // if successful delete, remove row and unbind edit event
  167. if ( jsonResponse.data.action == "delete" && jsonResponse.status == "success") {
  168. jQuery(".user-ratings-dashboard-list table tr.rating-info-" + id).remove();
  169.  
  170. }
  171.  
  172. // update rating in user dashboard row?
  173.  
  174. } else {
  175.  
  176. // update rating results if success
  177. if (jsonResponse.status == 'success') {
  178. var ratingResult = jQuery(".rating-result-" + jsonResponse.data.rating_form_id + "-"
  179. + jsonResponse.data.post_id).filter(".mrp-filter");
  180.  
  181. if (ratingResult) {
  182. ratingResult.replaceWith(jsonResponse.data.html);
  183. }
  184. }
  185.  
  186. // remove existing errors for rating items, optional fields and custom fields
  187. jQuery("#rating-form-" + id + " .rating-item .mrp-error, #rating-form-" + id + " .custom-field .mrp-error, " +
  188. "#rating-form-" + id + " .optional-field .mrp-error").html("");
  189.  
  190. // check validation results
  191. if ( (jsonResponse.validation_results && jsonResponse.validation_results.length > 0) || jsonResponse.message ) {
  192. var messages = '';
  193.  
  194. if ( jsonResponse.validation_results ) {
  195. var $index = 0;
  196. for ($index; $index< jsonResponse.validation_results.length; $index++) {
  197.  
  198. if ( jsonResponse.validation_results[$index].field ) {
  199. jQuery("#" + jsonResponse.validation_results[$index].field + "-" + jsonResponse.data.sequence + "-error")
  200. .html(jsonResponse.validation_results[$index].message);
  201. } else {
  202. messages += '<p class="mrp message mrp-' + jsonResponse.validation_results[$index].severity + '">'
  203. + jsonResponse.validation_results[$index].message + '</p>';
  204. }
  205. }
  206. }
  207.  
  208. if (jsonResponse.message) {
  209. messages += '<p class="message ' + jsonResponse.status + '">'
  210. + jsonResponse.message + '</p>';
  211. }
  212.  
  213. if (ratingForm && ratingForm.parent().find('.message')) {
  214. ratingForm.parent().find('.message').remove();
  215. }
  216.  
  217. if (ratingForm && ratingForm.parent()) {
  218. ratingForm.before(messages);
  219. }
  220. }
  221.  
  222. // remove rating form if success
  223. if ( jsonResponse.status == 'success' && jsonResponse.data.hide_rating_form == true && ratingForm) {
  224. ratingForm.remove();
  225. }
  226.  
  227. var spinnerId = 'mrp-spinner-' + id;
  228. jQuery("#" + spinnerId).remove();
  229.  
  230. if ( jsonResponse.status == 'success' && jsonResponse.data.user_id ) {
  231.  
  232. // update buttons and hidden fields
  233. if ( jsonResponse.data.rating_entry_id ) {
  234.  
  235. if ( ! jQuery("#ratingEntryId-" + id).length ) { // save
  236.  
  237. var html = '<input type="hidden" value="' + jsonResponse.data.rating_entry_id + '" id="ratingEntryId-' + id + '" />';
  238. html += '<input type="button" class="btn btn-default delete-rating" id="deleteBtn-' + id + '" value="' + jsonResponse.data.delete_btn_text + '" />';
  239.  
  240. jQuery(html).insertBefore('#saveBtn-' + id);
  241.  
  242. jQuery("#deleteBtn-" + id).on("click", function(e) {
  243. deleteRating(e);
  244. });
  245.  
  246. // update text
  247. jQuery("#saveBtn-" + id).attr('value', jsonResponse.data.submit_btn_text);
  248.  
  249. jQuery("#ratingEntryId-" + id).attr('value', jsonResponse.data.rating_entry_id);
  250. }
  251.  
  252. // update - do nothing
  253. } else {
  254. // delete
  255. jQuery("#ratingEntryId-" + id).remove();
  256. jQuery("#saveBtn-" + id).attr('value', jsonResponse.data.submit_btn_text);
  257. jQuery("#deleteBtn-" + id).off();
  258. jQuery("#deleteBtn-" + id).remove();
  259. jQuery("#entryId-" + id).remove();
  260. }
  261. }
  262. }
  263.  
  264. }
  265.  
  266.  
  267. /**
  268. * Selected rating item value on hover and click
  269. */
  270. var ratingItemStatus = {};
  271.  
  272. var useCustomStarImages = jQuery.parseJSON(mrp_frontend_data.use_custom_star_images);
  273.  
  274. jQuery(".mrp-star-rating-select .mrp-star-empty, .mrp-star-rating-select .mrp-star-full").on("click", function(e) {
  275. starRatingClick(e);
  276. });
  277.  
  278. /**
  279. * Star rating on click
  280. */
  281. function starRatingClick(e) {
  282. var elementId = e.currentTarget.id;
  283. updateRatingItemStatus(elementId, 'clicked');
  284.  
  285. if (useCustomStarImages == true ) {
  286. jQuery("#" + elementId).not('.mrp-minus').removeClass('mrp-custom-empty-star mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-full-star mrp-star-full');
  287. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass('mrp-custom-empty-star mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-full-star mrp-star-full');
  288. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass('mrp-custom-full-star mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-empty-star mrp-star-empty');
  289. } else {
  290. jQuery("#" + elementId).not('.mrp-minus').removeClass(icon_classes.star_empty + " " + icon_classes.star_hover).addClass(icon_classes.star_full);
  291. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass(icon_classes.star_empty + " " + icon_classes.star_hover).addClass(icon_classes.star_full);
  292. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass(icon_classes.star_full + " " + icon_classes.star_hover).addClass(icon_classes.star_empty);
  293. }
  294.  
  295. updateSelectedHiddenValue(elementId);
  296. }
  297.  
  298. /**
  299. * Star rating minus click
  300. *
  301. * @param e
  302. * @returns
  303. */
  304. function starRatingMinusClick(e) {
  305. var elementId = e.currentTarget.id;
  306. updateRatingItemStatus(elementId, '');
  307.  
  308. if (useCustomStarImages == true) {
  309. jQuery("#" + elementId).not('.mrp-minus').removeClass('mrp-custom-empty-star mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-full-star mrp-star-full');
  310. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass('mrp-custom-empty-star mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-full-star mrp-star-full');
  311. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass('mrp-custom-full-star mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-empty-star mrp-star-empty');
  312. } else {
  313. jQuery("#" + elementId).not('.mrp-minus').removeClass(icon_classes.star_empty + " " + icon_classes.star_hover).addClass(icon_classes.star_full);
  314. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass(icon_classes.star_empty + " " + icon_classes.star_hover).addClass(icon_classes.star_full);
  315. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass(icon_classes.star_full + " " + icon_classes.star_hover).addClass(icon_classes.star_empty);
  316. }
  317.  
  318. updateSelectedHiddenValue(elementId);
  319. }
  320.  
  321. /**
  322. * Star rating hover
  323. * @param e
  324. * @returns
  325. */
  326. function starRatingHover(e) {
  327. var elementId = e.currentTarget.id;
  328. var ratingItemIdSequence = getRatingItemIdSequence(elementId);
  329.  
  330. if (jQuery("#" + ratingItemIdSequence).val() == 0 || (ratingItemStatus[ratingItemIdSequence] != 'clicked'
  331. && ratingItemStatus[ratingItemIdSequence] != undefined)) {
  332.  
  333. updateRatingItemStatus(elementId, 'hovered');
  334.  
  335. if (useCustomStarImages == true) {
  336. jQuery("#" + elementId).not('.mrp-minus').removeClass('mrp-custom-empty-star').addClass('mrp-custom-hover-star mrp-star-hover');
  337. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass('mrp-custom-empty-star').addClass('mrp-custom-hover-star mrp-star-hover');
  338. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass('mrp-custom-full-star mrp-star-full mrp-custom-hover-star mrp-star-hover').addClass('mrp-custom-empty-star mrp-star-empty');
  339.  
  340. } else {
  341. jQuery("#" + elementId).not('.mrp-minus').removeClass(icon_classes.star_empty).addClass(icon_classes.star_hover);
  342. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass(icon_classes.star_empty).addClass(icon_classes.star_hover);
  343. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass(icon_classes.star_full + " " + icon_classes.star_hover).addClass(icon_classes.star_empty);
  344. }
  345. }
  346. }
  347.  
  348. /**
  349. * Thumbs up click
  350. *
  351. * @param e
  352. * @returns
  353. */
  354. function thumbsUpClick(e) {
  355. var elementId = e.currentTarget.id;
  356.  
  357. jQuery("#" + elementId).removeClass(icon_classes.thumbs_up_off).addClass(icon_classes.thumbs_up_on);
  358. jQuery("#" + elementId).prev().removeClass(icon_classes.thumbs_down_on).addClass(icon_classes.thumbs_down_off);
  359.  
  360. updateSelectedHiddenValue(elementId);
  361. }
  362.  
  363. /**
  364. * Thumbs down click
  365. *
  366. * @param e
  367. * @returns
  368. */
  369. function thumbsDownClick(e) {
  370. var elementId = e.currentTarget.id;
  371.  
  372. jQuery("#" + elementId).removeClass(icon_classes.thumbs_down_off).addClass(icon_classes.thumbs_down_on);
  373. jQuery("#" + elementId).next().removeClass(icon_classes.thumbs_up_on).addClass(icon_classes.thumbs_up_off);
  374.  
  375. updateSelectedHiddenValue(elementId);
  376. }
  377.  
  378. // now cater for touch screen devices
  379. var touchData = {
  380. started : null, // detect if a touch event is sarted
  381. currrentX : 0,
  382. yCoord : 0,
  383. previousXCoord : 0,
  384. previousYCoord : 0,
  385. touch : null
  386. };
  387.  
  388. /**
  389. * Touch start
  390. */
  391. function touchStart(e) {
  392. touchData.started = new Date().getTime();
  393. var touch = e.originalEvent.touches[0];
  394. touchData.previousXCoord = touch.pageX;
  395. touchData.previousYCoord = touch.pageY;
  396. touchData.touch = touch;
  397. }
  398.  
  399. /**
  400. * Star rating touch
  401. */
  402. function starRatingTouch(e) {
  403. var elementId = e.currentTarget.id;
  404.  
  405. var now = new Date().getTime();
  406. // Detecting if after 200ms if in the same position.
  407. if ((touchData.started !== null)
  408. && ((now - touchData.started) < 200)
  409. && (touchData.touch !== null)) {
  410. var touch = touchData.touch;
  411. var xCoord = touch.pageX;
  412. var yCoord = touch.pageY;
  413. if ((touchData.previousXCoord === xCoord)
  414. && (touchData.previousYCoord === yCoord)) {
  415.  
  416. if (useCustomStarImages == true) {
  417. jQuery("#" + elementId).not('.mrp-minus').removeClass('mrp-custom-empty-star mrp-star-empty').addClass('mrp-custom-full-star mrp-star-full');
  418. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass('mrp-custom-empty-star mrp-star-empty').addClass('mrp-custom-full-star mrp-star-full');
  419. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass('mrp-custom-full-star mrp-star-full').addClass('mrp-custom-empty-star mrp-star-empty');
  420. } else {
  421. jQuery("#" + elementId).not('.mrp-minus').removeClass(icon_classes.star_empty).addClass(icon_classes.star_full);
  422. jQuery("#" + elementId).prevAll().not('.mrp-minus').removeClass(icon_classes.star_empty).addClass(icon_classes.star_full);
  423. jQuery("#" + elementId).nextAll().not('.mrp-minus').removeClass(icon_classes.star_full).addClass(icon_classes.star_empty);
  424. }
  425.  
  426. updateSelectedHiddenValue(elementId);
  427. }
  428. }
  429. touchData.started = null;
  430. touchData.touch = null;
  431. }
  432.  
  433. /**
  434. * Thumbs down touch
  435. */
  436. function thumbsDownTouch(e) {
  437. var elementId = e.currentTarget.id;
  438.  
  439. var now = new Date().getTime();
  440. // Detecting if after 200ms if in the same position.
  441. if ((touchData.started !== null)
  442. && ((now - touchData.started) < 200)
  443. && (touchData.touch !== null)) {
  444. var touch = touchData.touch;
  445. var xCoord = touch.pageX;
  446. var yCoord = touch.pageY;
  447. if ((touchData.previousXCoord === xCoord)
  448. && (touchData.previousYCoord === yCoord)) {
  449.  
  450. jQuery("#" + elementId).removeClass(icon_classes.thumbs_down_off).addClass(icon_classes.thumbs_down_on);
  451. jQuery("#" + elementId).next().removeClass(icon_classes.thumbs_up_on).addClass(icon_classes.thumbs_up_off);
  452.  
  453. updateSelectedHiddenValue(elementId);
  454. }
  455. }
  456. touchData.started = null;
  457. touchData.touch = null;
  458. }
  459.  
  460. /**
  461. * Thumbs up touch
  462. */
  463. function thumbsUpTouch(e) {
  464. var elementId = e.currentTarget.id;
  465.  
  466. var now = new Date().getTime();
  467. // Detecting if after 200ms if in the same position.
  468. if ((touchData.started !== null)
  469. && ((now - touchData.started) < 200)
  470. && (touchData.touch !== null)) {
  471. var touch = touchData.touch;
  472. var xCoord = touch.pageX;
  473. var yCoord = touch.pageY;
  474. if ((touchData.previousXCoord === xCoord)
  475. && (touchData.previousYCoord === yCoord)) {
  476.  
  477. jQuery("#" + elementId).removeClass(icon_classes.thumbs_up_off).addClass(icon_classes.thumbs_up_on);
  478. jQuery("#" + elementId).prev().removeClass(icon_classes.thumbs_down_on).addClass(icon_classes.thumbs_down_off);
  479.  
  480. updateSelectedHiddenValue(elementId);
  481. }
  482. }
  483. touchData.started = null;
  484. touchData.touch = null;
  485. }
  486.  
  487. /**
  488. * Updates the rating item status to either hovered or clicked
  489. */
  490. function updateRatingItemStatus(elementId, status) {
  491. var ratingItemIdSequence = getRatingItemIdSequence(elementId);
  492. if (ratingItemIdSequence != null) {
  493. ratingItemStatus[ratingItemIdSequence] = status;
  494. }
  495. }
  496.  
  497. /**
  498. * Retrieves the rating item id sequence used to store the status of a rating item option
  499. */
  500. function getRatingItemIdSequence(elementId) {
  501. var parts = elementId.split("-");
  502.  
  503. var ratingItemId = parts[4]; /// skip 2: rating-item-
  504. var sequence = parts[5];
  505.  
  506. var ratingItemIdSequence = 'rating-item-' + ratingItemId + '-' + sequence;
  507. return ratingItemIdSequence;
  508. }
  509.  
  510. /**
  511. * Updates the selected hidden value for a rating item
  512. */
  513. function updateSelectedHiddenValue(elementId) {
  514.  
  515. // id is in format "index-3-rating-item-2-1"
  516.  
  517. var parts = elementId.split("-");
  518. var value = parts[1]; // this is the star index
  519. var ratingItemId = parts[4]; /// skipt 2: rating-item-
  520. var sequence = parts[5];
  521.  
  522. // update hidden value for storing selected option
  523. var hiddenValue = '#rating-item-'+ ratingItemId + '-' + sequence;
  524.  
  525. jQuery(hiddenValue).val(value);
  526. }
  527.  
  528.  
  529. jQuery("#include-rating").change(function() {
  530. if (this.checked) {
  531. jQuery("p.mrp-comment-form-field").show("slow", function() {} );
  532. } else {
  533. jQuery("p.mrp-comment-form-field").hide("slow", function() {} );
  534. }
  535. });
  536.  
  537.  
  538. var rowActions = jQuery(".user-ratings-dashboard-list td.rating-actions a");
  539. jQuery.each(rowActions, function(index, element) {
  540.  
  541. jQuery(element).click(function(event) {
  542.  
  543. var anchorId = this.id;
  544. var parts = anchorId.split("-");
  545. var action = parts[0];
  546. var ratingFormId = parts[1];
  547. var postId = parts[2];
  548. var sequence = parts[3];
  549.  
  550. var id = ratingFormId + '-' + postId +'-' + sequence;
  551.  
  552. if (action == 'edit' && ! jQuery("#rating-form-" + id).length) {
  553.  
  554. var data = {
  555. action : "get_rating_form",
  556. nonce : mrp_frontend_data.ajax_nonce,
  557. postId : postId,
  558. ratingFormId : ratingFormId,
  559. sequence : sequence
  560. };
  561.  
  562. var spinnerId = 'mrp-spinner-' + id;
  563.  
  564. // only one user ratings dashboard supported on a page...
  565. var colspan = jQuery(".user-ratings-dashboard-list table:first").find("tr:first th").length;
  566.  
  567. jQuery("<i style=\"margin-left: 10px;\" id=\"" + spinnerId + "\"class=\""
  568. + icon_classes.spinner + "\">").insertAfter(".user-ratings-dashboard-list table tr.rating-info-" + id + " td.rating-actions a:last");
  569.  
  570. jQuery.post(mrp_frontend_data.ajax_url, data, function(response) {
  571. var responseJSON = jQuery.parseJSON(response);
  572.  
  573. jQuery("<tr class=\"rating-edit-" + id + "\"><td colspan=\"" + colspan + "\">" + responseJSON.data.html + "</td></tr>").insertAfter("tr.rating-info-" + id)
  574.  
  575. jQuery("#edit-" + id).css( "display", "none" );
  576. jQuery("#cancel-" + id).css( "display", "inline-block" );
  577.  
  578. jQuery("#rating-form-" + id).show( "slow", function() {} );
  579. jQuery("#" + spinnerId).remove();
  580.  
  581. bindRatingFormEvents(id);
  582.  
  583. jQuery("#cancel-" + id).on( "click", function(e) {
  584.  
  585. //jQuery(".user-ratings-dashboard-list table tr.rating-edit-" + id).hide( "slow", function() {
  586. jQuery(".user-ratings-dashboard-list table tr.rating-edit-" + id).remove();
  587. //});
  588.  
  589. jQuery("#edit-" + id).css( "display", "inline-block" );
  590. jQuery("#cancel-" + id).css( "display", "none" );
  591.  
  592. unbindRatingFormEvents(id);
  593. });
  594. });
  595. }
  596.  
  597. return false;
  598. });
  599. });
  600.  
  601. var ratingForms = jQuery(".rating-form form");
  602. jQuery.each(ratingForms, function( key, value ) {
  603. var parts = value.id.split("-"); // e.g. rating-form-2-1-0
  604. var id = parts[2] + "-" + parts[3] + "-" + parts[4];
  605. bindRatingFormEvents(id);
  606. });
  607.  
  608. /**
  609. * Binds rating form events
  610. */
  611. function bindRatingFormEvents( id ) {
  612. jQuery("#saveBtn-" + id).on("click", function(e) {
  613. saveRating(e);
  614. });
  615. jQuery("#deleteBtn-" + id).on("click", function(e) {
  616. deleteRating(e);
  617. });
  618. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-star-empty, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-full").on("click", function(e) {
  619. starRatingClick(e);
  620. });
  621. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-minus, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-empty, " +
  622. "#rating-form-" + id + " .mrp-star-rating-select .mrp-star-full").on("mouseenter mouseleave", function(e) {
  623. starRatingHover(e);
  624. });
  625. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-minus").on("click", function(e) {
  626. starRatingMinusClick(e);
  627. });
  628.  
  629. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-on, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-off").on("click", function(e) {
  630. thumbsUpClick(e);
  631. });
  632. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-on, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-off").on("click", function(e) {
  633. thumbsDownClick(e);
  634. });
  635. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-off, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-on").on( "touchend touchcancel", function(e) {
  636. thumbsDownTouch(e);
  637. });
  638. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-off, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-on").on( "touchend touchcancel", function(e) {
  639. thumbsUpTouch(e);
  640. });
  641. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-star-empty, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-full, " +
  642. "#rating-form-" + id + " .mrp-star-rating-select .mrp-minus").on("touchend touchcancel", function(e) {
  643. starRatingTouch(e);
  644. });
  645. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-star-empty, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-full, " +
  646. "#rating-form-" + id + " .mrp-star-rating-select .mrp-minus, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-on, " +
  647. "#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-off, .mrp-thumbs-select .mrp-thumbs-down-on, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-on").on("touchstart", function(e) {
  648. touchStart(e);
  649. });
  650. }
  651.  
  652. /**
  653. * Unbinds rating form events
  654. */
  655. function unbindRatingFormEvents( id ) {
  656.  
  657. // off() will remove boths touch and click event handlers
  658.  
  659. jQuery("#saveBtn-" + id).off();
  660. jQuery("#deleteBtn-" + id).off();
  661. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-star-empty, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-full").off();
  662. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-minus, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-empty, #rating-form-" + id + " .mrp-star-rating-select .mrp-star-full").off();
  663. jQuery("#rating-form-" + id + " .mrp-star-rating-select .mrp-minus").off();
  664.  
  665. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-on, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-off").off();
  666. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-on, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-off").off();
  667. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-off, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-up-on").off();
  668. jQuery("#rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-off, #rating-form-" + id + " .mrp-thumbs-select .mrp-thumbs-down-on").off();
  669. }
  670.  
  671. /**
  672. * Binds the WP Comment form events
  673. */
  674. function bindWPCommentFormEvents() {
  675. jQuery(".mrp-comment-form-field .mrp-star-rating-select .mrp-star-empty, .mrp-comment-form-field .mrp-star-rating-select .mrp-star-full").on("click", function(e) {
  676. starRatingClick(e);
  677. });
  678. jQuery(".mrp-comment-form-field .mrp-star-rating-select .mrp-minus, .mrp-comment-form-field .mrp-star-rating-select .mrp-star-empty, " +
  679. ".mrp-comment-form-field .mrp-star-rating-select .mrp-star-full").on("mouseenter mouseleave", function(e) {
  680. starRatingHover(e);
  681. });
  682. jQuery(".mrp-comment-form-field .mrp-star-rating-select .mrp-minus").on("click", function(e) {
  683. starRatingMinusClick(e);
  684. });
  685.  
  686. jQuery(".mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-down-on, .mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-down-off").on("click", function(e) {
  687. thumbsUpClick(e);
  688. });
  689. jQuery(".mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-up-on, .mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-up-off").on("click", function(e) {
  690. thumbsDownClick(e);
  691. });
  692. jQuery(".mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-down-off, .mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-down-on").on( "touchend touchcancel", function(e) {
  693. thumbsDownTouch(e);
  694. });
  695. jQuery(".mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-up-off, .mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-up-on").on( "touchend touchcancel", function(e) {
  696. thumbsUpTouch(e);
  697. });
  698. jQuery(".mrp-comment-form-field .mrp-star-rating-select .mrp-star-empty, .mrp-comment-form-field .mrp-star-rating-select .mrp-star-full, " +
  699. ".mrp-comment-form-field .mrp-star-rating-select .mrp-minus").on("touchend touchcancel", function(e) {
  700. starRatingTouch(e);
  701. });
  702. jQuery(".mrp-comment-form-field .mrp-star-rating-select .mrp-star-empty, .mrp-comment-form-field .mrp-star-rating-select .mrp-star-full, " +
  703. ".mrp-comment-form-field .mrp-star-rating-select .mrp-minus, .mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-up-on, " +
  704. ".mrp-comment-form-field .mrp-thumbs-select .mrp-thumbs-up-off, .mrp-comment-form-field.mrp-thumbs-select .mrp-thumbs-down-on, " +
  705. ".mrp-thumbs-select .mrp-thumbs-down-on").on("touchstart", function(e) {
  706. touchStart(e);
  707. });
  708. }
  709. bindWPCommentFormEvents();
  710. });
Advertisement
Add Comment
Please, Sign In to add comment