admin-settings.js
20.3 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
* Fires when the dom is ready
*
*/
jQuery(document).ready(function() {
if ( jQuery('#cpac').length === 0 ) {
return false;
}
// General
cpac_pointer();
cpac_submit_form();
// Settings Page
cpac_clear_input_defaults();
// Columns Page
cpac_sortable();
cpac_menu();
cpac_help();
cpac_add_column();
cpac_importexport();
cpac_sidebar_feedback();
//cpac_sidebar_scroll();
// we start by binding the toggle and remove events.
jQuery('.cpac-column').each( function( i, col ) {
jQuery( col ).column_bind_toggle();
jQuery( col ).column_bind_remove();
jQuery( col ).column_bind_clone();
jQuery( col ).cpac_bind_container_addon_events();
});
});
function cpac_importexport() {
jQuery( '#php-export-results textarea' ).on( 'focus, mouseup', function() {
jQuery( this ).select();
} ).select().focus();
}
/*
* Submit Form
*
* @since 2.0.2
*/
function cpac_submit_form() {
jQuery('.form-update a.submit-update').click( function(e){
e.preventDefault();
jQuery(this).closest('.columns-container').find('.cpac-columns form').submit();
});
}
/*
* Column: bind toggle events
*
* For performance we bind all other events after the click event.
*
* @since 2.0
*/
jQuery.fn.column_bind_toggle = function() {
var column = jQuery(this);
column.find( 'td.column_type a, td.column_edit, td.column_label a.toggle, td.column_label .edit-button' ).click( function( e ) {
e.preventDefault();
column.toggleClass( 'opened' ).find( '.column-form' ).slideToggle( 150 );
if ( ! column.hasClass( 'events-binded' ) ) {
column.column_bind_events();
}
column.addClass('events-binded');
// hook for addons
jQuery( document ).trigger( 'column_init', column );
} );
};
/*
* Column: bind remove events
*
* @since 2.0
*/
jQuery.fn.column_bind_remove = function() {
jQuery(this).find('.remove-button').click( function(e) {
jQuery(this).closest('.cpac-column').column_remove();
e.preventDefault();
});
};
/**
* Column: bind clone events
*
* @since 2.3.4
*/
jQuery.fn.column_bind_clone = function() {
jQuery( this ).find( '.clone-button' ).click( function( e ) {
var column, clone;
e.preventDefault();
column = jQuery( this ).closest( '.cpac-column' );
clone = column.column_clone();
if ( typeof clone !== 'undefined' ) {
clone.removeClass( 'loading' ).hide().slideDown();
}
} );
};
jQuery.fn.cpac_column_refresh = function() {
var el = jQuery( this );
// Mark column as loading
el.addClass( 'loading' );
el.find( '.column-form' ).prepend( '<span class="spinner" />' );
// Fetch new form HTML
jQuery.post( ajaxurl, {
plugin_id: 'cpac',
action: 'cpac_column_refresh',
column: jQuery( this ).find( 'input.column-name' ).val(),
formdata: jQuery( this ).parents( 'form' ).serialize()
}, function( data ) {
// Replace current form by new form
var newel = jQuery( '<div>' + data + '</div>' ).children();
el.replaceWith( newel );
el = newel;
// Bind events
el.column_bind_toggle();
el.column_bind_remove();
el.column_bind_clone();
el.column_bind_events();
// Remove "loading" marking from column
el.removeClass( 'loading' ).addClass( 'opened' ).find( '.column-form' ).show();
// Allow plugins to hook into this event
jQuery( document ).trigger( 'column_change', el );
} );
};
/*
* Form Events
*
* @since 2.0
*/
jQuery.fn.column_bind_events = function() {
var column = jQuery( this );
var container = column.closest( '.columns-container ');
var storage_model = container.attr( 'data-type' );
// Current column type
var default_value = column.find( '.column_type select option:selected' ).val();
column.find( '.column_type select' ).change( function() {
var option = jQuery( 'optgroup', this ).children( ':selected' );
var type = option.val();
var label = option.text();
var msg = jQuery( this ).next( '.msg' ).hide();
// Find template element for this field type
var template = container.find( '.for-cloning-only .cpac-column[data-type="' + type + '"]' );
if ( template.length ) {
if ( template.find( '.is-disabled' ).length ) {
msg.html( template.find( '.is-disabled' ).html() ).show();
// Set to default
jQuery(this).find( 'option' ).removeAttr( 'selected' );
jQuery(this).find( 'option[value="' + default_value + '"]' ).attr( 'selected', 'selected' );
}
// Prevent column types that do not allow it to have multiple instances
else if ( typeof template.attr( 'data-clone' ) === 'undefined' && jQuery( '.cpac-columns', container ).find( '[data-type="' + type + '"]' ).length ) {
msg.html( cpac_i18n.clone.replace( '%s', '<strong>' + label + '</strong>' ) ).show();
// Set to default
jQuery(this).find('option').removeAttr('selected');
jQuery(this).find('option[value="' + default_value + '"]').attr('selected', 'selected');
return;
}
else {
var clone = template.clone();
// Open settings
clone.addClass('opened').find('.column-form').show();
clone.find( '.column-meta' ).replaceWith( column.find( '.column-meta' ) );
clone.find( '.column-form' ).replaceWith( column.find( '.column-form' ) );
// Increment clone id
clone.cpac_update_clone_id( storage_model );
// Load clone
column.replaceWith( clone );
clone.cpac_column_refresh();
}
}
} );
/** change label */
column.find('.column_label .input input').bind( 'keyup change', function() {
var value = jQuery( this ).val();
jQuery(this).closest('.cpac-column').find( 'td.column_label .inner > a.toggle' ).text( value );
});
/** width */
// slider
column.column_width_slider();
// indicator
var width_indicator = column.find('.column-meta span.width');
width_indicator.on( 'update', function(){
var _width = column.find('input.width').val();
var _unit = column.find('input.unit').filter(':checked').val();
if ( _width > 0 ) {
jQuery(this).text( _width + _unit );
} else {
jQuery(this).text('');
}
});
// unit selector
var width_unit_select = column.find('.column_width .unit-select label');
width_unit_select.on( 'click', function(){
column.find('span.unit').text( jQuery(this).find('input').val() );
column.column_width_slider(); // re-init slider
width_indicator.trigger('update'); // update indicator
});
// width_input
var width_input = column.find('input.width')
.on( 'keyup', function() {
column.column_width_slider(); // re-init slider
jQuery(this).trigger('validate'); // validate input
width_indicator.trigger('update'); // update indicator
})
// width_input:validate
.on( 'validate', function() {
var _width = width_input.val();
var _new_width = jQuery.trim( _width );
if ( ! jQuery.isNumeric( _new_width ) ) {
_new_width = _new_width.replace(/\D/g,'');
}
if ( _new_width.length > 3 ) {
_new_width = _new_width.substring(0, 3);
}
if ( _new_width <= 0 ) {
_new_width = '';
}
if ( _new_width !== _width ) {
width_input.val( _new_width );
}
});
/** display custom image size */
column.find('.column_image_size label.custom-size').click( function(){
var parent = jQuery(this).closest('.input');
if ( jQuery(this).hasClass('image-size-custom') ) {
jQuery('.custom-size-w', parent).removeClass('hidden');
jQuery('.custom-size-h', parent).removeClass('hidden');
}
else {
jQuery('.custom-size-w', parent).addClass('hidden');
jQuery('.custom-size-h', parent).addClass('hidden');
}
});
/** tooltip */
column.find('.column-form .label label, .column-form .label .info').hover(function(){
jQuery(this).parents( '.label' ).find('p.description').show();
},function(){
jQuery(this).parents( '.label' ).find('p.description').hide();
});
if ( column.find( '.column_type select' ).val() == 'column-meta' ) {
column.find( '.column_field_type select' ).change( function() {
column.cpac_column_refresh();
} );
}
};
/*
* Column: remove from DOM
*
* @since 2.0
*/
jQuery.fn.column_remove = function() {
jQuery(this).addClass('deleting').animate({ opacity : 0, height: 0 }, 350, function(e) {
jQuery(this).remove();
});
};
/*
* Column: remove from DOM
*
* @since 2.0
*/
jQuery.fn.column_width_slider = function() {
var column_width = jQuery(this).find('.column_width');
var input_width = column_width.find('input.width'),
input_unit = column_width.find('input.unit'),
unit = input_unit.filter(':checked').val(),
width = input_width.val(),
slider = column_width.find('.width-slider'),
indicator = jQuery(this).find('.column-meta span.width');
// width
if ( '%' == unit && width > 100 ) {
width = 100;
}
input_width.val( width );
slider.slider({
range: 'min',
min: 0,
max: '%' == unit ? 100 : 500,
value: width,
slide: function( event, ui ) {
input_width.val( ui.value );
indicator.trigger('update');
input_width.trigger('validate');
}
});
};
/*
* Column: clone
*
* @since 2.3.4
*/
jQuery.fn.column_clone = function() {
var container = jQuery( this ).closest( '.columns-container' );
var column = jQuery( this );
if ( typeof column.attr( 'data-clone' ) === 'undefined' ) {
var message = cpac_i18n.clone.replace( '%s', '<strong>' + column.find( '.column_label .toggle' ).text() + '</strong>' );
/*var el_message = jQuery( '<div class="cpac_message error"><p>' + message + '</p></div>' );
container.find( '.cpac-boxes' ).before( el_message );
el_message.hide().slideDown().delay( 2500 ).slideUp( function() {
jQuery( this ).remove();
} );*/
column.addClass( 'opened' ).find( '.column-form' ).slideDown( 150 );
column.find( '.msg' ).html( message ).show();
return;
}
var clone = jQuery( this ).clone();
clone.cpac_update_clone_id( container.attr( 'data-type' ) );
jQuery( this ).after( clone );
// rebind toggle events
clone.column_bind_toggle();
// rebind remove events
clone.column_bind_remove();
// rebind clone events
clone.column_bind_clone();
// rebind all other events
clone.column_bind_events();
// reinitialize sortability
cpac_sortable();
// hook for addons
jQuery( document ).trigger( 'column_add', clone );
return clone;
};
/*
* Update clone ID
*
* @since 2.0
*/
jQuery.fn.cpac_update_clone_id = function( storage_model ) {
var el = jQuery( this );
var type = el.attr( 'data-type' );
var all_columns = jQuery( '.columns-container[data-type="' + storage_model + '"]').find( '.cpac-columns' );
var columns = jQuery( all_columns ).find( '*[data-type="' + type + '"]' ).not( el );
/* var type = el.attr( 'data-type' );
var all_columns = el.closest( '.cpac-boxes' ).find( '.cpac-columns' );
var columns = jQuery( all_columns ).find( '*[data-type="' + type + '"]' ).not( el );*/
// get clone ID
var ids = jQuery.map( columns, function( e, i ) {
if ( jQuery(e).attr('data-clone') ){
return parseInt( jQuery( e ).attr( 'data-clone' ), 10 );
}
return 0;
});
ids.sort();
var max_id = Math.max.apply( null, ids ) + 1;
for ( var id=0; id<=max_id; id++ ) {
if ( -1 === jQuery.inArray( id, ids ) )
break;
}
// only increment when needed
if ( 0 === id )
return;
// get original clone ID
var clone_id = el.attr( 'data-clone' );
var clone_suffix = '';
if ( clone_id ) {
clone_suffix = '-' + clone_id;
}
// set clone ID
el.attr( 'data-clone', id );
el.find( 'input.clone' ).val( id );
el.find( 'input.column-name' ).val( type + '-' + id );
// update input names with clone ID
var inputs = el.find( 'input, select, label' );
jQuery( inputs ).each( function( i, v ) {
var new_name = type + '-' + id;
// name
if( jQuery(v).attr( 'name' ) ) {
jQuery(v).attr( 'name', jQuery(v).attr( 'name' ).replace( type + clone_suffix, new_name) );
}
// for
if( jQuery(v).attr( 'for' ) ) {
jQuery(v).attr( 'for', jQuery(v).attr( 'for' ).replace( type + clone_suffix, new_name ) );
}
// id
if( jQuery(v).attr( 'id' ) ) {
jQuery(v).attr( 'id', jQuery(v).attr( 'id' ).replace( type + clone_suffix, new_name ) );
}
});
};
function cpac_create_column( container ) {
var clone = jQuery( '.for-cloning-only .cpac-column', container ).first().clone();
var storage_model = container.attr( 'data-type' );
if ( clone.length > 0 ) {
// increment clone id ( before adding to DOM, otherwise radio buttons will reset )
clone.cpac_update_clone_id( storage_model );
// add to DOM
jQuery( '.cpac-columns form', container ).append( clone );
// rebind toggle events
clone.column_bind_toggle();
// rebind remove events
clone.column_bind_remove();
// rebind clone events
clone.column_bind_clone();
// rebind all other events
clone.column_bind_events();
// reinitialize sortability
cpac_sortable();
// hook for addons
jQuery( document ).trigger( 'column_add', clone );
}
return clone;
}
/*
* Add Column
*
* @since 2.0
*/
function cpac_add_column() {
jQuery( '#cpac .add_column' ).click( function( e ) {
var container = jQuery( this ).closest( '.columns-container' );
var clone = cpac_create_column( container );
// open settings
clone.addClass('opened').find('.column-form').slideDown(150, function(){
jQuery('html, body').animate({ scrollTop: clone.offset().top - 58 }, 300);
});
e.preventDefault();
});
}
/**
* @since 2.2.1
*/
function cpac_sidebar_feedback() {
jQuery( function( $ ) {
var sidebox = $( '.sidebox#direct-feedback' );
sidebox.find( '#feedback-choice a.no' ).click( function( e ) {
e.preventDefault();
sidebox.find( '#feedback-choice' ).slideUp();
sidebox.find( '#feedback-support' ).slideDown();
} );
sidebox.find( '#feedback-choice a.yes' ).click( function( e ) {
e.preventDefault();
sidebox.find( '#feedback-choice' ).slideUp();
sidebox.find( '#feedback-rate' ).slideDown();
} );
} );
}
/*
* Sidebar Scroll
*
* @since 1.5
*/
/*function cpac_sidebar_scroll() {
if ( jQuery('.columns-right-inside').length === 0 ) {
return;
}
if ( jQuery('.columns-right-inside:visible').offset() ) {
var sidebar = jQuery('.columns-right-inside:visible');
var top = sidebar.offset().top - parseFloat( sidebar.css('margin-top').replace(/auto/, 0) ) - 70;
var viewport_height = jQuery(window).height();
var sidebar_height = sidebar.height();
jQuery(window).scroll(function (event) {
var y = jQuery(this).scrollTop();
// top position of div#cpac is calculated everytime incase of an opened help screen
var offset = jQuery('#cpac').offset().top - parseFloat( jQuery('#cpac').css('margin-top').replace(/auto/, 0) );
var sidebar_fits_on_screen = sidebar_height < ( viewport_height - 32 ); // adminbar
// whether that's below
if ( ( y >= top + offset ) && sidebar_fits_on_screen ) {
jQuery('.columns-right-inside:visible').addClass('fixed');
} else {
jQuery('.columns-right-inside:visible').removeClass('fixed');
}
});
}
}*/
/*
* Clear Input Defaults
*
*/
function cpac_clear_input_defaults() {
jQuery.fn.cleardefault = function() {
return this.focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
};
jQuery("#cpac-box-plugin_settings .addons input").cleardefault();
}
/*
* Help
*
* usage: <a href="javascript:;" class="help" data-help="tab-2"></a>
*/
function cpac_help() {
jQuery('#cpac a.help').click( function(e) {
e.preventDefault();
var panel = jQuery('#contextual-help-wrap');
panel.parent().show();
jQuery('a[href="#tab-panel-cpac-' + jQuery(this).attr('data-help') + '"]', panel).trigger('click');
panel.slideDown( 'fast', function() {
panel.focus();
});
});
}
/*
* WP Pointer
*
*/
function cpac_pointer() {
jQuery('.cpac-pointer').each(function(){
// vars
var el = jQuery(this),
html = el.attr('rel'),
pos = el.attr('data-pos');
var position = {
at: 'left top', // position of wp-pointer relative to the element which triggers the pointer event
my: 'right top', // position of wp-pointer relative to the at-coordinates
edge: 'right', // position of arrow
offset: '0 0' // offset for wp-pointer
};
if ( 'right' == pos ) {
position = {
at: 'right middle',
my: 'left middle',
edge: 'left'
};
}
// create pointer
el.pointer({
content: jQuery('#' + html).html(),
position: position,
pointerWidth: 250,
close: function() {
el.removeClass('open');
},
// bug fix. with an arrow on the right side the position of wp-pointer is incorrect. it does not take
// into account the padding of the arrow. adding "wp-pointer-' + position.edge" will fix that.
pointerClass: 'wp-pointer wp-pointer-' + position.edge
});
// click
el.click( function() {
if( el.hasClass('open') ) {
el.removeClass('open');
}
else {
el.addClass('open');
}
});
// show on hover
el.hover( function() {
jQuery(this).pointer('open');
}, function() {
if( ! el.hasClass('open') ) {
jQuery(this).pointer('close');
}
});
});
}
/*
* Sortable
*
* @since 1.5
*/
function cpac_sortable() {
jQuery( 'div.cpac-columns' ).each( function() {
if ( jQuery( this ).hasClass( 'ui-sortable' ) ) {
jQuery( this ).sortable( 'refresh' );
}
else {
jQuery( this ).sortable( {
items : '.cpac-column'
} );
}
} );
}
/*
* Menu
*
* @since 1.5
*/
function cpac_menu() {
var menu = jQuery('#cpac div.cpac-menu');
// click
menu.find('a').click( function(e, el) {
var id = jQuery(this).attr('href');
if ( id ) {
var type = id.replace('#cpac-box-','');
// remove current
jQuery('.cpac-menu a').removeClass('current');
jQuery('.columns-container').hide();
// set current
jQuery(this).addClass('current');
var container = jQuery('.columns-container[data-type="' + type + '"]').show();
var columns = container.find( '.cpac-columns' );
// hook for addons
jQuery( document ).trigger( 'cac_menu_change', columns );
}
// re init sidebar scroll
//cpac_sidebar_scroll();
e.preventDefault();
});
// activate first menu
menu.find('a.current').trigger('click');
}
/*
* Bind events: triggered after column is init, changed or added
*
*/
jQuery( document ).bind('column_init column_change column_add', function( e, column ){
jQuery( column ).cpac_bind_column_addon_events();
jQuery( column ).cpac_bind_container_addon_events();
});
/*
* Optional Radio Click events
*
*/
jQuery.fn.cpac_bind_column_addon_events = function() {
var column = jQuery( this );
var inputs = column.find('[data-toggle-id] label');
inputs.on( 'click', function(){
var id = jQuery( this ).closest('td.input').data('toggle-id');
var state = jQuery( 'input', this ).val();
// Toggle indicator icon
var label = column.find('[data-indicator-id="' + id + '"]' ).removeClass( 'on' );
if ( 'on' == state ) {
label.addClass( 'on' );
}
// Toggle additional options
var additional = column.find('[data-additional-option-id="' + id + '"]' ).addClass( 'hide' );
if ( 'on' == state ) {
additional.removeClass( 'hide' );
}
});
// Hide additonal options on ready
column.find('[data-toggle-id]').each( function(){
var additional = column.find('[data-additional-option-id="' + jQuery( this ).data('toggle-id') + '"]' ).addClass( 'hide' );
if ( 'on' == jQuery( 'input:checked', this ).val() ) {
additional.removeClass( 'hide' );
}
});
};
/*
* Indicator Click Events
*
*/
jQuery.fn.cpac_bind_container_addon_events = function() {
var column = jQuery( this );
var indicator = column.find('[data-indicator-id]');
indicator.unbind('click').click( function() {
var id = jQuery( this ).data('indicator-id');
var radio = column.find('[data-toggle-id="' + id + '"] input' );
if ( jQuery( this ).hasClass('on') ) {
jQuery( this ).removeClass('on').addClass('off');
radio.filter('[value=off]').prop('checked', true);
}
else {
jQuery( this ).removeClass('off').addClass('on');
radio.filter('[value=on]').prop('checked', true);
}
});
};