UserExperience_Remove_CssJs_Page_View.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * File: UserExperience_Remove_CssJs_Page_View.js
  3. *
  4. * @since 2.7.0
  5. *
  6. * @package W3TC
  7. *
  8. * @global W3TCRemoveCssJsData
  9. */
  10. jQuery(function() {
  11. const wpadminbar_height = (jQuery(window).width() > 600 && jQuery('#wpadminbar').length) ? jQuery('#wpadminbar').outerHeight() : 0,
  12. nav_bar_height = (jQuery('#w3tc-top-nav-bar').length) ? jQuery('#w3tc-top-nav-bar').outerHeight() : 0,
  13. options_menu_height = (jQuery('#w3tc > #w3tc-options-menu').length) ? jQuery('#w3tc > #w3tc-options-menu').outerHeight() : 0,
  14. form_bar_height = (jQuery('.w3tc_form_bar').length) ? jQuery('.w3tc_form_bar').outerHeight() : 0;
  15. jQuery(document).on(
  16. 'click',
  17. '#w3tc_remove_cssjs_singles_add',
  18. function() {
  19. let maxId = -1;
  20. jQuery('.remove_cssjs_singles_path').each(
  21. function() {
  22. const currentId = parseInt(jQuery(this).closest('li').attr('id').replace('remove_cssjs_singles_', ''), 10);
  23. if (!isNaN(currentId)) {
  24. maxId = Math.max(maxId, currentId);
  25. }
  26. }
  27. );
  28. const singleId = maxId + 1;
  29. const li = jQuery(
  30. '<li id="remove_cssjs_singles_' + singleId + '">' +
  31. '<table class="form-table">' +
  32. '<tr class="accordion-header">' +
  33. '<th>' + W3TCRemoveCssJsData.lang.singlesPathLabel + '</th>' +
  34. '<td>' +
  35. '<input class="remove_cssjs_singles_path" type="text" name="user-experience-remove-cssjs-singles[' + singleId + '][url_pattern]" value="" > ' +
  36. '<input type="button" class="button remove_cssjs_singles_delete" value="' + W3TCRemoveCssJsData.lang.singlesDelete + '"/>' +
  37. '<span class="accordion-toggle dashicons dashicons-arrow-down-alt2"></span>' +
  38. '<p class="description">' + W3TCRemoveCssJsData.lang.singlesPathDescription + '</p>' +
  39. '<div class="description_example">' +
  40. '<p class="description_example_trigger"><span class="dashicons dashicons-editor-help"></span><span class="description_example_text">' + W3TCRemoveCssJsData.lang.singlesExampleTrigger + '</span></p>' +
  41. '<div class="description">' +
  42. '<strong>' + W3TCRemoveCssJsData.lang.singlesPathExampleDirLabel + '</strong>' +
  43. '<code>' + W3TCRemoveCssJsData.lang.singlesPathExampleDir + '</code>' +
  44. '<strong>' + W3TCRemoveCssJsData.lang.singlesPathExampleFileLabel + '</strong>' +
  45. '<code>' + W3TCRemoveCssJsData.lang.singlesPathExampleFile + '</code>' +
  46. '</div>' +
  47. '</div>' +
  48. '</td>' +
  49. '</tr>' +
  50. '<tr>' +
  51. '<th><label for="remove_cssjs_singles_' + singleId + '_action">' + W3TCRemoveCssJsData.lang.singlesBehaviorLabel + '</label></th>' +
  52. '<td>' +
  53. '<p class="description">' + W3TCRemoveCssJsData.lang.singlesBehaviorDescription + '</p>' +
  54. '<label class="remove_cssjs_singles_behavior"><input class="remove_cssjs_singles_behavior_radio" type="radio" name="user-experience-remove-cssjs-singles[' + singleId + '][action]" value="exclude" checked><strong>' + W3TCRemoveCssJsData.lang.singlesBehaviorExcludeText + '</strong> ' + W3TCRemoveCssJsData.lang.singlesBehaviorExcludeText2 + '</label>' +
  55. '<br/>' +
  56. '<label class="remove_cssjs_singles_behavior"><input class="remove_cssjs_singles_behavior_radio" type="radio" name="user-experience-remove-cssjs-singles[' + singleId + '][action]" value="include"><strong>' + W3TCRemoveCssJsData.lang.singlesBehaviorIncludeText + '</strong> ' + W3TCRemoveCssJsData.lang.singlesBehaviorIncludeText2 + '</label>' +
  57. '</td>' +
  58. '</tr>' +
  59. '<tr>' +
  60. '<th><label class="remove_cssjs_singles_' + singleId + '_includes_label" for="remove_cssjs_singles_' + singleId + '_includes">' + W3TCRemoveCssJsData.lang.singlesIncludesLabelExclude + '</label></th>' +
  61. '<td>' +
  62. '<textarea id="remove_cssjs_singles_' + singleId + '_includes" name="user-experience-remove-cssjs-singles[' + singleId + '][includes]" rows="5" cols="50" ></textarea>' +
  63. '<p class="description remove_cssjs_singles_' + singleId + '_includes_description">' + W3TCRemoveCssJsData.lang.singlesIncludesDescriptionExclude + '</p>' +
  64. '<div class="description_example">' +
  65. '<p class="description_example_trigger"><span class="dashicons dashicons-editor-help"></span><span class="description_example_text">' + W3TCRemoveCssJsData.lang.singlesExampleTrigger + '</span></p>' +
  66. '<div class="description">' +
  67. '<code>' + W3TCRemoveCssJsData.lang.singlesIncludesExample + '</code>' +
  68. '</div>' +
  69. '</div>' +
  70. '</td>' +
  71. '</tr>' +
  72. '<tr>' +
  73. '<th><label class="remove_cssjs_singles_' + singleId + '_includes_content_label" for="remove_cssjs_singles_' + singleId + '_includes_content">' + W3TCRemoveCssJsData.lang.singlesIncludesContentLabelExclude + '</label></th>' +
  74. '<td>' +
  75. '<textarea id="remove_cssjs_singles_' + singleId + '_includes_content" name="user-experience-remove-cssjs-singles[' + singleId + '][includes_content]" rows="5" cols="50" ></textarea>' +
  76. '<p class="description remove_cssjs_singles_' + singleId + '_includes_content_description">' + W3TCRemoveCssJsData.lang.singlesIncludesContentDescriptionExclude + '</p>' +
  77. '<div class="description_example">' +
  78. '<p class="description_example_trigger"><span class="dashicons dashicons-editor-help"></span><span class="description_example_text">' + W3TCRemoveCssJsData.lang.singlesExampleTrigger + '</span></p>' +
  79. '<div class="description">' +
  80. '<code>' + W3TCRemoveCssJsData.lang.singlesIncludesContentExample + '</code>' +
  81. '</div>' +
  82. '</div>' +
  83. '</td>' +
  84. '</tr>' +
  85. '</table>' +
  86. '</li>'
  87. );
  88. jQuery('#remove_cssjs_singles_empty').remove();
  89. jQuery('#remove_cssjs_singles').append(li);
  90. li.find('tr:not(:first-child)').slideToggle(50);
  91. li.find('tr:first-child td .description').first().toggle(50);
  92. li.find('tr:first-child td .description_example').toggle(50);
  93. li.find('.accordion-toggle').toggleClass('dashicons-arrow-down-alt2 dashicons-arrow-up-alt2');
  94. // Scroll to taget after .5 seconds.
  95. setTimeout(
  96. function() {
  97. jQuery('html, body').animate(
  98. {
  99. scrollTop: li.find('.remove_cssjs_singles_path').offset().top - wpadminbar_height - nav_bar_height - options_menu_height - form_bar_height - 10
  100. },
  101. 600
  102. );
  103. li.find('.remove_cssjs_singles_path').focus();
  104. },
  105. 500
  106. );
  107. }
  108. );
  109. jQuery(document).on(
  110. 'change',
  111. '.remove_cssjs_singles_path',
  112. function() {
  113. let $inputField = jQuery(this);
  114. let singlePath = $inputField.val();
  115. let originalValue = $inputField.data('originalValue');
  116. if (singlePath && null !== singlePath) {
  117. singlePath = singlePath.trim();
  118. let exists = false;
  119. jQuery('.remove_cssjs_singles_path').not($inputField).each(
  120. function() {
  121. if (jQuery(this).val() === singlePath) {
  122. alert(W3TCRemoveCssJsData.lang.singlesExists);
  123. exists = true;
  124. $inputField.val(originalValue);
  125. // A timeout is needed here as the alert "steals" focus and causes a race condition.
  126. setTimeout(
  127. function() {
  128. $inputField.focus();
  129. },
  130. 100
  131. );
  132. return false;
  133. }
  134. }
  135. );
  136. if (!exists) {
  137. $inputField.data('originalValue', singlePath);
  138. }
  139. } else {
  140. alert(W3TCRemoveCssJsData.lang.singlesEmptyUrl);
  141. $inputField.val(originalValue);
  142. // A timeout is needed here as the alert "steals" focus and causes a race condition.
  143. setTimeout(
  144. function() {
  145. $inputField.focus();
  146. },
  147. 100
  148. );
  149. }
  150. }
  151. );
  152. jQuery(document).on(
  153. 'click',
  154. '.description_example_trigger',
  155. function () {
  156. var $trigger = jQuery(this).find('.description_example_text');
  157. var $description = jQuery(this).siblings('.description');
  158. if ($description.css('display') === 'none') {
  159. $trigger.text(W3TCRemoveCssJsData.lang.singlesExampleTriggerClose);
  160. $description.css('display', 'inline-block');
  161. } else {
  162. $trigger.text(W3TCRemoveCssJsData.lang.singlesExampleTrigger);
  163. $description.css('display', 'none');
  164. }
  165. }
  166. );
  167. jQuery(document).on(
  168. 'click',
  169. '.remove_cssjs_singles_delete',
  170. function () {
  171. jQuery(this).parents('#remove_cssjs_singles li').remove();
  172. if (0 === jQuery('#remove_cssjs_singles li').length) {
  173. jQuery('#remove_cssjs_singles').append('<li id="remove_cssjs_singles_empty">' + W3TCRemoveCssJsData.lang.singlesNoEntries + '<input type="hidden" name="user-experience-remove-cssjs-singles[]"></li>');
  174. }
  175. w3tc_beforeupload_bind();
  176. }
  177. );
  178. jQuery(document).on(
  179. 'change',
  180. '.remove_cssjs_singles_behavior_radio',
  181. function () {
  182. const parentId = jQuery(this).closest('li').attr('id');
  183. if (this.value === 'exclude') {
  184. jQuery('.' + parentId + '_includes_label').text(W3TCRemoveCssJsData.lang.singlesIncludesLabelExclude);
  185. jQuery('.' + parentId + '_includes_description').text(W3TCRemoveCssJsData.lang.singlesIncludesDescriptionExclude);
  186. jQuery('.' + parentId + '_includes_content_label').text(W3TCRemoveCssJsData.lang.singlesIncludesContentLabelExclude);
  187. jQuery('.' + parentId + '_includes_content_description').text(W3TCRemoveCssJsData.lang.singlesIncludesContentDescriptionExclude);
  188. } else {
  189. jQuery('.' + parentId + '_includes_label').text(W3TCRemoveCssJsData.lang.singlesIncludesLabelInclude);
  190. jQuery('.' + parentId + '_includes_description').text(W3TCRemoveCssJsData.lang.singlesIncludesDescriptionInclude);
  191. jQuery('.' + parentId + '_includes_content_label').text(W3TCRemoveCssJsData.lang.singlesIncludesContentLabelInclude);
  192. jQuery('.' + parentId + '_includes_content_description').text(W3TCRemoveCssJsData.lang.singlesIncludesContentDescriptionInclude);
  193. }
  194. }
  195. );
  196. jQuery(document).on(
  197. 'click',
  198. '.w3tc_remove_cssjs_singles .accordion-toggle',
  199. function() {
  200. var $icon = jQuery(this);
  201. var $table = $icon.closest('li').find('table');
  202. // Toggle visibility of all rows except the first one
  203. $table.find('tr:not(:first-child)').slideToggle(50);
  204. // Toggle visibility of .description and .description_example
  205. $icon.closest('td').find('.description').first().toggle(50);
  206. $icon.closest('td').find('.description_example').toggle(50);
  207. // Change the icon
  208. $icon.toggleClass('dashicons-arrow-down-alt2 dashicons-arrow-up-alt2');
  209. }
  210. );
  211. setRemoveCssjsSinglesPathValues();
  212. });
  213. function setRemoveCssjsSinglesPathValues() {
  214. jQuery('.remove_cssjs_singles_path').each(
  215. function() {
  216. var $inputField = jQuery(this);
  217. var originalValue = $inputField.val();
  218. $inputField.data('originalValue', originalValue);
  219. }
  220. );
  221. }