admin-import-export.js
5.83 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
jQuery( document ).ready( function( $ ) {
/**
* Stores the selected file details for form imports.
*
* name is the filename from the user's computer, including extension.
* content is the base64 encoded contents as a result of using the HTML5 FileReader API.
* @type {Object}
*/
var importFormFile = {
name: '',
content: '',
extraChecksOff: false
};
/**
* Listen for clicks on our "import" button.
* It sets data for our batch processor and then instantiates a batch process.
*
* @since 3.4.0
* @param object e Click Event
* @return void
*/
$( document ).on( 'click', '#nf-import-form-submit', function( e ) {
// Make sure that our file field isn't empty.
if ( '' == importFormFile.name ) return false;
//Get status of trusted source checkbox
importFormFile.extraChecksOff = document.querySelector('#nf_import_form_turn_off_extra_checks').checked;
// Settings object for our batch processor
var settings = {
batch_type: 'import_form',
extraData: importFormFile,
loadingText: 'Importing...',
onCompleteCallback: function( response ) {
// If we don't get back a form ID, then bail.
if ( 'undefined' == typeof response.form_id ) return false;
jQuery( '#nf-import-file' ).val('');
jQuery( '#nf-import-url' ).attr( 'href', nfAdmin.builderURL + response.form_id );
var blockingErrors = false;
// If we have errors...
if ( 'undefined' != typeof response.errors ) {
var errorOutput = '';
// Record them.
response.errors.forEach(
function( error ) {
// Block success if one was fatal.
if ( 'fatal' == error.type ) {
blockingErrors = true;
}
console.error( error.type + ': ' + error.code );
errorOutput += '<p>' + error.message + '</p>';
}
);
jQuery( '#row-nf-import-response-error td' ).html( errorOutput );
jQuery( '#row-nf-import-response-error' ).show();
}
if ( ! blockingErrors ) {
jQuery( '#row-nf-import-response' ).show();
}
}
}
/**
* Instantiate our batch processor.
*
* This will open the modal and present the user with content and buttons.
*/
new NinjaBatchProcessor( settings );
} );
$( document ).on( 'change', '#nf_import_fields', function( e ) {
jQuery( '#nf_import_security' ).val(nfAdmin.batchNonce);
});
/**
* Selecting a file within an input field triggers a jQuery change event.
*
* When we select a form file to import, we need to do a few things:
*
* Disable the primary button of our batch processing modal.
* Grab the file and make sure that it has a .nff extension.
* Read the contents and base64 encode them using the HTML5 FileReader API.
* Set the contents to our importFormFile variable.
*
* @since 3.4.0
* @param object e Change Event
* @return {[type]} [description]
*/
$( document ).on( 'change', '#nf-import-file', function( e ) {
// Hide our success message.
jQuery( '#row-nf-import-response' ).hide();
jQuery( '#row-nf-import-response-error' ).hide();
// Hide our extension type error.
jQuery( '#row-nf-import-type-error' ).hide();
// Grab the file from the input.
var file = e.target.files[0];
// If our file var is empty, bail.
if ( ! file ) {
return false;
}
// Use some Regex to get the extension
var extension = file.name.match(/\.[0-9a-z]+$/i);
// If we don't have a .nff extension, show our type error and bail.
if ( '.nff' !== extension[0] ) {
jQuery( '#row-nf-import-type-error' ).show();
importFormFile.name = '';
importFormFile.content = '';
return false;
}
// Instantiate the HTML5 FileReader API.
var reader = new FileReader();
/**
* When the HTML5 API says that we've successfully loaded the file contents:
* Set our importFormFile var.
* Enable our batch processor primary button.
* We use Javascript's addEventListener to update our var.
*/
reader.addEventListener( 'load', function () {
importFormFile.name = file.name;
importFormFile.content = reader.result;
}, false);
// Use the readAsDataURL method of the FileReader API.
reader.readAsDataURL( file );
} );
var clickedElement;
$(document).off( 'mousedown' ).on( 'mousedown', function( e ) {
clickedElement = e.target;
});
$( '#nf_export_form_2' ).off( 'focus' ).on( 'focus', function () {
//show the dropdown on focus of the input
$( '.nf-form-dropdown' ).show();
});
$( '#nf_export_form_2' ).off( 'keyup' ).on( 'keyup', function () {
//show the dropdown if it isn't show
$( '.nf-form-dropdown' ).show();
// get the value of the input, which we filter on
var filter = $( this ).val();
if( '' === filter ) {
//if the filter val is empty, show all form options
$( '.nf-form-dropdown' ).find( 'li' ).show();
} else {
$.each( $( '#nf_form_export_options span' ), function ( index, span ) {
var tmpSpan = $( span );
// test to see if span text contains the entered value
if ( 0 <= tmpSpan.text().toLowerCase().indexOf( filter.toLowerCase() ) ) {
// shows options that DO contain the text entered
tmpSpan.parent().show();
} else {
// hides options the do not contain the text entered
tmpSpan.parent().hide();
}
});
}
});
$( '#nf_export_form_2' ).off( 'blur' ).on( 'blur' , function( e ) {
if( 'undefined' !== typeof clickedElement ) {
if ( ! $( clickedElement ).hasClass( 'nf-form-option-item' ) ) {
$( '#nf_export_form_2' ).val( '' );
$( '.nf-form-dropdown' ).hide();
}
}
});
$( '.nf-form-option' ).off( 'click' ).on( 'click', function() {
// on click get the value of the input
var val = $( this ).data( 'val' );
// nf_export_form is now a hidden field instead of select element
$( '#nf_export_form' ).val( val );
// set the text of the input field
$( '#nf_export_form_2' ).val( '' );
// and hide the option.
$( '.nf-form-dropdown' ).hide();
});
});