PluginProbe
WANotifier for Forms and Actions / 2.7.13
WANotifier for Forms and Actions v2.7.13
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / assets / js / admin.js

admin.js in WANotifier for Forms and Actions 2.7.13, at assets/js/admin.js

360 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global waNotifier */
2 (function($) {
3
4 // Make standard ajax calls
5 function notifierAjax(data, callback){
6 $.ajax({
7 type: 'post',
8 dataType: 'json',
9 url: notifierObj.ajaxurl,
10 data: data,
11 success: callback
12 });
13 }
14
15 // Show / hide fields as per conditional logic
16 function conditionallyShowFields() {
17 if ($('.meta-fields').length == 0) {
18 return;
19 }
20
21 $('.form-field').each(function() {
22 var thisField = $(this).find(':input');
23
24 // Conditionally show/hide fields
25 var conditions = $(this).attr('data-conditions') || '';
26 var conditionsOperator = $(this).attr('data-conditions-operator') || 'OR';
27 if (conditions !== '') {
28 var fieldElem = $(this);
29 var conditionsArray = JSON.parse(conditions);
30 var fieldConditionResults = [];
31
32 conditionsArray.forEach(function(condition) {
33 var fieldClass = '.' + condition.field + '_field';
34 var fieldInput = $(fieldClass + ' :input');
35 var fieldInputType = fieldInput.prop('type');
36
37 // Do not fetch value of hidden fields.
38 if(!$(fieldClass).is(':visible')){
39 return;
40 }
41
42 // Get field value
43 var fieldVal = fieldInput.val();
44
45 // Get field value if it's radio or checkbox
46 if($.inArray(fieldInputType, ['radio', 'checkbox']) !== -1) {
47 fieldVal = $(fieldClass + ' :input:checked').val();
48 }
49
50 var showThis = false;
51 if(condition.operator == '==') {
52 showThis = (fieldVal == condition.value) ? true : false;
53 }
54 else if(condition.operator == '!=') {
55 showThis = (fieldVal != condition.value) ? true : false;
56 }
57
58 fieldConditionResults.push(showThis);
59 });
60
61 if('OR' == conditionsOperator){
62 var showField = false;
63 }
64 else if('AND' == conditionsOperator){
65 var showField = fieldConditionResults[0];
66 }
67
68 fieldConditionResults.forEach(function(showThis){
69 if('OR' == conditionsOperator){
70 showField = showField || showThis;
71 }
72 else if('AND' == conditionsOperator){
73 showField = showField && showThis;
74 }
75 });
76
77 if(showField){
78 fieldElem.show();
79 var disabled = fieldElem.find(':input').attr('data-disabled') || 'no';
80 if('no' == disabled){
81 fieldElem.find(':input').removeAttr('disabled');
82 }
83 }
84 else{
85 fieldElem.hide();
86 fieldElem.find(':input').attr('disabled', 'disabled');
87 }
88 }
89
90 // Apply data limit on fields
91 if (thisField.hasClass('force-text-limit')) {
92 var content = thisField.val();
93 var contentLength = content.length;
94 var limit = thisField.attr('data-limit');
95 if (contentLength >= limit) {
96 thisField.siblings('label').find('.limit-used').text(limit);
97 thisField.val(content.substr(0, limit));
98 } else {
99 thisField.siblings('label').find('.limit-used').text(contentLength);
100 }
101 }
102
103 });
104 }
105
106 // Fetch and display trigger fields
107 function fetchAndDisplayTriggerFields(){
108 const post_id = $('#post_ID').val() || 0;
109 const trigger = $('#notifier_trigger').val() || '';
110
111 if('' == trigger) {
112 $('.notifier-trigger-merge-tags').html('');
113 return;
114 }
115
116 $('.notifier-trigger-merge-tags').html('<div class="loader"></div>');
117
118 var data = {
119 action: 'notifier_fetch_trigger_fields',
120 trigger: trigger,
121 post_id: post_id,
122 _wpnonce: notifierObj.nonces.fetch_trigger_fields
123 };
124
125 notifierAjax( data, function(response) {
126 if (response.status == 'success') {
127 $('.notifier-trigger-merge-tags').html(response.html);
128 conditionallyShowFields();
129 $('.trigger-fields-wrap select').select2({
130 closeOnSelect: false,
131 placeholder: 'Click to select fields'
132 });
133 }
134 });
135
136 }
137
138 // Uploading media to WP using wp.media
139 var file_frame;
140 function uploadMediaFile( button, preview_media ) {
141 var button_id = button.attr('id');
142 var field_id = button_id.replace( '_button', '' );
143 var preview_id = button_id.replace( '_button', '_preview' );
144
145 // Create the media frame.
146 file_frame = wp.media.frames.file_frame = wp.media({
147 title: button.data( 'uploader_title' ),
148 button: {
149 text: button.data( 'uploader_button_text' ),
150 },
151 library: {
152 type: button.data( 'uploader_supported_file_types' ).split(',')
153 },
154 multiple: false
155 });
156
157 // When an image is selected, run a callback.
158 file_frame.on( 'select', function() {
159 attachment = file_frame.state().get('selection').first().toJSON();
160 jQuery("#"+field_id).attr('data-type', attachment.type);
161 jQuery("#"+field_id).attr('data-subtype', attachment.subtype);
162 jQuery("#"+field_id).siblings('.notifier-media-preview').find('.notifier-media-preview-item').addClass('hide');
163 if( preview_media ) {
164 if('image' == attachment.type || ('application' == attachment.type && 'pdf' == attachment.subtype) ) {
165 jQuery("#"+field_id).attr('data-url', attachment.sizes.full.url);
166 jQuery("#"+preview_id+'_image').removeClass('hide').attr('src', attachment.sizes.thumbnail.url);
167 }
168 else if ('video' == attachment.type) {
169 jQuery("#"+field_id).attr('data-url', attachment.url);
170 jQuery("#"+preview_id+'_video').removeClass('hide').find('source').attr('src', attachment.url);
171 jQuery("#"+preview_id+'_video')[0].load();
172 }
173 }
174 jQuery("#"+field_id).val(attachment.id).change();
175 });
176
177 // Finally, open the modal
178 file_frame.open();
179 }
180
181 // Fetch activity logs on Tools page
182 function fetchActivityLogs(){
183 $('.activity-log-preview-wrap').html('');
184 var currenEle = $('#notifier_activity_date');
185 if(currenEle.val() === ''){
186 return false;
187 }
188 currenEle.addClass('disabled-field');
189 data = {
190 'action': 'fetch_activity_logs_by_date',
191 'notifier_activity_date': currenEle.val(),
192 '_wpnonce': notifierObj.nonces.fetch_activity_logs
193 }
194
195 notifierAjax(data, function(response){
196 currenEle.removeClass('disabled-field');
197 $('.activity-log-preview-wrap').html(response.preview);
198 });
199 }
200
201 $(document).on('ready', function() {
202
203 /*****************
204 * Dashboard page
205 ****************/
206
207 // Toggle steps on dashboard page
208 $('.toggle-step').on('click', function() {
209 $(this).closest('.step').toggleClass('active');
210 $(this).closest('.step').siblings().removeClass('active');
211 });
212
213 // Highlight menu items
214 const wan_menu_elem = $('#toplevel_page_notifier');
215 const wan_menu_elem_a = $('#toplevel_page_notifier > a');
216 const notifier_cpts = ['wa_notifier_trigger'];
217 const current_cpt = $('#notifier-admin-header').data('post-type') || '';
218 if (notifier_cpts.includes(current_cpt)) {
219 wan_menu_elem.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
220 wan_menu_elem_a.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
221 }
222
223 // Toggle trigger info
224 $('.notifier-show-trigger-info').click(function(e){
225 e.preventDefault();
226 $(this).closest('.notifier-trigger-wrap').find('.notifier-trigger-info').toggleClass('hide');
227 });
228
229 /*****************
230 * Global
231 ****************/
232
233 // Make the top admin header sticky
234 var wpcontent_top = $('#wpcontent').offset().top;
235 $(window).scroll( function() {
236 if (window.pageYOffset > wpcontent_top) {
237 $('#notifier-admin-header').addClass('sticky');
238 } else {
239 $('#notifier-admin-header').removeClass('sticky');
240 }
241 });
242
243 // Show feilds conditionally
244 conditionallyShowFields();
245 $(document).on('change keyup', '.meta-fields :input', function() {
246 conditionallyShowFields();
247 });
248
249 /*****************
250 * Settings page
251 ****************/
252
253 $('.notifier-media-upload-button').click(function() {
254 uploadMediaFile( $(this), true );
255 });
256
257 $('.notifier-media-delete-button').click(function() {
258 $(this).next( '.notifier-media-attachment-id' ).val( '' ).attr('data-tyoe', '').attr('data-subtyoe', '').attr('data-url', '').change();
259 $(this).siblings( '.notifier-media-preview' ).find('img').attr('src', '').addClass('hide');
260 $(this).siblings( '.notifier-media-preview' ).find('source').attr('src', '').parent().addClass('hide');
261 return false;
262 });
263
264 $(document).on('change', '#ctc_button_style', function(){
265 var btn_style = $(this).val();
266 var $this = $(this);
267 if(btn_style == 'default'){
268 $('.notifier-btn-preview-wrap').html('');
269 return false;
270 }
271
272 if(btn_style == 'btn-custom-image'){
273 $('.notifier-chat-btn-image-url').show();
274 $('.notifier-btn-preview-wrap').hide();
275 }
276 else{
277 $('.notifier-btn-preview-wrap').show();
278 $('.notifier-chat-btn-image-url').hide();
279 $this.addClass('disabled-field');
280
281 data = {
282 action: 'notifier_preview_btn_style',
283 btn_style: btn_style,
284 _wpnonce: notifierObj.nonces.preview_btn_style
285 }
286
287 notifierAjax(data, function(response){
288 $this.removeClass('disabled-field');
289 $('.notifier-btn-preview-wrap').html(response.preview);
290 });
291 }
292 });
293
294 /*****************
295 * Triggers page
296 ****************/
297
298 if($('#notifier_trigger').length > 0){
299 $('#notifier_trigger').select2({
300 templateResult: function(option) {
301 var $option = $(
302 '<div><strong>' + option.text + '</strong></div><small>' + option.title + '</small>'
303 );
304 return $option;
305 }
306 });
307 }
308
309 $(document).on('change', '.notifier-enable-trigger', function(){
310 var enabled = ($(this).prop('checked')) ? 'yes' : 'no';
311 var postId = $(this).data('post-id');
312 var $this = $(this);
313 data = {
314 action: 'notifier_change_trigger_status',
315 post_id: postId,
316 enabled: enabled,
317 _wpnonce: notifierObj.nonces.change_trigger_status
318 }
319 notifierAjax(data, function(response){});
320 });
321
322 // Do stuff if on the trigger edit page
323 if ($('#notifier-trigger-data').length > 0) {
324 fetchAndDisplayTriggerFields(); // Fetch and display data and recipient fields
325 $('#notifier-trigger-data :input').on('change keyup', function(){
326 if('notifier_trigger' == $(this).attr('name')){
327 fetchAndDisplayTriggerFields();
328 }
329 });
330 }
331
332 // Select all checkbox fields
333 $(document).on('click', '.notifier-select-all-checkboxes', function(e){
334 e.preventDefault();
335 $(this).closest('.trigger-fields-wrap').find('option').prop('selected', true);
336 $(this).closest('.trigger-fields-wrap').find('select').trigger('change');
337 });
338
339 // Unselect all checkbox fields
340 $(document).on('click', '.notifier-unselect-all-checkboxes', function(e){
341 e.preventDefault();
342 $(this).closest('.trigger-fields-wrap').find('option').prop('selected', false);
343 $(this).closest('.trigger-fields-wrap').find('select').trigger('change');
344 });
345
346 /*****************
347 * Tools page
348 ****************/
349 if($('#notifier_activity_date').length > 0){
350 fetchActivityLogs();
351 }
352
353 $(document).on('change', '#notifier_activity_date', function(){
354 fetchActivityLogs();
355 });
356
357 });
358
359 })(jQuery);
360