PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.5
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.5
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / libraries / cmb2-field-js-controls / js / js-controls.js

js-controls.js in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.5, at libraries/cmb2-field-js-controls/js/js-controls.js

151 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( $ ) {
2 // Edit link
3 $('a.cmb-field-js-controls-edit').on( 'click', function( e ) {
4 e.preventDefault();
5
6 var field_row = $(this).parent().next('.cmb-row');
7 var field = $($(this).attr('href'));
8
9 if ( field_row.is( ':hidden' ) ) {
10 // Store previous value to restore if cancel is clicked
11 $(this).attr('data-value', field.val());
12
13 // Show field row and js controls after
14 field_row.slideDown( 'fast' );
15 field_row.next('.cmb-field-js-controls-after').show();
16
17 // Hide this
18 $(this).hide();
19 }
20 });
21
22 // Save link
23 $('a.cmb-field-js-controls-save').on( 'click', function( e ) {
24 e.preventDefault();
25
26 var field_row = $(this).parent().prev('.cmb-row');
27 var field = $($(this).attr('href'));
28 var field_type = 'text';
29
30 // Get the field type
31 if( field_row[0].hasAttribute('data-fieldtype') )
32 field_type = field_row.attr('data-fieldtype');
33
34 // Datetime picker
35 if( field_type === 'text_datetime_timestamp' )
36 field = $($(this).attr('href') + '_date');
37
38 if ( ! field_row.is( ':hidden' ) ) {
39 // Update value to show
40 var value_container = field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-value');
41
42 value_container.html( cmb_js_controls_display_field( field, field_type ) );
43
44 // Remove previous value attribute
45 field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-edit').removeAttr('data-value');
46
47 // Hide field row and show js controls before
48 field_row.slideUp( 'fast' );
49 field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-edit').show();
50
51 // Hide this
52 $(this).parent().hide();
53 }
54 });
55
56 // Cancel link
57 $('a.cmb-field-js-controls-cancel').on( 'click', function( e ) {
58 e.preventDefault();
59
60 var field_row = $(this).parent().prev('.cmb-row');
61 var field = $($(this).attr('href'));
62 var field_type = 'text';
63
64 // Get the field type
65 if( field_row[0].hasAttribute('data-fieldtype') )
66 field_type = field_row.attr('data-fieldtype');
67
68 // Datetime picker
69 if( field_type === 'text_datetime_timestamp' )
70 field = $($(this).attr('href') + '_date');
71
72 if ( ! field_row.is( ':hidden' ) ) {
73 // Restore previous value
74 field.val( field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-edit').attr('data-value') );
75
76 // Remove previous value attribute
77 field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-edit').removeAttr('data-value');
78
79 // Hide field row and show js controls before
80 field_row.slideUp( 'fast' );
81 field_row.prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-edit').show();
82
83 // Hide this
84 $(this).parent().hide();
85 }
86 });
87 })(jQuery);
88
89 function cmb_js_controls_display_field( field, field_type ) {
90 var field_id = field.attr('id');
91 var value = field.val();
92 var output = undefined;
93
94 // Filter by cmb_js_controls_display_{field_id}
95 output = wp.hooks.applyFilters( 'cmb_js_controls_display_' + field_id, output, field, field_type );
96
97 if( output !== undefined && output.length ) {
98 return output;
99 }
100
101 // Filter by cmb_js_controls_display_{field_type}
102 output = wp.hooks.applyFilters( 'cmb_js_controls_display_' + field_type, output, field );
103
104 if( output !== undefined && output.length ) {
105 return output;
106 }
107
108 // Built-in CMB2 fields
109
110 // select
111 if( field_type === 'select' && field.find('option[value="' + field.val() + '"]').length ) {
112 return field.find('option[value="' + field.val() + '"]').html();
113 }
114
115 // colorpicker
116 if( field_type === 'colorpicker' ) {
117 return '<span class="cmb2-colorpicker-swatch"><span style="background-color:' + field.val() + '"></span> ' + field.val() + '</span>';
118 }
119
120 // radio
121 if( field_type === 'radio' ) {
122 // TODO: Get label text
123 }
124
125 // Datetime Picker
126 if( field_type === 'text_datetime_timestamp' ) {
127 var date = field.val();
128 var time = field.next().val();
129
130 return date + ' ' + time;
131 }
132
133 // CMB2 custom fields
134 if( field_type === 'post_ajax_search' || field_type === 'user_ajax_search' || field_type === 'term_ajax_search' ) {
135 var anchor = field.closest('.cmb-row').prev('.cmb-field-js-controls-before').find('.cmb-field-js-controls-value .cmb-column a');
136
137 if( anchor.length ) {
138
139 var href = anchor.attr('href')
140 .replace(/(post=)[^\&]+/, '$1' + field.val()) // Post parameter
141 .replace(/(user=)[^\&]+/, '$1' + field.val()) // User parameter
142 .replace(/(term=)[^\&]+/, '$1' + field.val()); // Term parameter
143
144 return '<a href="' + href + '" class="edit-link">' + field.next('input').val() + '</a>'
145 } else {
146 return field.next('input').val();
147 }
148 }
149
150 return field.val();
151 }