Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
211 lines (183 loc) · 4.84 KB
/
Copy pathscript.js
File metadata and controls
211 lines (183 loc) · 4.84 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
/**
* QR Code Creator — admin script.
*
* Generates QR codes locally in the browser using the bundled qr-code-styling
* library. No data is sent to any third-party service.
*
* @package QRCodeCreator
* @since 1.0.0
*/
( function ( $ ) {
'use strict';
var settings = window.qrCodeCreator || {};
var i18n = settings.i18n || {};
var qrCode = null;
var mediaFrame = null;
/**
* Read the current form values into a qr-code-styling options object.
*
* @return {Object}
*/
function buildOptions() {
var size = parseInt( $( '#qr_code_size' ).val(), 10 ) || 200;
var fg = $( '#qr_code_color' ).val() || '#000000';
var bg = $( '#qr_code_bgcolor' ).val() || '#ffffff';
var ecc = $( '#qr_code_ecc' ).val() || 'M';
var logo = $( '#qr_code_logo' ).val();
var options = {
width: size,
height: size,
type: 'canvas',
data: $( '#qr_code_content' ).val(),
margin: 10,
qrOptions: {
errorCorrectionLevel: ecc
},
dotsOptions: {
color: fg,
type: 'square'
},
cornersSquareOptions: {
color: fg
},
cornersDotOptions: {
color: fg
},
backgroundOptions: {
color: bg
}
};
if ( logo ) {
options.image = logo;
options.imageOptions = {
crossOrigin: 'anonymous',
margin: 5,
imageSize: 0.4,
hideBackgroundDots: true
};
}
return options;
}
/**
* Show an error message in the result column.
*
* @param {string} message Message text.
*/
function showError( message ) {
$( '#qr_code_error' ).find( 'p' ).text( message );
$( '#qr_code_error' ).show();
$( '#qr_code_loading, #qr_code_preview, #qr_code_download' ).hide();
}
/**
* Generate (or regenerate) the QR code.
*/
function generate() {
var content = $.trim( $( '#qr_code_content' ).val() );
$( '#qr_code_error' ).hide();
if ( ! content ) {
showError( i18n.errorEmpty || 'Please enter some content to generate QR code.' );
return;
}
if ( 'undefined' === typeof window.QRCodeStyling ) {
showError( i18n.errorGenerate || 'Error generating QR code. Please try again.' );
return;
}
try {
var options = buildOptions();
// Render into a fresh container each time.
$( '#qr_code' ).empty();
qrCode = new window.QRCodeStyling( options );
qrCode.append( document.getElementById( 'qr_code' ) );
$( '#qr_code_loading' ).hide();
$( '#qr_code_preview, #qr_code_download' ).show();
} catch ( e ) {
showError( i18n.errorGenerate || 'Error generating QR code. Please try again.' );
}
}
/**
* Download the current QR code in the given format.
*
* @param {string} extension png or svg.
*/
function download( extension ) {
if ( ! qrCode ) {
return;
}
qrCode.download( {
name: 'qr-code',
extension: extension
} );
}
/**
* Reset the form and clear the result.
*/
function reset() {
$( '#qr_code_content' ).val( '' );
$( '#qr_code_size' ).val( '200' );
$( '#qr_code_ecc' ).val( 'H' );
$( '#qr_code_color' ).val( '#000000' );
$( '#qr_code_bgcolor' ).val( '#ffffff' );
$( '#qr_code_logo' ).val( '' );
$( '#qr_code' ).empty();
$( '#qr_code_logo_preview' ).hide().find( 'img' ).attr( 'src', '' );
$( '#qr_code_logo_remove' ).hide();
$( '#qr_code_loading, #qr_code_preview, #qr_code_download, #qr_code_error' ).hide();
qrCode = null;
$( '#qr_code_content' ).trigger( 'focus' );
}
/**
* Show the chosen logo preview.
*
* @param {string} url Image URL.
*/
function showLogoPreview( url ) {
$( '#qr_code_logo_preview' ).show().find( 'img' ).attr( 'src', url );
$( '#qr_code_logo_remove' ).show();
}
/**
* Open the WordPress media library to pick a logo.
*/
function chooseLogo() {
if ( mediaFrame ) {
mediaFrame.open();
return;
}
mediaFrame = wp.media( {
title: i18n.mediaTitle || 'Choose a logo image',
button: { text: i18n.mediaButton || 'Use this image' },
library: { type: 'image' },
multiple: false
} );
mediaFrame.on( 'select', function () {
var attachment = mediaFrame.state().get( 'selection' ).first().toJSON();
$( '#qr_code_logo' ).val( attachment.url );
showLogoPreview( attachment.url );
} );
mediaFrame.open();
}
$( function () {
$( '#create_qr_code' ).on( 'click', generate );
$( '#reset_qr_code' ).on( 'click', reset );
$( '#qr_code_content' ).on( 'keypress', function ( e ) {
if ( 13 === e.which && e.ctrlKey ) {
generate();
}
} );
$( '#qr_code_download_png' ).on( 'click', function () {
download( 'png' );
} );
$( '#qr_code_download_svg' ).on( 'click', function () {
download( 'svg' );
} );
$( '#qr_code_logo_upload' ).on( 'click', function ( e ) {
e.preventDefault();
chooseLogo();
} );
$( '#qr_code_logo_remove' ).on( 'click', function ( e ) {
e.preventDefault();
$( '#qr_code_logo' ).val( '' );
$( '#qr_code_logo_preview' ).hide().find( 'img' ).attr( 'src', '' );
$( this ).hide();
} );
} );
} )( jQuery );