-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
216 lines (164 loc) · 5.59 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @author Steven A. Zahm
*/
;jQuery(document).ready( function($) {
var $document = $( document );
var CN_Form = {
init: function() {
var selected = $('input[name=entry_type]');
var type;
if ( selected.length == 1 ) {
type = selected.val();
} else if ( selected.length > 1) {
type = $('input[name=entry_type]:checked').val();
}
// Show custom fields based on current selected entry type.
this.show( type );
// Convert the org field to a select if the entry type is an individual.
if ( 'individual' == type ) {
this.convertOrgFieldToSelect();
} else {
this.convertOrgFieldToInput()
}
// // Add new rule for jQuery Validate for the phone number field.
// /** @link http://stackoverflow.com/a/15482666/5351316 */
// $.validator.addMethod(
// 'phoneFormat',
// function( value, element ) {
// return this.optional( element ) || /^\(\d{3}\) \d{3}-\d{4}$/.test( value );
// },
// 'Please enter a valid phone number.'
// );
//
// // Format and validate entered phone numbers.
// $( '#phone-numbers' ).on( 'keyup paste', 'input[name^="phone"]', function() {
//
// /** @link http://stackoverflow.com/a/37066380/5351316 */
// // $( this ).val( $( this ).val().replace( /^(\d{3})(\d{3})(\d+)$/, '($1) $2-$3' ) );
//
// /** @link http://stackoverflow.com/a/28588038/5351316 */
// // Remove invalid chars from the input
// var input = this.value.replace( /[^0-9\(\)\s\-]/g, "" );
// var inputlen = input.length;
//
// // Get just the numbers in the input
// var numbers = this.value.replace( /\D/g, '' );
// var numberslen = numbers.length;
//
// // Value to store the masked input
// var newval = "";
//
// // Loop through the existing numbers and apply the mask
// for ( var i = 0; i < numberslen; i++ ) {
// if ( i == 0 ) newval = "(" + numbers[ i ];
// else if ( i == 3 ) newval += ") " + numbers[ i ];
// else if ( i == 6 ) newval += "-" + numbers[ i ];
// else newval += numbers[ i ];
// }
//
// // Re-add the non-digit characters to the end of the input that the user entered and that match the mask.
// if ( inputlen >= 1 && numberslen == 0 && input[ 0 ] == "(" ) newval = "(";
// else if ( inputlen >= 6 && numberslen == 3 && input[ 4 ] == ")" && input[ 5 ] == " " ) newval += ") ";
// else if ( inputlen >= 5 && numberslen == 3 && input[ 4 ] == ")" ) newval += ")";
// else if ( inputlen >= 6 && numberslen == 3 && input[ 5 ] == " " ) newval += " ";
// else if ( inputlen >= 10 && numberslen == 6 && input[ 9 ] == "-" ) newval += "-";
//
// $( this ).val( newval.substring( 0, 14 ) );
//
// $( this ).rules( 'add', {
// required: true,
// phoneFormat: true
// } )
// });
//
// // Add the placeholder attribute to phone text input field.
// $document.on( 'entry-field-type-added', function( event, type ) {
//
// if ( 'phone' == type ) $( 'input[name^="' + type + '"]' ).attr( 'placeholder', '(###) ###-####' );
// } );
//
// Display the custom fields based on selected entry type.
$document.on( 'entry-type-selected', function( event, type ) {
CN_Form.show( type );
} );
},
show: function( type ) {
// var website = $( '#custom_website-hide' );
// var volume = $( '#custom_volume-hide' );
switch ( type ) {
case 'individual':
// if ( website.is(':checked') ) {
//
// website.trigger( 'click' );
// }
//
// if ( volume.is(':checked') ) {
//
// volume.trigger( 'click' );
// }
CN_Form.convertOrgFieldToSelect();
break;
case 'organization':
// if ( ! website.is(':checked') ) {
//
// website.trigger( 'click' );
// }
//
// if ( ! volume.is(':checked') ) {
//
// volume.trigger( 'click' );
// }
CN_Form.convertOrgFieldToInput();
break;
}
},
convertOrgFieldToSelect: function() {
// console.log('Convert to select.');
var input = $( 'input#cn-organization' );
if ( input.length ) {
var value = input.val();
var options = '<option value=""></option>';
var names = [];
$.each( cnEntryOrganizationOptions.options, function( key, name ) {
names.push( { 'key': key, 'value': name } );
} );
names.sort( function( a, b ) {
var aName = a.value.toLowerCase();
var bName = b.value.toLowerCase();
return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
} );
// console.log(names);
$.each( names, function( key, name ) {
options = options + '<option value="' + name.value +'">' + name.value + '</option>'
} );
input.replaceWith(
'<select id="cn-organization" name="organization" style="width: 100%;" data-placeholder="' + cnEntryOrganizationOptions.string.select_default + '">' +
options +
'</select>' );
// console.log( value );
$('select#cn-organization').val( value ).chosen({ width: '100%' });
// Set style of label.
// $('label[for=cn-organization]').css({
// fontWeight: 'bold',
// marginRight: '10px'
// })
}
},
convertOrgFieldToInput: function() {
// console.log('Convert to input.');
var select = $('select#cn-organization');
if ( select.length ) {
var value = select.val();
select.chosen('destroy')
.replaceWith('<input type="text" id="cn-organization" name="organization" value="">');
$( 'input#cn-organization' ).val( value );
// Set style of label.
$('label[for=cn-organization]').css({
fontWeight: 'normal',
marginRight: '0'
})
}
}
};
CN_Form.init();
});