PluginProbe
TablePress – Tables in WordPress made easy / 1.12
TablePress – Tables in WordPress made easy v1.12
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / admin / js / import.js

import.js in TablePress – Tables in WordPress made easy 1.12, at admin/js/import.js

128 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * JavaScript code for the "Import" screen
3 *
4 * @package TablePress
5 * @subpackage Views JavaScript
6 * @author Tobias Bäthge
7 * @since 1.0.0
8 */
9
10 jQuery( document ).ready( function( $ ) {
11
12 'use strict';
13
14 /**
15 * File extension of the file that is imported.
16 */
17 var extension = 'csv';
18
19 /**
20 * Show select box for table to replace only if needed.
21 *
22 * @since 1.0.0
23 */
24 $( '#row-import-type' ).on( 'change', 'input', function() {
25 var import_type = $( this ).val();
26 $( '#tables-import-existing-table' ).prop( 'disabled', ( ( 'replace' !== import_type && 'append' !== import_type ) || 'zip' === extension ) );
27 } )
28 .find( 'input:checked' ).change();
29
30 /**
31 * Show only the import source field that was selected with the radio button.
32 *
33 * @since 1.0.0
34 */
35 $( '#row-import-source' ).on( 'change', 'input', function() {
36 $( '#row-import-source-file-upload, #row-import-source-url, #row-import-source-server, #row-import-source-form-field' ).hide();
37 $( '#row-import-source-' + $(this).val() ).show();
38 } )
39 .find( 'input:checked' ).change();
40
41 /**
42 * Select correct value in import format dropdown on file select.
43 *
44 * @since 1.0.0
45 */
46 $( '#tables-import-file-upload, #tables-import-url, #tables-import-server' ).on( 'change', function( event ) {
47 var path = $(this).val(),
48 import_type = $( '#row-import-type' ).find( 'input:checked' ).val(),
49 filename_start,
50 extension_start,
51 filename = path;
52
53 // Default extension: CSV for file upload and server, HTML for URL.
54 if ( 'tables-import-url' === event.target.id ) {
55 extension = 'html';
56 }
57 // Determine filename from full path.
58 filename_start = path.lastIndexOf( '\\' );
59 if ( -1 !== filename_start ) { // Windows-based path
60 filename = path.substr( filename_start + 1 );
61 } else {
62 filename_start = path.lastIndexOf( '/' );
63 if ( -1 !== filename_start ) { // Windows-based path
64 filename = path.substr( filename_start + 1 );
65 }
66 }
67 // Determine extension from filename.
68 extension_start = filename.lastIndexOf( '.' );
69 if ( -1 !== extension_start ) {
70 extension = filename.substr( extension_start + 1 ).toLowerCase();
71 }
72
73 // Allow .htm for HTML as well.
74 if ( 'htm' === extension ) {
75 extension = 'html';
76 }
77
78 $( '#tables-import-existing-table' ).prop( 'disabled', ( ( 'replace' !== import_type && 'append' !== import_type ) || 'zip' === extension ) );
79
80 // Don't change the format for ZIP archives.
81 if ( 'zip' === extension ) {
82 return;
83 }
84
85 $( '#tables-import-format' ).val( extension );
86 } );
87
88 /**
89 * Check, whether inputs are valid
90 *
91 * @since 1.0.0
92 */
93 $( '#tablepress-page' ).find( 'form' ).on( 'submit.tablepress', function( /* event */ ) {
94 var import_source = $( '#row-import-source' ).find( 'input:checked' ).val(),
95 selected_import_source_field = $( '#tables-import-' + import_source ).get(0),
96 valid_form = true,
97 import_type = $( '#row-import-type' ).find( 'input:checked' ).val();
98
99 // Te value of the selected import source field must be set/changed from the default.
100 if ( selected_import_source_field.defaultValue === selected_import_source_field.value ) {
101 $( selected_import_source_field )
102 .addClass( 'invalid' )
103 .one( 'change', function() { $(this).removeClass( 'invalid' ); } )
104 .focus().select();
105 valid_form = false;
106 }
107
108 // If replace or append is selected, a table must be selected - except for ZIP files.
109 if ( ( 'replace' === import_type || 'append' === import_type ) && 'zip' !== extension ) {
110 if ( '' === $( '#tables-import-existing-table' ).val() ) {
111 $( '#row-import-type' )
112 .one( 'change', 'input', function() { $( '#tables-import-existing-table' ).removeClass( 'invalid' ); } );
113 $( '#tables-import-existing-table' )
114 .addClass( 'invalid' )
115 .one( 'change', function() { $(this).removeClass( 'invalid' ); } )
116 .focus().select();
117 valid_form = false;
118 }
119 }
120
121 // Don't submit the form if it's not valid.
122 if ( ! valid_form ) {
123 return false;
124 }
125 } );
126
127 } );
128