PluginProbe
WANotifier for Forms and Actions / 2.7.6
WANotifier for Forms and Actions v2.7.6
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.6, at assets/js/admin.js

347 lines 10.7 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 $(document).on('ready', function() {
181
182 /*****************
183 * Dashboard page
184 ****************/
185
186 // Toggle steps on dashboard page
187 $('.toggle-step').on('click', function() {
188 $(this).closest('.step').toggleClass('active');
189 $(this).closest('.step').siblings().removeClass('active');
190 });
191
192 // Highlight menu items
193 const wan_menu_elem = $('#toplevel_page_notifier');
194 const wan_menu_elem_a = $('#toplevel_page_notifier > a');
195 const notifier_cpts = ['wa_notifier_trigger'];
196 const current_cpt = $('#notifier-admin-header').data('post-type') || '';
197 if (notifier_cpts.includes(current_cpt)) {
198 wan_menu_elem.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
199 wan_menu_elem_a.removeClass('wp-not-current-submenu').addClass('wp-has-current-submenu wp-menu-open');
200 }
201
202 // Toggle trigger info
203 $('.notifier-show-trigger-info').click(function(e){
204 e.preventDefault();
205 $(this).closest('.notifier-trigger-wrap').find('.notifier-trigger-info').toggleClass('hide');
206 });
207
208 /*****************
209 * Global
210 ****************/
211
212 // Make the top admin header sticky
213 var wpcontent_top = $('#wpcontent').offset().top;
214 $(window).scroll( function() {
215 if (window.pageYOffset > wpcontent_top) {
216 $('#notifier-admin-header').addClass('sticky');
217 } else {
218 $('#notifier-admin-header').removeClass('sticky');
219 }
220 });
221
222 // Show feilds conditionally
223 conditionallyShowFields();
224 $(document).on('change keyup', '.meta-fields :input', function() {
225 conditionallyShowFields();
226 });
227
228 /*****************
229 * Settings page
230 ****************/
231
232 $('.notifier-media-upload-button').click(function() {
233 uploadMediaFile( $(this), true );
234 });
235
236 $('.notifier-media-delete-button').click(function() {
237 $(this).next( '.notifier-media-attachment-id' ).val( '' ).attr('data-tyoe', '').attr('data-subtyoe', '').attr('data-url', '').change();
238 $(this).siblings( '.notifier-media-preview' ).find('img').attr('src', '').addClass('hide');
239 $(this).siblings( '.notifier-media-preview' ).find('source').attr('src', '').parent().addClass('hide');
240 return false;
241 });
242
243 $(document).on('change', '#ctc_button_style', function(){
244 var btn_style = $(this).val();
245 var $this = $(this);
246 if(btn_style == 'default'){
247 $('.notifier-btn-preview-wrap').html('');
248 return false;
249 }
250
251 if(btn_style == 'btn-custom-image'){
252 $('.notifier-chat-btn-image-url').show();
253 $('.notifier-btn-preview-wrap').hide();
254 }
255 else{
256 $('.notifier-btn-preview-wrap').show();
257 $('.notifier-chat-btn-image-url').hide();
258 $this.addClass('disabled-field');
259
260 data = {
261 action: 'notifier_preview_btn_style',
262 btn_style: btn_style,
263 }
264
265 notifierAjax(data, function(response){
266 $this.removeClass('disabled-field');
267 $('.notifier-btn-preview-wrap').html(response.preview);
268 });
269 }
270 });
271
272 /*****************
273 * Triggers page
274 ****************/
275
276 if($('#notifier_trigger').length > 0){
277 $('#notifier_trigger').select2({
278 templateResult: function(option) {
279 var $option = $(
280 '<div><strong>' + option.text + '</strong></div><small>' + option.title + '</small>'
281 );
282 return $option;
283 }
284 });
285 }
286
287 $(document).on('change', '.notifier-enable-trigger', function(){
288 var enabled = ($(this).prop('checked')) ? 'yes' : 'no';
289 var postId = $(this).data('post-id');
290 var $this = $(this);
291 data = {
292 action: 'notifier_change_trigger_status',
293 post_id: postId,
294 enabled: enabled
295 }
296 notifierAjax(data, function(response){});
297 });
298
299 // Do stuff if on the trigger edit page
300 if ($('#notifier-trigger-data').length > 0) {
301 fetchAndDisplayTriggerFields(); // Fetch and display data and recipient fields
302 $('#notifier-trigger-data :input').on('change keyup', function(){
303 if('notifier_trigger' == $(this).attr('name')){
304 fetchAndDisplayTriggerFields();
305 }
306 });
307 }
308
309 // Select all checkbox fields
310 $(document).on('click', '.notifier-select-all-checkboxes', function(e){
311 e.preventDefault();
312 $(this).closest('.trigger-fields-wrap').find('option').prop('selected', true);
313 $(this).closest('.trigger-fields-wrap').find('select').trigger('change');
314 });
315
316 // Unselect all checkbox fields
317 $(document).on('click', '.notifier-unselect-all-checkboxes', function(e){
318 e.preventDefault();
319 $(this).closest('.trigger-fields-wrap').find('option').prop('selected', false);
320 $(this).closest('.trigger-fields-wrap').find('select').trigger('change');
321 });
322
323 /*****************
324 * Tools page
325 ****************/
326 $(document).on('change', '#notifier_activity_date', function(){
327 $('.activity-log-preview-wrap').html('');
328 var currenEle = $(this);
329 if(currenEle.val() === ''){
330 return false;
331 }
332 currenEle.addClass('disabled-field');
333 data = {
334 'action': 'fetch_activity_logs_by_date',
335 'notifier_activity_date': currenEle.val(),
336 }
337
338 notifierAjax(data, function(response){
339 currenEle.removeClass('disabled-field');
340 $('.activity-log-preview-wrap').html(response.preview);
341 });
342 });
343
344 });
345
346 })(jQuery);
347