scratch – Diff between revs 58 and 125
?pathlinks?
Rev 58 | Rev 125 | |||
---|---|---|---|---|
1 | /* =========================================================== |
1 | /* =========================================================== |
|
2 | * trumbowyg.base64.js v1.0 |
2 | * trumbowyg.base64.js v1.0 |
|
3 | * Base64 plugin for Trumbowyg |
3 | * Base64 plugin for Trumbowyg |
|
4 | * http://alex-d.github.com/Trumbowyg |
4 | * http://alex-d.github.com/Trumbowyg |
|
5 | * =========================================================== |
5 | * =========================================================== |
|
6 | * Author : Cyril Biencourt (lizardK) |
6 | * Author : Cyril Biencourt (lizardK) |
|
7 | */ |
7 | */ |
|
8 | |
8 | |
|
9 | (function ($) { |
9 | (function ($) { |
|
10 | 'use strict'; |
10 | 'use strict'; |
|
11 | |
11 | |
|
12 | var isSupported = function () { |
12 | var isSupported = function () { |
|
13 | return typeof FileReader !== 'undefined'; |
13 | return typeof FileReader !== 'undefined'; |
|
14 | }; |
14 | }; |
|
15 | |
15 | |
|
16 | var isValidImage = function (type) { |
16 | var isValidImage = function (type) { |
|
17 | return /^data:image\/[a-z]?/i.test(type); |
17 | return /^data:image\/[a-z]?/i.test(type); |
|
18 | }; |
18 | }; |
|
19 | |
19 | |
|
20 | $.extend(true, $.trumbowyg, { |
20 | $.extend(true, $.trumbowyg, { |
|
21 | langs: { |
21 | langs: { |
|
22 | // jshint camelcase:false |
22 | // jshint camelcase:false |
|
23 | en: { |
23 | en: { |
|
24 | base64: 'Image as base64', |
24 | base64: 'Image as base64', |
|
25 | file: 'File', |
25 | file: 'File', |
|
26 | errFileReaderNotSupported: 'FileReader is not supported by your browser.', |
26 | errFileReaderNotSupported: 'FileReader is not supported by your browser.', |
|
27 | errInvalidImage: 'Invalid image file.' |
27 | errInvalidImage: 'Invalid image file.' |
|
28 | }, |
28 | }, |
|
29 | fr: { |
29 | fr: { |
|
30 | base64: 'Image en base64', |
30 | base64: 'Image en base64', |
|
31 | file: 'Fichier' |
31 | file: 'Fichier' |
|
32 | }, |
32 | }, |
|
33 | cs: { |
33 | cs: { |
|
34 | base64: 'Vložit obrázek', |
34 | base64: 'Vložit obrázek', |
|
35 | file: 'Soubor' |
35 | file: 'Soubor' |
|
36 | }, |
36 | }, |
|
37 | zh_cn: { |
37 | zh_cn: { |
|
38 | base64: '图片(Base64编码)', |
38 | base64: '图片(Base64编码)', |
|
39 | file: '文件' |
39 | file: '文件' |
|
40 | }, |
40 | }, |
|
41 | nl: { |
41 | nl: { |
|
42 | errFileReaderNotSupported: 'Uw browser ondersteunt deze functionaliteit niet.', |
42 | errFileReaderNotSupported: 'Uw browser ondersteunt deze functionaliteit niet.', |
|
43 | errInvalidImage: 'De gekozen afbeelding is ongeldig.' |
43 | errInvalidImage: 'De gekozen afbeelding is ongeldig.' |
|
- | 44 | }, |
||
- | 45 | ja: { |
||
- | 46 | base64: '画像 (Base64形式)', |
||
- | 47 | file: 'ファイル', |
||
- | 48 | errFileReaderNotSupported: 'あなたのブラウザーはFileReaderをサポートしていません', |
||
- | 49 | errInvalidImage: '画像形式が正しくありません' |
||
44 | } |
50 | } |
|
45 | }, |
51 | }, |
|
46 | // jshint camelcase:true |
52 | // jshint camelcase:true |
|
47 | |
53 | |
|
48 | plugins: { |
54 | plugins: { |
|
49 | base64: { |
55 | base64: { |
|
50 | shouldInit: isSupported, |
56 | shouldInit: isSupported, |
|
51 | init: function (trumbowyg) { |
57 | init: function (trumbowyg) { |
|
52 | var btnDef = { |
58 | var btnDef = { |
|
53 | isSupported: isSupported, |
59 | isSupported: isSupported, |
|
54 | fn: function () { |
60 | fn: function () { |
|
55 | trumbowyg.saveRange(); |
61 | trumbowyg.saveRange(); |
|
56 | |
62 | |
|
57 | var file; |
63 | var file; |
|
58 | var $modal = trumbowyg.openModalInsert( |
64 | var $modal = trumbowyg.openModalInsert( |
|
59 | // Title |
65 | // Title |
|
60 | trumbowyg.lang.base64, |
66 | trumbowyg.lang.base64, |
|
61 | |
67 | |
|
62 | // Fields |
68 | // Fields |
|
63 | { |
69 | { |
|
64 | file: { |
70 | file: { |
|
65 | type: 'file', |
71 | type: 'file', |
|
66 | required: true, |
72 | required: true, |
|
67 | attributes: { |
73 | attributes: { |
|
68 | accept: 'image/*' |
74 | accept: 'image/*' |
|
69 | } |
75 | } |
|
70 | }, |
76 | }, |
|
71 | alt: { |
77 | alt: { |
|
72 | label: 'description', |
78 | label: 'description', |
|
73 | value: trumbowyg.getRangeText() |
79 | value: trumbowyg.getRangeText() |
|
74 | } |
80 | } |
|
75 | }, |
81 | }, |
|
76 | |
82 | |
|
77 | // Callback |
83 | // Callback |
|
78 | function (values) { |
84 | function (values) { |
|
79 | var fReader = new FileReader(); |
85 | var fReader = new FileReader(); |
|
80 | |
86 | |
|
81 | fReader.onloadend = function (e) { |
87 | fReader.onloadend = function (e) { |
|
82 | if (isValidImage(e.target.result)) { |
88 | if (isValidImage(e.target.result)) { |
|
83 | trumbowyg.execCmd('insertImage', fReader.result); |
89 | trumbowyg.execCmd('insertImage', fReader.result); |
|
84 | $(['img[src="', fReader.result, '"]:not([alt])'].join(''), trumbowyg.$box).attr('alt', values.alt); |
90 | $(['img[src="', fReader.result, '"]:not([alt])'].join(''), trumbowyg.$box).attr('alt', values.alt); |
|
85 | trumbowyg.closeModal(); |
91 | trumbowyg.closeModal(); |
|
86 | } else { |
92 | } else { |
|
87 | trumbowyg.addErrorOnModalField( |
93 | trumbowyg.addErrorOnModalField( |
|
88 | $('input[type=file]', $modal), |
94 | $('input[type=file]', $modal), |
|
89 | trumbowyg.lang.errInvalidImage |
95 | trumbowyg.lang.errInvalidImage |
|
90 | ); |
96 | ); |
|
91 | } |
97 | } |
|
92 | }; |
98 | }; |
|
93 | |
99 | |
|
94 | fReader.readAsDataURL(file); |
100 | fReader.readAsDataURL(file); |
|
95 | } |
101 | } |
|
96 | ); |
102 | ); |
|
97 | |
103 | |
|
98 | $('input[type=file]').on('change', function (e) { |
104 | $('input[type=file]').on('change', function (e) { |
|
99 | file = e.target.files[0]; |
105 | file = e.target.files[0]; |
|
100 | }); |
106 | }); |
|
101 | } |
107 | } |
|
102 | }; |
108 | }; |
|
103 | |
109 | |
|
104 | trumbowyg.addBtnDef('base64', btnDef); |
110 | trumbowyg.addBtnDef('base64', btnDef); |
|
105 | } |
111 | } |
|
106 | } |
112 | } |
|
107 | } |
113 | } |
|
108 | }); |
114 | }); |
|
109 | })(jQuery); |
115 | })(jQuery); |
|
110 | |
116 | |