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

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