-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
1888 lines (1698 loc) · 52.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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2015 LoopIndex, This file is part of the Track Changes plugin for CKEditor.
The track changes plugin is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program as the file lgpl.txt. If not, see http://www.gnu.org/licenses/lgpl.html.
Written by (David *)Frenkiel - https://github.com/imdfl
*/
(function(CKEDITOR, global) {
"use strict";
/**
* @class LITE
* @singleton
* The LITE namespace
*/
var LITE = {
/**
* @class LITE.Events
*/
Events : {
/**
* @member LITE.Events
* @event INIT
* string value: "lite:init"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
*/
INIT : "lite:init",
/**
* @member LITE.Events
* @event ACCEPT
* string value: "lite:accept"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
* @param {Object} options filtering options
*/
ACCEPT : "lite:accept",
/**
* @member LITE.Events
* @event REJECT
* string value: "lite:reject"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
* @param {Object} options filtering options
*/
REJECT : "lite:reject",
/**
* @member LITE.Events
* @event SHOW_HIDE
* string value: "lite:showHide"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
* @param {Boolean} show indicates the new change tracking show status
*/
SHOW_HIDE : "lite:showHide",
/**
* @member LITE.Events
* @event TRACKING
* string value: "lite:tracking"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
* @param {Boolean} tracking indicates the new tracking status
*/
TRACKING : "lite:tracking",
/**
* @member LITE.Events
* @event CHANGE
* string value: "lite:change"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
*/
CHANGE : "lite:change",
/**
* @member LITE.Events
* @event HOVER_IN
* string value: "lite:hover-in"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
* @param {Object} node The DOM node hovered
* @param {String} changeId The relevant change id
*/
HOVER_IN: "lite:hover-in",
/**
* @member LITE.Events
* @event HOVER_OUT
* string value: "lite:hover-out"
* @param {LITE.LITEPlugin} lite an instance of a lite object associated with a ckeditor instance
* @param {Object} node The DOM node hovered
* @param {String} changeId The relevant change id
*/
HOVER_OUT: "lite:hover-out"
},
Commands : {
TOGGLE_TRACKING : "lite-toggletracking",
TOGGLE_SHOW : "lite-toggleshow",
ACCEPT_ALL : "lite-acceptall",
REJECT_ALL : "lite-rejectall",
ACCEPT_ONE : "lite-acceptone",
REJECT_ONE : "lite-rejectone",
TOGGLE_TOOLTIPS: "lite-toggletooltips"
}
},
tooltipDefaults = {
show: true,
path: "js/opentip-adapter.js",
classPath: "OpentipAdapter",
cssPath: "css/opentip.css",
delay: 500
},
defaultTooltipTemplate = null,
LITEConstants = {
deleteTag: 'del',
insertTag: 'ins',
deleteClass: 'ice-del',
insertClass: 'ice-ins',
attributes: {
changeId: "data-cid",
userId: "data-userid",
userName: "data-username",
sessionId: "data-session-id",
changeData: "data-changedata",
time: "data-time",
lastTime: "data-last-change-time"
},
stylePrefix: 'ice-cts',
preserveOnPaste: 'p',
css: 'css/lite.css'
},
defaultTooltipTemplate = "%a by %u %t",
ice = null,
_emptyRegex = /^[\s\r\n]*$/, // for getting the clean text
_cleanRE = [
{regex: /[\s]*title=\"[^\"]+\"/g, replace: "" },
{regex: /[\s]*data-selected=\"[^\"]+\"/g, replace:""}
],
_pluginMap = [],
cutKeystrokes = [CKEDITOR.CTRL + 88, // CTRL+X
CKEDITOR.CTRL + 120,
CKEDITOR.SHIFT + 46],
isOldCKEDITOR = false;
function cleanNode(node) {
var ret, name, parent,
i, len, child;
if (node.nodeType === ice.dom.ELEMENT_NODE) {
var children = node.childNodes;
for (i = 0; i < children.length; ++i) {
child = children[i];
cleanNode(child);
name = child.nodeName.toLowerCase();
if (name === LITEConstants.insertTag || name === LITEConstants.deleteTag) {
while (child.firstChild) {
node.insertBefore(child.firstChild, child);
}
node.removeChild(child);
}
}
}
name = node.nodeName.toLowerCase();
if (name === 'ins' || name === 'del') {
ret = jQuery.makeArray(node.childNodes);
}
else {
ret = [node];
}
return ret;
}
function cleanClipboard(nodes) {
if (! nodes ||! nodes.length) {
return [];
}
var ret = [];
nodes.forEach(function(node) {
ret = ret.concat(cleanNode(node));
});
return ret;
}
function isCutKeystroke(code) {
return cutKeystrokes.indexOf(code) >= 0;
}
function nodeToDOMNode(node) {
if (node && node.$ && (typeof node.getDocument === "function")) {
return node.$;
}
return node;
}
function _findPluginIndex(editor) {
for (var i = _pluginMap.length; i--;) {
var rec = _pluginMap[i];
if (rec.editor === editor) {
return i;
}
}
return -1;
}
function _findPluginRec (editor) {
var ind = _findPluginIndex(editor);
return ind >= 0 ? _pluginMap[ind] : null;
}
function _findPlugin(editor) {
var rec = _findPluginRec(editor);
return rec && rec.plugin;
}
function addPlugin(editor, plugin) {
_pluginMap.push({
plugin: plugin,
editor : editor
});
}
function padString(s, length, padWith, bSuffix) {
if (null === s || (typeof(s) === "undefined")) {
s = "";
}
else {
s = String(s);
}
padWith = String(padWith);
var padLength = padWith.length;
for (var i = s.length; i < length; i += padLength) {
if (bSuffix) {
s += padWith;
}
else {
s = padWith + s;
}
}
return s;
}
function padNumber(s, length) {
return padString(s, length, '0');
}
function relativeDateFormat(date, lang) {
var now = new Date(),
today = now.getDate(),
month = now.getMonth(),
year = now.getFullYear(),
minutes, hours;
var t = typeof(date);
if (t === "string" || t === "number") {
date = new Date(date);
}
var months = lang.MONTHS;
if (today == date.getDate() && month == date.getMonth() && year == date.getFullYear()) {
minutes = Math.floor((now.getTime() - date.getTime()) / 60000);
if (minutes < 1) {
return lang.NOW;
}
else if (minutes < 2) {
return lang.MINUTE_AGO;
}
else if (minutes < 60) {
return (lang.MINUTES_AGO.replace("xMinutes", minutes));
}
else {
hours = date.getHours();
minutes = date.getMinutes();
return lang.AT + " " + padNumber(hours, 2) + ":" + padNumber(minutes, 2, "0");
}
}
else if (year == date.getFullYear()) {
return lang.ON + " " + lang.LITE_LABELS_DATE(date.getDate(), date.getMonth());
}
else {
return lang.ON + " " + lang.LITE_LABELS_DATE(date.getDate(), date.getMonth(), date.getFullYear());
}
}
var initModule = function() {
var ckv = parseFloat(CKEDITOR.version);
isOldCKEDITOR = isNaN(ckv) || ckv < 4.4;
initModule = function(){};
};
/**
* returns true if the element matches one of the patterns
*/
function elementMatchesSelectors($el, patterns) {
var i, len = patterns && patterns.length;
if (! $el || ! len) {
return false;
}
for (i = 0; i < len; ++i) {
if ($el.is(patterns[i])) {
return true;
}
}
return false;
}
/**
* @class LITE.AcceptRejectOptions
* A map of options for filtering changes before they're accepted/rejected.
*/
/**
* @member LITE.AcceptRejectOptions
* @property {Array} include
* An array of user ids to include. Only changes made by users in the include list will be accepted/rejected
*/
/**
* @member LITE.AcceptRejectOptions
* @property {Array} exclude
* An array of user ids to exclude. Changes made by users in the exclude list will be not accepted/rejected
*/
/**
* @member LITE.AcceptRejectOptions
* @property {Function} filter
* a filter function of the form function({userid, time, data}):boolean . Only changes for which the function
* returns true are accepted/rejected
*/
/**
* @class LITE.configuration
* The configuration object for the {@link LITE.lite} and the {@link LITE.LITEPlugin} objects
* This object is usually created in the CKEditor configuration file. It can also be created/modified
* in the callback for CKEditor's <strong>configLoaded</strong> event
* <p>In the config file, create this object with code such as:
* <pre>
* CKEDITOR.editorConfig = function( config ) {
* // ... your own configuration
* var lite = config.lite = (config.lite || {});
* // now assign values to properties: lite.xxx = yyy;
* </pre>
* And here's an example for configuring lite in the <strong>configLoaded</strong> event:
* <pre>
* function onConfigLoaded(e) {
* var conf = e.editor.config;
* var lt = conf.lite = (conf.lite || {});
* lt.isTracking = false;
* }
* </pre>
*/
/**
* @member LITE.configuration
* @property {Boolean} isTracking
* Initial tracking state of the plugin. Default: <code>true</code>
*/
/**
* @member LITE.configuration
* @property {Object} debug
* set debug.log to true for LITE to print error messages in the browser console
*/
/**
* @member LITE.configuration
* @property {Array} includes
* sets the javascript include files to be included in LITE instead of the default. Use only for debugging or extending the plugin
*/
/**
* @member LITE.configuration
* @property {Object} userStyles
* A map of user id=>user style index
* Normally LITE will assign a style number for each user id it encounters in the markup. If you want to maintain consistent
* style per users (e.g. Melixon is always colored green, Thierry in chartreuse), assign a value to this property, e.g.
* <pre>
* lite.userStyles = {
* 15: 1,
* 18:2,
* 21:3
* };
* </pre>
*/
/**
* @member LITE.configuration
* @property {Object} tooltips
* Configures the tooltips shown by LITE
* <div><strong>Omit the classPath member in order to get tooltips in standard html title elements</strong></div>
* These are the default values used by LITE:
* <pre>
* lite.tooltips = {
* show: true, // set to false to prevent tooltips
* path: "js/opentip-adapter.js", // change to point to your own implementation
* classPath: "OpentipAdapter", // the full name of tooltip class construtor
* cssPath: "css/opentip.css", // the stylesheet file of the tooltips
* delay: 500 // the delay in milliseconds between hovering over a change node and the appearance of a tooltip
* };
* </pre>
*
*/
/**
* @member LITE.configuration
* @property {String} jQueryPath="js/jquery.min.js"
* the path (relative to the LITE plugin.js file) to jQuery
*/
/**
* @member LITE.configuration
* @property {String} tooltipTemplate="%a by %u %t"
* A format string used to create the content of tooltips shown over change spans
* <h3>formats</h3>
* (use uppercase to apply the format to the last modification date of the change span rather than the first)
* <ul>
* <li><strong>%a</strong> The action, "added" or "deleted" (not internationalized yet)
* <li><strong>%t</strong> Timestamp of the first edit action in this change span (e.g. "now", "3 minutes ago", "August 15 1972")
* <li><strong>%u</strong> the name of the user who made the change
* <li><strong>%dd</strong> double digit date of change, e.g. 02
* <li><strong>%d</strong> date of change, e.g. 2
* <li><strong>%mm</strong> double digit month of change, e.g. 09
* <li><strong>%m</strong> month of change, e.g. 9
* <li><strong>%yy</strong> double digit year of change, e.g. 11
* <li><strong>%y</strong> full month of change, e.g. 2011
* <li><strong>%nn</strong> double digit minutes of change, e.g. 09
* <li><strong>%n</strong> minutes of change, e.g. 9
* <li><strong>%hh</strong> double digit hour of change, e.g. 05
* <li><strong>%h</strong> hour of change, e.g. 5
* </ul>
*/
/**
* @member LITE.configuration
* @property {Boolean} contextMenu
* If false, don't add LITE commands to CKEditor's context menu
*/
/**
* @member LITE.configuration
* @property {Array} ignoreSelectors
* Array of CSS selector strings. When LITE processes insertion of html (e.g. clipboard paste or some other plugin
* invoking CKEditor's <code>insertHtml()</code>, it will skip nodes that match any of these selectors plus
* those that contain a non-empty value for the attribute <code>data-track-changes-ignore</code>. Note that mixing ignore
* and unignored nodes in the same insertion is not very useful, since the insertion will be handled entirely by
* LITE if at least one node is not ignored.
*/
/**
* @class LITE.lite
* The plugin object created by CKEditor. Since only one plugin is created per web page which may contain multiple instances of CKEditor, this object only handles
* the lifecycle of {@link LITE.LITEPlugin} the real plugin object.
*
*/
CKEDITOR.plugins.add( 'lite',
{
icons: "lite-acceptall,lite-acceptone,lite-rejectall,lite-rejectone,lite-toggleshow,lite-toggletracking",// %REMOVE_LINE_CORE%
hidpi: true,
lang: ["en", "de"],
_scriptsLoaded : null, // not false, which means we're loading
/**
* Called by CKEditor to init the plugin
* Creates an instance of a {@link LITE.LITEPlugin} if one is not already associated with the given editor.
* @param ed an instance of CKEditor
*/
init: function(ed) {
initModule();
var rec = _findPluginRec(ed);
if (rec) { // should not happen
return;
}
var path = this.path,
plugin = new LITEPlugin(path),
liteConfig = CKEDITOR.tools.extend({}, ed.config.lite || {}),
ttConfig = liteConfig.tooltips;
if (undefined === ttConfig) {
ttConfig = true;
}
if (ttConfig === true) {
ttConfig = tooltipDefaults;
}
liteConfig.tooltips = ttConfig;
addPlugin(ed, plugin);
plugin.init(ed, liteConfig);
ed.on("destroy", (function(editor) {
var ind = _findPluginIndex(editor);
if (ind >= 0) {
_pluginMap.splice(ind, 1);
}
}).bind(this));
if (this._scriptsLoaded) {
plugin._onScriptsLoaded();
return;
}
else if (this._scriptsLoaded === false) { // still loading, initial value was null
return;
}
this._scriptsLoaded = false;
var jQueryLoaded = (typeof(jQuery) === "function"),
self = this,
jQueryPath = liteConfig.jQueryPath || "js/jquery.min.js",
scripts = (liteConfig.includeType ? liteConfig["includes_" + liteConfig.includeType] : liteConfig.includes) || ["lite-includes.js"];
scripts = scripts.slice(); // create a copy not referenced by the config
for (var i = 0, len = scripts.length; i < len; ++i) {
scripts[i] = path + scripts[i];
}
if (! jQueryLoaded) {
scripts.splice(0, 0, this.path + jQueryPath);
}
if (ttConfig.path) {
scripts.push(this.path + ttConfig.path);
}
var load1 = function() {
if (scripts.length < 1) {
self._scriptsLoaded = true;
ice = global.ice;
if (! jQueryLoaded) {
jQuery.noConflict();
}
jQuery.each(_pluginMap, (function(i, rec) {
rec.plugin._onScriptsLoaded();
}));
}
else {
var script = scripts.shift();
CKEDITOR.scriptLoader.load(script, function() {load1();}, self);
}
};
load1(scripts);
},
/**
* returns the plugin instance associated with an editor
* @param {Object} editor A CKEditor instance. Each ckeditor instance has its own instance of a LITE plugin
* @returns {LITE.LITEPlugin} A LITE plugin instance
*/
findPlugin : function(editor) {
return _findPlugin(editor);
},
/**
* starts a new session in the plugin instance associated with an editor
* @param {Object} editor a CKEditor instance, in which the associated LITE plugin will start the session
*/
startNewSession: function(editor) {
var plugin = _findPlugin(editor);
if (plugin) {
plugin.startNewSession();
}
else {
_logError("startNewSession: plugin not found");
}
}
});
/**
* @class LITE.LITEPlugin
* The LITEPlugin is created per instance of a CKEditor. This object handles all the events and commands associated with change tracking in a specific editor.
*/
var LITEPlugin = function(path) {
this.path = path;
};
LITEPlugin.prototype = {
/**
* Called by CKEditor to init the plugin
* @param ed an instance of CKEditor
* @param {LITE.configuration} config a LITE configuration object, not null, ready to be used as a local copy
*/
init: function(ed, config) {
var lang = ed.lang.lite;
this._editor = ed;
this._domLoaded = false;
this._editor = null;
this._tracker = null;
this._isVisible = true; // changes are visible
this._liteCommandNames = [];
this._canAcceptReject = true; // enable state for accept reject overriding editor readonly
this._removeBindings = [];
if (! defaultTooltipTemplate) {
defaultTooltipTemplate = "%a " + lang.lite.BY + " %u %t";
}
ed.ui.addToolbarGroup('lite');
this._setPluginFeatures(ed, LITEConstants);
this._changeTimeout = null;
this._notifyChange = this._notifyChange.bind(this);
this._notifyTextChange = this._notifyTextChange.bind(this);
this._config = config;
var allow = config.acceptRejectInReadOnly === true;
var commandsMap = [
{
command : LITE.Commands.TOGGLE_TRACKING,
exec : this._onToggleTracking,
title: lang.TOGGLE_TRACKING,
// icon: "track_changes_on_off.png",
trackingOnly : false
},
{
command: LITE.Commands.TOGGLE_SHOW,
exec: this._onToggleShow,
title: lang.TOGGLE_SHOW,
// icon: "show_hide.png",
readOnly : true
},
{
command:LITE.Commands.ACCEPT_ALL,
exec:this._onAcceptAll,
title: lang.ACCEPT_ALL,
// icon:"accept_all.png",
readOnly : allow
},
{
command:LITE.Commands.REJECT_ALL,
exec: this._onRejectAll,
title: lang.REJECT_ALL,
// icon:"reject_all.png",
readOnly : allow
},
{
command:LITE.Commands.ACCEPT_ONE,
exec:this._onAcceptOne,
title: lang.ACCEPT_ONE,
// icon:"accept_one.png",
readOnly : allow
},
{
command:LITE.Commands.REJECT_ONE,
exec:this._onRejectOne,
title: lang.REJECT_ONE,
// icon:"reject_one.png",
readOnly : allow
},
{
command:LITE.Commands.TOGGLE_TOOLTIPS,
exec:this._onToggleTooltips,
readOnly : true
}
];
this._isTracking = config.isTracking !== false; // user preference for tracking state
this._trackingState = null; // reflects the real tracking state, not just the user pref
this._eventsBounds = false;
ed.on("contentDom", (function(dom) {
this._onDomLoaded(dom);
}).bind(this));
ed.on("dataReady", (function(evt) {
this._onAfterSetData(evt);
}).bind(this));
var path = this.path;
var commands = config.commands || [LITE.Commands.TOGGLE_TRACKING, LITE.Commands.TOGGLE_SHOW, LITE.Commands.ACCEPT_ALL, LITE.Commands.REJECT_ALL, LITE.Commands.ACCEPT_ONE, LITE.Commands.REJECT_ONE];
var self = this;
function add1(rec) {
ed.addCommand(rec.command, {
exec : rec.exec.bind(self),
readOnly: rec.readOnly || false
});
if (rec.title && commands.indexOf(rec.command) >= 0) { // configuration doens't include this command
var name = self._commandNameToUIName(rec.command);
ed.ui.addButton(name, {
label : rec.title,
command : rec.command,
// icon : path + "icons/" + rec.icon,
toolbar: "lite"
});
if (rec.trackingOnly !== false) {
self._liteCommandNames.push(rec.command);
}
}
}
for (var i = 0, len = commandsMap.length; i < len; ++i) {
add1(commandsMap[i]);
}
if (config.contextMenu !== false) {
if ( ed.addMenuItems ) {
ed.addMenuGroup ( 'lite', 50);
var params = {};
if (commands.indexOf(LITE.Commands.ACCEPT_ONE) >= 0) {
params[LITE.Commands.ACCEPT_ONE] = {
label : lang.ACCEPT_ONE,
command : LITE.Commands.ACCEPT_ONE,
group : 'lite',
order : 1
};
}
if (commands.indexOf(LITE.Commands.REJECT_ONE) >= 0) {
params[LITE.Commands.REJECT_ONE] = {
label : lang.REJECT_ONE,
command : LITE.Commands.REJECT_ONE,
group : 'lite',
order : 2
};
}
ed.addMenuItems(params);
}
if ( ed.contextMenu ) {
ed.contextMenu.addListener( (function( element /*, selection */ ) {
if (element && this._tracker && this._tracker.currentChangeNode(element)) {
var ret = {};
ret[LITE.Commands.ACCEPT_ONE] = CKEDITOR.TRISTATE_OFF;
ret[LITE.Commands.REJECT_ONE]= CKEDITOR.TRISTATE_OFF;
return ret;
}
else {
return null;
}
}).bind(this) );
}
}
},
/**
* Change the state of change tracking for the change editor associated with this plugin.
* Toggles tracking visibility in accordance with the tracking state.
* @param {Boolean} track if undefined - toggle the state, otherwise set the tracking state to this value,
* @param {Object} options an optional object with the following fields: <ul><li>notify: boolean, if not false, dispatch the TRACKING event</li>
* <li>force: if true, don't check for pending changes and just toggle</li></ul>
*/
toggleTracking: function(track, options) {
if ("boolean" === typeof options) {
options = {
notify: options
};
}
options = options || {};
var tracking = (undefined === track) ? ! this._isTracking : track,
e = this._editor,
lang = this._editor.lang.lite,
force = options && options.force;
if (! tracking && this._isTracking && ! force) {
var nChanges = this._tracker.countChanges({verify: true});
if (nChanges) {
return window.alert(lang.PENDING_CHANGES);
}
}
this._isTracking = tracking;
this._setCommandsState(this._liteCommandNames, tracking ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED);
this._updateTrackingState();
this.toggleShow(tracking, false);
this._setCommandsState(LITE.Commands.TOGGLE_TRACKING, tracking ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF);
var ui = e.ui.get(this._commandNameToUIName(LITE.Commands.TOGGLE_TRACKING));
if (ui) {
this._setButtonTitle(ui, tracking ? lang.STOP_TRACKING : lang.START_TRACKING);
}
if (options.notify !== false) {
e.fire(LITE.Events.TRACKING, {tracking:tracking, lite:this});
}
},
/**
* Change the visibility of tracked changes for the change editor associated with this plugin
* @param {Boolean} [show=undefined] if show is a boolean value, set the visibility state to this value, otherwise toggle the state
* @param {Boolean} [bNotify=true] if not false, dispatch the TOGGLE_SHOW event
*/
toggleShow : function(show, bNotify) {
var vis = (typeof(show) === "undefined") ? (! this._isVisible) : show,
lang = this._editor.lang.lite;
this._isVisible = vis;
if (this._isTracking) {
this._setCommandsState(LITE.Commands.TOGGLE_SHOW, vis ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF);
}
this._tracker.setShowChanges(vis && this._isTracking);
var ui = this._editor.ui.get(this._commandNameToUIName(LITE.Commands.TOGGLE_SHOW));
if (ui) {
this._setButtonTitle(ui, vis ? lang.HIDE_TRACKED : lang.SHOW_TRACKED);
}
if (bNotify !== false) {
this._editor.fire(LITE.Events.SHOW_HIDE, {show:vis, lite:this});
}
},
/**
* Are tracked changes visible?
* @returns {Boolean} true if tracked changes are visible
*/
isVisible : function() {
return this._isVisible;
},
/**
* Are changes tracked?
* @returns {Boolean} true if changes are tracked
*/
isTracking: function() {
return this._isTracking;
},
/**
* Accept all tracked changes
* @param {LITE.AcceptRejectOptions} options for matching changes to accept
*/
acceptAll: function(options) {
this._tracker.acceptAll(options);
this._cleanup();
this._editor.fire(LITE.Events.ACCEPT, {lite: this, options : options});
},
/**
* Reject all tracked changes
* @param {LITE.AcceptRejectOptions} options for matching changes to accept
*/
rejectAll: function(options) {
this._tracker.rejectAll(options);
this._cleanup();
this._editor.fire(LITE.Events.REJECT, {lite: this, options : options});
},
/**
* Set the name & id of the current user
* @param {Object} info an object with the fields `name`, `id`
*/
setUserInfo: function(info) {
info = info || {};
this._config.userId = String(info.id);
this._config.userName = info.name || "";
if (this._tracker) {
this._tracker.setCurrentUser({ id: this._config.userId, name : this._config.userName });
}
/* if (this._editor) {
var lite = this._editor.config.lite || {};
this._editor.config.lite = lite;
}; */
},
/**
* Returns a copy of the properties of the current user: id, name
* @returns {Object} an object with the properties <strong>id</strong> and <strong>name</strong>
*/
getUserInfo: function() {
return this._tracker ? this._tracker.getCurrentUser() : { name: "", id: ""};
},
/**
* Return the count of pending changes
* @param {LITE.AcceptRejectOptions} [options=null] optional filtering for the changes we want to count.
*/
countChanges : function(options) {
return (this._tracker && this._tracker.countChanges(options)) || 0;
},
/**
* Enable or disable the accept changes ui. This does not affect the availabibility of the accept/reject api
* @param {Boolean} bEnable
*/
enableAcceptReject : function(bEnable) {
this._canAcceptReject = Boolean(bEnable);
this._onIceChange();
},
/**
* For the CKEditor content filtering system, not operational yet
*/
filterIceElement : function( e ) {
if (! e) {
return true;
}
try {
if (e.hasClass(LITEConstants.insertClass) || e.hasClass(LITEConstants.deleteClass)) {
return false;
}
}
catch (e) {
}
return true;
},
/**
* Create a new session. The change tracker unifies adjacent changes from the same user id, unless they are
* from different sessions
*/
startNewSession: function() {
var now = new Date();
this._sessionId = String.fromCharCode(65 + Math.round(Math.random() * 26)) + now.getDate() + now.getDay() + now.getHours() + now.getMinutes() + now.getMilliseconds();
if (this._tracker) {
this._tracker.setSessionId(this._sessionId);
}
},
/**
* returns the provided html, or the html content of the editor, without change tracking markup and without deleted changes
* @param {String} text optional html to clean up
* @returns {String}
*/
getCleanMarkup: function(text) {
if (null === text || undefined === text) {
text = (this._editor && this._editor.getData()) || "";
}
for (var i = _cleanRE.length - 1; i >= 0; --i) {
text = text.replace(_cleanRE[i].regex, _cleanRE[i].replace);
}
return text;
},
/**
* Returns the text content of the editor, without deleted changes
* @returns
*/
getCleanText : function() {
var doc = this._getDocument();
if (! doc) {
return "";
}
var data = this._editor.getData(),
root = doc.createElement("DIV");
root.innerHTML = data;
var textFragments = [];
textFragments.push("");
var deleteClass = this._tracker.getDeleteClass();
this._getCleanText(root, textFragments, deleteClass);
var str = textFragments.join("\n");
str = str.replace(/ (;)?/ig, ' ');
return str;
},
/**
* Accept the change associated with a DOM node
* @param node either a DOM node or a CKEditor DOM node
*/
acceptChange: function(node) {
node = nodeToDOMNode(node);
if (node && this._tracker) {
this._tracker.acceptChange(node);
this._cleanup();
this._editor.fire(LITE.Events.ACCEPT, {lite:this});
this._onSelectionChanged(null);
}
},
/**
* Reject the change associated with a DOM node
* @param node either a DOM node or a CKEditor DOM node
*/
rejectChange: function(node) {
node = nodeToDOMNode(node);
if (node && this._tracker) {
this._tracker.rejectChange(node);
this._cleanup();
this._editor.fire(LITE.Events.REJECT, {lite:this});
this._onSelectionChanged(null);
}
},
/**
* get a map of the pending changes. The keys are the change ids,
* the values are objects with the type, time, lastTime, session id, user id, user name, data (arbitrary string associated with the change)
* @param {LITE.AcceptRejectOptions} [options=null] filtering options for the returned changes
*/
getChanges: function(options) {
return (this._tracker && this._tracker.getChanges(options)) || {};
},
////////////// Implementation ///////////////
_getCleanText : function(e, textFragments, deleteClass) { // assumed never to be called with a text node
var cls = e.getAttribute("class");
if (cls && cls.indexOf(deleteClass) >= 0) {
return;
}