/
plugin.js
933 lines (777 loc) · 27.8 KB
/
plugin.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* CKEditor 4 LTS ("Long Term Support") is available under the terms of the Extended Support Model.
*/
( function() {
'use strict';
var stylesLoaded = false;
function loadStyles( editor, plugin ) {
if ( !stylesLoaded ) {
CKEDITOR.document.appendStyleSheet( plugin.path + 'styles/imagebase.css' );
stylesLoaded = true;
}
if ( editor.addContentsCss ) {
editor.addContentsCss( plugin.path + 'styles/imagebase.css' );
}
}
function getFocusedWidget( editor ) {
var widgets = editor.widgets,
currentActive = editor.focusManager.currentActive;
if ( !editor.focusManager.hasFocus ) {
return;
}
if ( widgets.focused ) {
return widgets.focused;
}
if ( currentActive instanceof CKEDITOR.plugins.widget.nestedEditable ) {
return widgets.getByElement( currentActive );
}
}
function hasWidgetFeature( widget, feature ) {
return widget.features && CKEDITOR.tools.array.indexOf( widget.features, feature ) !== -1;
}
function getWidgetsWithFeature( widgets, feature ) {
return CKEDITOR.tools.array.reduce( CKEDITOR.tools.object.keys( widgets ), function( featuredWidgets, id ) {
var widget = widgets[ id ];
if ( hasWidgetFeature( widget, feature ) ) {
featuredWidgets.push( widget );
}
return featuredWidgets;
}, [] );
}
function getLinkFeature() {
function isLinkable( widget ) {
return widget && hasWidgetFeature( widget, 'link' );
}
function addLinkAttributes( editor, linkElement, linkData ) {
// Set and remove all attributes associated with this state.
var attributes = CKEDITOR.plugins.link.getLinkAttributes( editor, linkData );
if ( !CKEDITOR.tools.isEmpty( attributes.set ) ) {
linkElement.setAttributes( attributes.set );
}
if ( attributes.removed.length ) {
linkElement.removeAttributes( attributes.removed );
}
}
function createLink( editor, img, linkData ) {
var link = img.getAscendant( 'a' ) || editor.document.createElement( 'a' );
addLinkAttributes( editor, link, linkData );
if ( !link.contains( img ) ) {
link.replace( img );
img.move( link );
}
return link;
}
function getLinkData( widget ) {
return CKEDITOR.plugins.link.parseLinkAttributes( widget.editor, widget.parts.link );
}
function appendMenuItems( editor ) {
editor.addMenuGroup( 'imagebase', 10 );
editor.addMenuItem( 'imagebase', {
label: editor.lang.link.menu,
command: 'link',
group: 'imagebase'
} );
}
function createOkListener( evt, dialog, widget ) {
return function() {
if ( !isLinkable( widget ) ) {
return;
}
evt.stop();
var data = {};
dialog.commitContent( data );
widget.setData( 'link', data );
};
}
function addUnlinkListener( editor, evtType, callback ) {
editor.getCommand( 'unlink' ).on( evtType, function( evt ) {
var widget = getFocusedWidget( editor );
// Override unlink only when link truly belongs to the widget.
// If wrapped inline widget in a link, let default unlink work (http://dev.ckeditor.com/ticket/11814).
if ( !isLinkable( widget ) ) {
return;
}
evt.stop();
if ( callback && typeof callback === 'function' ) {
callback( this, widget, editor );
}
evt.cancel();
} );
}
return {
allowedContent: {
a: {
attributes: '!href'
}
},
parts: {
link: 'a'
},
init: function() {
if ( this.editor.plugins.link && this.editor.contextMenu ) {
this.on( 'contextMenu', function( evt ) {
if ( this.parts.link ) {
evt.data.link = evt.data.unlink = CKEDITOR.TRISTATE_OFF;
}
} );
}
},
setUp: function( editor ) {
if ( !editor.plugins.link ) {
// All of listeners registered later on make only sense when link plugin is loaded.
return;
}
if ( editor.contextMenu ) {
appendMenuItems( editor );
}
editor.on( 'dialogShow', function( evt ) {
var widget = getFocusedWidget( editor ),
dialog = evt.data,
displayTextField,
okListener;
if ( !isLinkable( widget ) || dialog._.name !== 'link' ) {
return;
}
displayTextField = dialog.getContentElement( 'info', 'linkDisplayText' ).getElement().getParent().getParent();
dialog.setupContent( widget.data.link || {} );
displayTextField.hide();
// This listener overwrites the default action after pressing "OK" button in link dialog.
// It gets the user input and set appropriate data in the widget.
// `evt.stop` and higher priority are necessary to prevent adding unwanted link to
// widget's caption.
okListener = dialog.once( 'ok', createOkListener( evt, dialog, widget ), null, null, 9 );
dialog.once( 'hide', function() {
okListener.removeListener();
displayTextField.show();
} );
} );
// Overwrite default behaviour of unlink command.
addUnlinkListener( editor, 'exec', function( command, widget, editor ) {
widget.setData( 'link', null );
command.refresh( editor, editor.elementPath() );
} );
addUnlinkListener( editor, 'refresh', function( command, widget ) {
command.setState( widget.parts.link ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED );
} );
},
data: function( evt ) {
var editor = this.editor,
link = evt.data.link,
img = this.element.findOne( 'img' );
// Widget is inited with link, so let's set appropriate data.
if ( typeof link === 'undefined' && this.parts.link ) {
this.setData( 'link', getLinkData( this ) );
}
if ( typeof link === 'undefined' ) {
return;
}
// Unlink was invoked.
if ( link === null ) {
this.parts.link.remove( true );
this.parts.link = null;
// Reset link state (#tp3298).
delete evt.data.link;
} else {
this.parts.link = createLink( editor, img, link );
}
}
};
}
function getUploadFeature() {
/**
* Widget feature dedicated to handling seamless file uploads.
*
* This type serves solely as a mixin, and should be added using
* the {@link CKEDITOR.plugins.imagebase#addFeature} method.
*
* This API is not yet in a final shape, thus it is marked as private. It can change at any point in the future.
*
* @private
* @class CKEDITOR.plugins.imagebase.featuresDefinitions.upload
* @abstract
*/
var ret = {
/**
* The type used for progress reporting. It has to be a subclass of {@link CKEDITOR.plugins.imagebase.progressReporter}.
*
* It can be set to `false` so that there is no progress reporter created at all.
*
* @property {Function/Boolean} [progressReporterType=CKEDITOR.plugins.imagebase.progressBar]
*/
progressReporterType: ProgressBar,
setUp: function( editor, definition ) {
editor.on( 'paste', function( evt ) {
var dataTransfer = evt.data.dataTransfer,
isFileTransfer = dataTransfer && dataTransfer.isFileTransfer(),
filesCount = dataTransfer && dataTransfer.getFilesCount();
if ( editor.readOnly || !isFileTransfer ) {
return;
}
var matchedFiles = [],
curFile;
// Refetch the definition... original definition looks like an outdated copy and it doesn't
// include members inherited from imagebase.
definition = editor.widgets.registered[ definition.name ];
for ( var i = 0; i < filesCount; i++ ) {
curFile = dataTransfer.getFile( i );
if ( CKEDITOR.fileTools.isTypeSupported( curFile, definition.supportedTypes ) ) {
matchedFiles.push( curFile );
}
}
if ( matchedFiles.length === 0 ) {
return;
}
evt.cancel();
// At the time being we expect no other actions to happen after the widget was inserted.
evt.stop();
CKEDITOR.tools.array.forEach( matchedFiles, function( curFile, index ) {
var loader = ret._spawnLoader( editor, curFile, definition, curFile.name );
ret._insertWidget( editor, definition, URL.createObjectURL( curFile ), true, { uploadId: loader.id } );
// Now modify the selection so that the next widget won't replace the current one.
// This selection workaround is required to store multiple files.
if ( index === matchedFiles.length - 1 ) {
// We don't want to modify selection for the last element, so that the last widget remains selected.
return;
}
var sel = editor.getSelection(),
ranges = sel.getRanges();
ranges[ 0 ].enlarge( CKEDITOR.ENLARGE_ELEMENT );
ranges[ 0 ].collapse( false );
} );
} );
},
init: function() {
this.once( 'ready', function() {
var uploadId = this.data.uploadId;
if ( typeof uploadId !== 'undefined' ) {
var loader = this.editor.uploadRepository.loaders[ uploadId ];
if ( loader ) {
// There is a possibility that loader will not be found, e.g. pasting into a completely different editor.
this._beginUpload( this, loader );
}
}
} );
},
/**
* Informs whether the loader is complete.
*
* @private
* @param {CKEDITOR.fileTools.fileLoader} loader
* @returns {Boolean}
*/
_isLoaderDone: function( loader ) {
// This method should be removed once #1497 is done.
var xhr = loader.xhr;
return xhr && loader.xhr.readyState === 4;
},
/**
*
* @private
* @param {CKEDITOR.editor} editor
* @param {Blob/String} file See {@link CKEDITOR.fileTools.fileLoader}.
* @param {CKEDITOR.plugins.widget.definition} widgetDef The widget definition that the loader is spawned for.
* @param {String} [fileName] Preferred file name to be passed to the upload process.
* @returns {CKEDITOR.fileTools.fileLoader}
*/
_spawnLoader: function( editor, file, widgetDef, fileName ) {
var loadMethod = widgetDef.loadMethod || 'loadAndUpload',
loader = editor.uploadRepository.create( file, fileName, widgetDef.loaderType );
loader[ loadMethod ]( widgetDef.uploadUrl, widgetDef.additionalRequestParameters );
return loader;
},
/**
* Initializes the upload process for a given `widget` using `loader`.
*
* @private
* @param {CKEDITOR.plugins.widget} widget
* @param {CKEDITOR.fileTools.fileLoader} loader
*/
_beginUpload: function( widget, loader ) {
function widgetCleanup() {
// Remove upload id so that it's not being re-requested when e.g. someone copies and pastes
// the widget in other place.
if ( widget.isInited() ) {
widget.setData( 'uploadId', undefined );
}
widget.wrapper.removeClass( 'cke_widget_wrapper_uploading' );
}
function failHandling() {
widgetCleanup();
if ( widget.fire( 'uploadFailed', {
loader: loader
} ) !== false ) {
widget.editor.widgets.del( widget );
}
}
function uploadComplete() {
widgetCleanup();
widget.fire( 'uploadDone', {
loader: loader
} );
}
var loaderEventMapping = {
uploaded: uploadComplete,
abort: failHandling,
error: failHandling
},
listeners = [];
listeners.push( loader.on( 'abort', loaderEventMapping.abort ) );
listeners.push( loader.on( 'error', loaderEventMapping.error ) );
listeners.push( loader.on( 'uploaded', loaderEventMapping.uploaded ) );
this.on( 'destroy', function() {
CKEDITOR.tools.array.filter( listeners, function( curListener ) {
curListener.removeListener();
return false;
} );
} );
widget.setData( 'uploadId', loader.id );
if ( widget.fire( 'uploadStarted', loader ) !== false && widget.progressReporterType ) {
if ( !widget._isLoaderDone( loader ) ) {
// Deliberately add class to wrapper, it does not make sense for widget element.
widget.wrapper.addClass( 'cke_widget_wrapper_uploading' );
// Progress reporter has only sense if widget is in progress.
var progress = new widget.progressReporterType();
widget.wrapper.append( progress.wrapper );
progress.bindLoader( loader );
} else {
if ( loaderEventMapping[ loader.status ] ) {
loaderEventMapping[ loader.status ]();
}
}
}
},
/**
* @private
* @param {CKEDITOR.editor} editor
* @param {CKEDITOR.plugins.widget.definition} widgetDef
* @param {String} blobUrl Blob URL of an image.
* @param {Boolean} [finalize=true] If `false`, the widget will not be automatically finalized (added to {@link CKEDITOR.plugins.widget.repository}),
* but will be returned as a {@link CKEDITOR.dom.element} instance.
* @returns {CKEDITOR.plugins.widget/CKEDITOR.dom.element} The widget instance or {@link CKEDITOR.dom.element} of a widget wrapper if `finalize` was set to `false`.
*/
_insertWidget: function( editor, widgetDef, blobUrl, finalize, data ) {
var tplParams = ( typeof widgetDef.defaults == 'function' ? widgetDef.defaults() : widgetDef.defaults ) || {};
// Make sure to work on a new object, otherwise definition.defaults might get modified with instance-specific value.
tplParams = CKEDITOR.tools.extend( {}, tplParams );
tplParams.src = blobUrl;
var element = CKEDITOR.dom.element.createFromHtml( widgetDef.template.output( tplParams ) ),
wrapper = editor.widgets.wrapElement( element, widgetDef.name ),
temp = new CKEDITOR.dom.documentFragment( wrapper.getDocument() );
// Append wrapper to a temporary document. This will unify the environment
// in which #data listeners work when creating and editing widget.
temp.append( wrapper );
if ( finalize !== false ) {
editor.widgets.initOn( element, widgetDef, data );
return editor.widgets.finalizeCreation( temp );
} else {
return element;
}
}
/**
* Preferred file loader type used for requests.
*
* @property {Function} [loaderType=CKEDITOR.fileTools.fileLoader]
*/
/**
* Fired when upload was initiated and before the response is fetched.
*
* progress.once( 'uploadStarted', function( evt ) {
* evt.cancel();
* // Implement a custom progress bar.
* } );
*
* This event is cancelable. If canceled, the default progress bar will not be created
* and the widget wrapper will not be marked with the `cke_widget_wrapper_uploading` class.
*
* Note that the event will be fired even if the widget was created for a loader that
* is already resolved.
*
* @event uploadStarted
* @param {CKEDITOR.fileTools.fileLoader} data The loader that is used for this widget.
*/
/**
* Fired when the upload process succeeded. This is the event where you want apply the data
* from your response into a widget.
*
* progress.once( 'uploadDone', function( evt ) {
* var response = evt.data.loader.responseData.response;
* this.setData( 'backendUrl', response.url );
* } );
*
* @event uploadDone
* @param data
* @param {CKEDITOR.fileTools.fileLoader} data.loader The loader that caused this event.
*/
/**
* Fired when the upload process {@link CKEDITOR.fileTools.fileLoader#event-error failed} or was
* {@link CKEDITOR.fileTools.fileLoader#event-abort aborted}.
*
* progress.once( 'uploadFailed', function( evt ) {
* console.log( 'Loader: ' + evt.data.loader + ' failed to upload data.' );
* } );
*
* This event is cancelable. If it is not canceled, it will remove the widget.
*
* @event uploadFailed
* @param data
* @param {CKEDITOR.fileTools.fileLoader} data.loader The loader that caused this event.
*/
};
return ret;
}
function getCaptionFeature() {
function createCaption( widget ) {
var element = widget.element,
caption = element.getDocument().createElement( 'figcaption' );
element.append( caption );
widget.initEditable( 'caption', widget.definition.editables.caption );
return caption;
}
function isEmptyOrHasPlaceholder( widget ) {
return !widget.editables.caption.getData() || !!widget.parts.caption.data( 'cke-caption-placeholder' );
}
function addPlaceholder( widget ) {
widget.parts.caption.data( 'cke-caption-placeholder', widget.editor.lang.imagebase.captionPlaceholder );
}
function removePlaceholder( widget ) {
widget.parts.caption.data( 'cke-caption-placeholder', false );
}
function setVisibility( caption, isVisible ) {
caption.data( 'cke-caption-active', isVisible );
caption.data( 'cke-caption-hidden', !isVisible );
}
/**
* The widget feature dedicated for displaying a caption under the widget.
*
* This type serves solely as a mixin, and should be added using
* the {@link CKEDITOR.plugins.imagebase#addFeature} method.
*
* This API is not yet in the final shape, thus it is marked as private. It can change at any point in the future.
*
* @private
* @class CKEDITOR.plugins.imagebase.featuresDefinitions.caption
* @abstract
*/
return {
setUp: function( editor ) {
var listeners = [];
function listener( evt ) {
var path = evt.name === 'blur' ? editor.elementPath() : evt.data.path,
sender = path ? path.lastElement : null,
widgets = getWidgetsWithFeature( editor.widgets.instances, 'caption' );
if ( !editor.filter.check( 'figcaption' ) ) {
return CKEDITOR.tools.array.forEach( listeners, function( listener ) {
listener.removeListener();
} );
}
CKEDITOR.tools.array.forEach( widgets, function( widget ) {
widget._refreshCaption( sender );
} );
}
listeners.push( editor.on( 'selectionChange', listener , null, null, 9 ) );
listeners.push( editor.on( 'blur', listener ) );
},
init: function() {
if ( !this.editor.filter.check( 'figcaption' ) ) {
return;
}
if ( !this.parts.caption ) {
this.parts.caption = createCaption( this );
}
// Refresh caption only if it's empty and doesn't have a placeholder to prevent hiding caption on paste (#1592).
if ( !this.editables.caption.getData() && !this.parts.caption.data( 'cke-caption-placeholder' ) ) {
this._refreshCaption();
}
},
/**
* Method used to decide if the caption should be displayed for the focused widget and whether it
* should contain the placeholder text.
*
* @private
* @member CKEDITOR.plugins.imagebase.featuresDefinitions.caption
* @param {CKEDITOR.dom.element} sender The element that this function should be called on.
*/
_refreshCaption: function( sender ) {
var isFocused = getFocusedWidget( this.editor ) === this,
caption = this.parts.caption,
editable = this.editables.caption;
if ( isFocused ) {
if ( !editable.getData() && !sender.equals( caption ) ) {
addPlaceholder( this );
} else if ( !sender || ( sender.equals( caption ) && sender.data( 'cke-caption-placeholder' ) ) ) {
removePlaceholder( this );
}
setVisibility( caption, true );
} else if ( isEmptyOrHasPlaceholder( this ) ) {
removePlaceholder( this );
setVisibility( caption, false );
}
}
};
}
var featuresDefinitions = {
caption: getCaptionFeature(),
upload: getUploadFeature(),
link: getLinkFeature()
};
function createWidgetDefinition( editor, definition ) {
var baseDefinition;
/**
* This is an abstract class that describes the definition of a basic image widget
* created by the {@link CKEDITOR.plugins.imagebase#addImageWidget} method.
*
* Note that because the image widget is a type of a widget, this definition extends
* {@link CKEDITOR.plugins.widget.definition}.
* It adds several parts of the image and implements the basic version of
* the {@link CKEDITOR.plugins.widget.definition#upcast} callback.
*
* @abstract
* @since 4.9.0
* @class CKEDITOR.plugins.imagebase.imageWidgetDefinition
* @mixins CKEDITOR.plugins.widget.definition
*/
baseDefinition = {
pathName: editor.lang.imagebase.pathName,
defaults: {
imageClass: ( editor.config.easyimage_class || '' ),
alt: '',
src: '',
caption: ''
},
template: '<figure class="{imageClass}">' +
'<img alt="{alt}" src="{src}" />' +
'<figcaption>{caption}</figcaption>' +
'</figure>',
allowedContent: {
img: {
attributes: '!src,alt,width,height'
},
figure: true,
figcaption: true
},
requiredContent: 'figure; img[!src]',
/**
* An array containing the names of features added to this widget's definition.
*
* @property {String[]} features
*/
features: [],
editables: {
caption: {
selector: 'figcaption',
pathName: editor.lang.imagebase.pathNameCaption,
allowedContent: 'br em strong sub sup u s; a[!href,target]'
}
},
parts: {
image: 'img',
caption: 'figcaption'
},
upcasts: {
figure: function( element ) {
if ( element.find( 'img', true ).length === 1 ) {
return element;
}
}
}
};
definition = CKEDITOR.tools.object.merge( baseDefinition, definition );
/**
* Image widget definition overrides the {@link CKEDITOR.plugins.widget.definition#upcast} property.
* It is automatically set to enumerate the keys of {@link #upcasts}.
* Avoid changes unless you know what you're doing!
*
* @member CKEDITOR.plugins.imagebase.imageWidgetDefinition
* @property {String}
*/
definition.upcast = CKEDITOR.tools.object.keys( definition.upcasts ).join( ',' );
return definition;
}
var UPLOAD_PROGRESS_THROTTLING = 100;
/**
* This is the base type for progress reporters.
*
* A progress reporter can be updated:
*
* * Automatically, by binding it to an existing {@link CKEDITOR.fileTools.fileLoader} instance.
* * Manually, using the {@link #updated}, {@link #done}, {@link #failed} and {@link #aborted} methods.
*
* @class CKEDITOR.plugins.imagebase.progressReporter
* @constructor
* @param {String} [wrapperHtml='<div class="cke_loader"></div>']
*/
function ProgressReporter( wrapperHtml ) {
/**
* @property {CKEDITOR.dom.element} wrapper An element created for wrapping the progress bar.
*/
this.wrapper = CKEDITOR.dom.element.createFromHtml( wrapperHtml || '<div class="cke_loader"></div>' );
}
ProgressReporter.prototype = {
/**
* Method to be called in order to refresh the progress.
*
* @param {Number} progress Progress representation where `1.0` means "complete" and `0` means "no progress".
*/
updated: function() {},
/**
* Marks the progress reporter as complete.
*/
done: function() {
this.remove();
},
/**
* Marks the progress reporter as aborted.
*/
aborted: function() {
this.remove();
},
/**
* Marks the progress reporter as failed.
*/
failed: function() {
this.remove();
},
/**
* Removes the progress reporter from the DOM.
*/
remove: function() {
this.wrapper.remove();
},
/**
* Binds this progress reporter to a given `loader`.
*
* It will automatically remove its listeners when the `loader` has triggered one of the following events:
*
* * {@link CKEDITOR.fileTools.fileLoader#event-abort}
* * {@link CKEDITOR.fileTools.fileLoader#event-error}
* * {@link CKEDITOR.fileTools.fileLoader#event-uploaded}
*
* @param {CKEDITOR.fileTools.fileLoader} loader The loader that should be observed.
*/
bindLoader: function( loader ) {
var progressListeners = [];
function removeProgressListeners() {
if ( progressListeners ) {
CKEDITOR.tools.array.forEach( progressListeners, function( listener ) {
listener.removeListener();
} );
progressListeners = null;
}
}
var updateListener = CKEDITOR.tools.eventsBuffer( UPLOAD_PROGRESS_THROTTLING, function() {
if ( loader.uploadTotal ) {
this.updated( loader.uploaded / loader.uploadTotal );
}
}, this );
progressListeners.push( loader.on( 'update', updateListener.input, this ) );
progressListeners.push( loader.once( 'abort', this.aborted, this ) );
progressListeners.push( loader.once( 'uploaded', this.done, this ) );
progressListeners.push( loader.once( 'error', this.failed, this ) );
// Some events should cause all listeners to be removed.
progressListeners.push( loader.once( 'abort', removeProgressListeners ) );
progressListeners.push( loader.once( 'uploaded', removeProgressListeners ) );
progressListeners.push( loader.once( 'error', removeProgressListeners ) );
}
};
/**
* The type that adds a vertical progress bar.
*
* var progress = new CKEDITOR.plugins.imagebase.progressBar();
* myWrapper.append( progress.wrapper, true );
* progress.bindLoader( myFileLoader );
*
* @class CKEDITOR.plugins.imagebase.progressBar
* @extends CKEDITOR.plugins.imagebase.progressReporter
* @constructor
*/
function ProgressBar() {
ProgressReporter.call( this, '<div class="cke_loader">' +
'<div class="cke_bar" styles="transition: width ' + UPLOAD_PROGRESS_THROTTLING / 1000 + 's"></div>' +
'</div>' );
/**
* @property {CKEDITOR.dom.element} bar The bar element whose width represents the progress.
*/
this.bar = this.wrapper.getFirst();
}
ProgressBar.prototype = new ProgressReporter();
ProgressBar.prototype.updated = function( progress ) {
var percentage = Math.round( progress * 100 );
percentage = Math.max( percentage, 0 );
percentage = Math.min( percentage, 100 );
this.bar.setStyle( 'width', percentage + '%' );
};
CKEDITOR.plugins.add( 'imagebase', {
requires: 'widget,filetools',
lang: 'az,bg,cs,da,de,de-ch,el,en,en-au,et,fa,fr,gl,hr,hu,it,ku,lt,lv,nb,nl,pl,pt,pt-br,ro,ru,sk,sq,sr,sr-latn,sv,tr,ug,uk,zh,zh-cn',
init: function( editor ) {
loadStyles( editor, this );
}
} );
/**
* Namespace providing a set of helper functions for working with image widgets.
*
* @since 4.9.0
* @singleton
* @class CKEDITOR.plugins.imagebase
*/
CKEDITOR.plugins.imagebase = {
/**
* An object that contains all available feature definitions.
* @property {Object}
*/
featuresDefinitions: featuresDefinitions,
/**
* Registers a new widget based on the passed definition.
*
* @param {CKEDITOR.editor} editor The editor that will get the widget registered.
* @param {String} name The widget name.
* @param {CKEDITOR.plugins.imagebase.imageWidgetDefinition} definition The widget's definition.
*/
addImageWidget: function( editor, name, definition ) {
var widget = editor.widgets.add( name, createWidgetDefinition( editor, definition ) );
editor.addFeature( widget );
},
/**
* Adds a new feature to the passed widget's definition by invoking the initial setup once for the editor
* and extending the widget's definition to include all fields needed by this feature.
*
* var widgetDefinition = {};
* widgetDefinition = CKEDITOR.plugins.imagebase.addFeature( editor, 'link', widgetDefinition );
* CKEDITOR.plugins.imagebase.addImageWidget( editor, 'myWidget', widgetDefinition );
*
* @param {CKEDITOR.editor} editor The editor that will get the widget registered.
* @param {String} name The feature name.
* @param {CKEDITOR.plugins.imagebase.imageWidgetDefinition} definition The widget's definition.
* @returns {CKEDITOR.plugins.imagebase.imageWidgetDefinition} The widget's definition extended
* with fields needed by the feature.
*/
addFeature: function( editor, name, definition ) {
var featureDefinition = CKEDITOR.tools.clone( this.featuresDefinitions[ name ] ),
ret;
function mergeMethods( oldOne, newOne ) {
if ( !oldOne && !newOne ) {
return;
}
return function() {
oldOne && oldOne.apply( this, arguments );
newOne && newOne.apply( this, arguments );
};
}
featureDefinition.init = mergeMethods( definition.init, featureDefinition.init );
featureDefinition.data = mergeMethods( definition.data, featureDefinition.data );
if ( featureDefinition.setUp ) {
featureDefinition.setUp( editor, definition );
delete featureDefinition.setUp;
}
ret = CKEDITOR.tools.object.merge( definition, featureDefinition );
if ( !CKEDITOR.tools.isArray( ret.features ) ) {
ret.features = [];
}
ret.features.push( name );
return ret;
},
progressBar: ProgressBar,
progressReporter: ProgressReporter
};
}() );