forked from voceconnect/widget-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget-data.js
105 lines (95 loc) · 3.09 KB
/
widget-data.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
!function ($) {
"use strict";
/**
* Display the notification div, populate a message, and set a CSS class
* @param String message Message to display
* @param String className CSS class of error or success
*/
var show_notification = function(message, className){
var notification = $('div#notifier').empty().removeClass('error updated');
notification.html('<p>' + message + '</p>');
notification.addClass(className);
notification.fadeIn('slow');
jQuery('body,html').animate({
scrollTop: 0
}, 800);
},
wrapper = $('<div/>').css({
height:0,
width:0,
'overflow':'hidden'
});
$(function () {
var fileInput = $('#widget-upload-file').wrap(wrapper),
widgetCheckboxes = $('.widget-data .widget-checkbox'),
widgetSelectionError = $('.widget-data p.widget-selection-error');
/**
* Handle click events for widget-data to select all checkboxes on click, to uncheck all
* checkboxes on click, and to activate the file upload when the file upload button is clicked.
* @param Object e Event object
*/
$('.widget-data').on('click', '.select-all, .unselect-all, .upload-button', function(e){
e.preventDefault();
if( $(this).hasClass('select-all') ){
widgetCheckboxes.not(":checked").each(function(){
$(this).attr( 'checked', true );
});
} else if( $(this).hasClass('unselect-all') ){
widgetCheckboxes.filter(":checked").each(function(){
$(this).attr( 'checked', false );
});
} else if( $(this).hasClass('upload-button') ){
fileInput.click();
}
});
/**
* Handle the export form submission
* @param Object e Event object
*/
$('form#widget-export-settings').submit(function(e) {
// return and show notification if no widgets are selected
if (widgetCheckboxes.filter(':checked').length === 0) {
e.preventDefault();
show_notification('Please select a widget to continue.', 'error');
return;
}
var message = 'All of the requested widgets have been exported.';
$('form#widget-export-settings').fadeOut('slow');
window.setTimeout(function () {
window.location.replace(widgets_url);
}, 4000);
show_notification(message, 'updated');
});
/***
* Handle imports
* @param Object e Event object
*/
$('form#import-widget-data').submit(function(e){
e.preventDefault();
if (widgetCheckboxes.filter(':checked').length === 0) {
widgetSelectionError.fadeIn('slow').delay(2000).fadeOut('slow');
return false;
}
var message, newClass;
$.post( ajaxurl, $("#import-widget-data").serialize(), function(r){
var res = wpAjax.parseAjaxResponse(r, 'notifier');
if( ! res )
return;
$('.import-wrapper').fadeOut('slow');
show_notification('All widgets with registered sidebars have been imported successfully.', 'updated');
// window.setTimeout(function () {
// window.location.replace(widgets_url);
// }, 4000);
});
});
/**
*
*/
fileInput.change(function(){
var outputText = $('#upload-widget-data .file-name'),
sub = $(this).val().lastIndexOf('\\') + 1,
filename = $(this).val().substring(sub);
outputText.val(filename);
});
});
}(window.jQuery);