PluginProbe
WANotifier for Forms and Actions / 2.7.9
WANotifier for Forms and Actions v2.7.9
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.9, at assets/js/admin.js

356 lines 10.8 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 };
123
124 notifierAjax( data, function(response) {
125 if (response.status == 'success') {
126 $('.notifier-trigger-merge-tags').html(response.html);
127 conditionallyShowFields();
128 $('.trigger-fields-wrap select').select2({
129 closeOnSelect: false,
130 placeholder: 'Click to select fields'
131 });
132 }
133 });
134
135 }
136
137 // Uploading media to WP using wp.media
138 var file_frame;
139 function uploadMediaFile( button, preview_media ) {
140 var button_id = button.attr('id');
141 var field_id = button_id.replace( '_button', '' );
142 var preview_id = button_id.replace( '_button', '_preview' );
143
144 // Create the media frame.
145 file_frame = wp.media.frames.file_frame = wp.media({
146 title: button.data( 'uploader_title' ),
147 button: {
148 text: button.data( 'uploader_button_text' ),
149 },
150 library: {
151 type: button.data( 'uploader_supported_file_types' ).split(',')
152 },
153 multiple: false
154 });
155
156 // When an image is selected, run a callback.
157 file_frame.on( 'select', function() {
158 attachment = file_frame.state().get('selection').first().toJSON();
159 jQuery("#"+field_id).attr('data-type', attachment.type);
160 jQuery("#"+field_id).attr('data-subtype', attachment.subtype);
161 jQuery("#"+field_id).siblings('.notifier-media-preview').find('.notifier-media-preview-item').addClass('hide');
162 if( preview_media ) {
163 if('image' == attachment.type || ('application' == attachment.type && 'pdf' == attachment.subtype) ) {
164 jQuery("#"+field_id).attr('data-url', attachment.sizes.full.url);
165 jQuery("#"+preview_id+'_image').removeClass('hide').attr('src', attachment.sizes.thumbnail.url);
166 }
167 else if ('video' == attachment.type) {
168 jQuery("#"+field_id).attr('data-url', attachment.url);
169 jQuery("#"+preview_id+'_video').removeClass('hide').find('source').attr('src', attachment.url);
170 jQuery("#"+preview_id+'_video')[0].load();
171 }
172 }
173 jQuery("#"+field_id).val(attachment.id).change();
174 });
175
176 // Finally, open the modal
177 file_frame.open();
178 }
179
180 // Fetch activity logs on Tools page
181 function fetchActivityLogs(){
182 $('.activity-log-preview-wrap').html('');
183 var currenEle = $('#notifier_activity_date');
184 if(currenEle.val() === ''){
185 return false;
186 }
187 currenEle.addClass('disabled-field');
188 data = {
189 'action': 'fetch_activity_logs_by_date',
190 'notifier_activity_date': currenEle.val(),
191 }
192
193 notifierAjax(data, function(response){
194 currenEle.removeClass('disabled-field');
195 $('.activity-log-preview-wrap').html(response.preview);
196 });
197 }
198
199 $(document).on('ready', function() {
200
201 /*****************
202 * Dashboard page
203 ****************/
204
205 // Toggle steps on dashboard page
206 $('.toggle-step').on('click', function() {
207 $(this).closest('.step').toggleClass('active');
208 $(this).closest('.step').siblings().removeClass('active');
209 });
210
211 // Highlight menu items
212 const wan_menu_elem = $('#toplevel_page_notifier');
213 const wan_menu_elem_a = $('#toplevel_page_notifier > a');
214 const notifier_cpts = ['wa_notifier_trigger'];
215 const current_cpt = $('#notifier-admin-header').data('post-type') || '';
216 if (notifier_cpts.includes(current_cpt)) {
217 wan_menu_elem.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
218 wan_menu_elem_a.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
219 }
220
221 // Toggle trigger info
222 $('.notifier-show-trigger-info').click(function(e){
223 e.preventDefault();
224 $(this).closest('.notifier-trigger-wrap').find('.notifier-trigger-info').toggleClass('hide');
225 });
226
227 /*****************
228 * Global
229 ****************/
230
231 // Make the top admin header sticky
232 var wpcontent_top = $('#wpcontent').offset().top;
233 $(window).scroll( function() {
234 if (window.pageYOffset > wpcontent_top) {
235 $('#notifier-admin-header').addClass('sticky');
236 } else {
237 $('#notifier-admin-header').removeClass('sticky');
238 }
239 });
240
241 // Show feilds conditionally
242 conditionallyShowFields();
243 $(document).on('change keyup', '.meta-fields :input', function() {
244 conditionallyShowFields();
245 });
246
247 /*****************
248 * Settings page
249 ****************/
250
251 $('.notifier-media-upload-button').click(function() {
252 uploadMediaFile( $(this), true );
253 });
254
255 $('.notifier-media-delete-button').click(function() {
256 $(this).next( '.notifier-media-attachment-id' ).val( '' ).attr('data-tyoe', '').attr('data-subtyoe', '').attr('data-url', '').change();
257 $(this).siblings( '.notifier-media-preview' ).find('img').attr('src', '').addClass('hide');
258 $(this).siblings( '.notifier-media-preview' ).find('source').attr('src', '').parent().addClass('hide');
259 return false;
260 });
261
262 $(document).on('change', '#ctc_button_style', function(){
263 var btn_style = $(this).val();
264 var $this = $(this);
265 if(btn_style == 'default'){
266 $('.notifier-btn-preview-wrap').html('');
267 return false;
268 }
269
270 if(btn_style == 'btn-custom-image'){
271 $('.notifier-chat-btn-image-url').show();
272 $('.notifier-btn-preview-wrap').hide();
273 }
274 else{
275 $('.notifier-btn-preview-wrap').show();
276 $('.notifier-chat-btn-image-url').hide();
277 $this.addClass('disabled-field');
278
279 data = {
280 action: 'notifier_preview_btn_style',
281 btn_style: btn_style,
282 }
283
284 notifierAjax(data, function(response){
285 $this.removeClass('disabled-field');
286 $('.notifier-btn-preview-wrap').html(response.preview);
287 });
288 }
289 });
290
291 /*****************
292 * Triggers page
293 ****************/
294
295 if($('#notifier_trigger').length > 0){
296 $('#notifier_trigger').select2({
297 templateResult: function(option) {
298 var $option = $(
299 '<div><strong>' + option.text + '</strong></div><small>' + option.title + '</small>'
300 );
301 return $option;
302 }
303 });
304 }
305
306 $(document).on('change', '.notifier-enable-trigger', function(){
307 var enabled = ($(this).prop('checked')) ? 'yes' : 'no';
308 var postId = $(this).data('post-id');
309 var $this = $(this);
310 data = {
311 action: 'notifier_change_trigger_status',
312 post_id: postId,
313 enabled: enabled
314 }
315 notifierAjax(data, function(response){});
316 });
317
318 // Do stuff if on the trigger edit page
319 if ($('#notifier-trigger-data').length > 0) {
320 fetchAndDisplayTriggerFields(); // Fetch and display data and recipient fields
321 $('#notifier-trigger-data :input').on('change keyup', function(){
322 if('notifier_trigger' == $(this).attr('name')){
323 fetchAndDisplayTriggerFields();
324 }
325 });
326 }
327
328 // Select all checkbox fields
329 $(document).on('click', '.notifier-select-all-checkboxes', function(e){
330 e.preventDefault();
331 $(this).closest('.trigger-fields-wrap').find('option').prop('selected', true);
332 $(this).closest('.trigger-fields-wrap').find('select').trigger('change');
333 });
334
335 // Unselect all checkbox fields
336 $(document).on('click', '.notifier-unselect-all-checkboxes', function(e){
337 e.preventDefault();
338 $(this).closest('.trigger-fields-wrap').find('option').prop('selected', false);
339 $(this).closest('.trigger-fields-wrap').find('select').trigger('change');
340 });
341
342 /*****************
343 * Tools page
344 ****************/
345 if($('#notifier_activity_date').length > 0){
346 fetchActivityLogs();
347 }
348
349 $(document).on('change', '#notifier_activity_date', function(){
350 fetchActivityLogs();
351 });
352
353 });
354
355 })(jQuery);
356