/
core.js
executable file
·744 lines (679 loc) · 19.7 KB
/
core.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
/*core.js is part of Aloha Editor project http://aloha-editor.org
*
* Aloha Editor is a WYSIWYG HTML5 inline editing library and editor.
* Copyright (c) 2010-2012 Gentics Software GmbH, Vienna, Austria.
* Contributors http://aloha-editor.org/contribution.php
*
* Aloha Editor is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* Aloha Editor 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* As an additional permission to the GNU GPL version 2, you may distribute
* non-source (e.g., minimized or compacted) forms of the Aloha-Editor
* source code without the copy of the GNU GPL normally required,
* provided you include this license notice and a URL through which
* recipients can access the Corresponding Source.
*/
define([
'jquery',
'PubSub',
'aloha/pluginmanager'
], function (
$,
PubSub,
PluginManager
) {
'use strict';
var Aloha = window.Aloha;
/**
* Checks whether the current user agent is supported.
*
* @return {boolean} True if Aloha supports the current browser.
*/
function isBrowserSupported() {
var browser = Aloha.browser;
var version = browser.version;
return !(
// Chrome 21
(browser.chrome && parseFloat(version) < 21) ||
// Safari 4
(browser.webkit && !browser.chrome && parseFloat(version) < 532.5) ||
// FF 3.5
(browser.mozilla && parseFloat(version) < 1.9) ||
// IE 7
(browser.msie && version < 7) ||
// Right now Opera needs some work
(browser.opera && version < 11)
);
}
/**
* Checks whether the given jQuery event originates from an Aloha dialog
* element.
*
* This is used to facilitate a hackish way of preventing blurring
* editables when interacting with Aloha UI modals.
*
* @param {jQuery<Event>} $event
* @return {boolean} True if $event is initiated from within an Aloha
* dialog element.
*/
function originatesFromDialog($event) {
return $($event.target).closest('.aloha-dialog').length > 0;
}
/**
* Checks whether the given jQuery event originates from a jQuery UI
* element.
*
* Just like originatesFromDialog() this is used to prevent deactivating
* editables when interacting with UI elements in a hackish way.
*
* @param {jQuery<Event>} $event The processed event.
* @returns {boolean} true if $event is initieated from a jQuery UI
* element, and false otherwise.
*/
function originatesFromUiWidget($event) {
return $($event.target).closest('.ui-widget').length > 0;
}
/**
* Registers events on the documents to cause editables to be blurred when
* clicking outside of editables.
*
* Hack: Except when the click originates from a modal dialog.
*/
function registerEvents() {
$('html').mousedown(function ($event) {
if (Aloha.activeEditable && !Aloha.eventHandled
&& !originatesFromDialog($event)
&& !originatesFromUiWidget($event)) {
Aloha.deactivateEditable();
}
}).mouseup(function () {
Aloha.eventHandled = false;
});
}
/**
* Initialize Aloha.
*
* @param {function} event Event to trigger after completing tasks.
* @param {function} next Function to call after completing tasks.
*/
function initAloha(event, next) {
if (!isBrowserSupported()) {
var console = window.console;
if (console) {
var fn = console.error ? 'error' : console.log ? 'log' : null;
if (fn) {
console[fn]('This browser is not supported');
}
}
return;
}
// Because different css is to be applied based on what the user-agent
// supports. For example: outlines do not render in IE7.
if (Aloha.browser.webkit) {
$('html').addClass('aloha-webkit');
} else if (Aloha.browser.opera) {
$('html').addClass('aloha-opera');
} else if (Aloha.browser.msie) {
$('html').addClass('aloha-ie' + parseInt(Aloha.browser.version, 10));
} else if (Aloha.browser.mozilla) {
$('html').addClass('aloha-mozilla');
}
if (navigator.appVersion.indexOf('Win') !== -1) {
Aloha.OSName = 'Win';
} else if (navigator.appVersion.indexOf('Mac') !== -1) {
Aloha.OSName = 'Mac';
} else if (navigator.appVersion.indexOf('X11') !== -1) {
Aloha.OSName = 'Unix';
} else if (navigator.appVersion.indexOf('Linux') !== -1) {
Aloha.OSName = 'Linux';
}
// Fix inconsistent browser behavior in Internet Explorer:
// per default the Internet Explorer's own feature AutoUrlDetect is turned on.
// When this feature is active the "editor will automatically create a hyperlink
// for any text that is formatted as a URL."
// https://msdn.microsoft.com/en-us/library/aa769893(v=vs.85).aspx
// http://blogs.msdn.com/b/ieinternals/archive/2010/09/15/ie9-beta-minor-change-list.aspx
//
// Since this behavior is different to all other browsers we will
// turn this feature off in Internet Explorer >= 9 (turning it off is supported
// starting from IE9)
if (Aloha.browser.msie && parseFloat(Aloha.browser.version) >= 9.0 && typeof document.execCommand === 'function') {
document.execCommand('AutoUrlDetect', false, false);
}
// Add hacky workaround for IE problem when editing in a frameset:
// Sometimes it is necessary to click into an editable multiple times to activate it (for the first time)
if (Aloha.browser.msie && document.body && document.body.children.length > 0) {
var sel = window.getSelection();
var range = document.createRange();
range.setStart(document.body.children[0], 0);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
sel.removeAllRanges();
}
registerEvents();
Aloha.settings.base = Aloha.getAlohaUrl();
Aloha.Log.init();
// Initialize error handler for general javascript errors.
if (Aloha.settings.errorhandling) {
window.onerror = function (msg, url, line) {
Aloha.Log.error(Aloha, 'Error message: ' + msg + '\nURL: ' +
url + '\nLine Number: ' + line);
return true;
};
}
event();
next();
}
/**
* Initialize managers
*
* @param {function} event Event to trigger after completing tasks.
* @param {function} next Function to call after completing tasks.
*/
function initRepositoryManager(event, next) {
Aloha.RepositoryManager.init();
event();
next();
}
/**
* Initialize Aloha plugins.
*
*
* @param {function} event Event to trigger after completing tasks.
* @param {function} next Callback that will be invoked after all plugins
* have been initialized. Whereas plugins are loaded
* synchronously, plugins may initialize
* asynchronously.
*/
function initPluginManager(event, next) {
// Because if there are no loadedPlugins specified, then the default is
// to initialized all available plugins.
if (0 === Aloha.loadedPlugins.length) {
var plugins = PluginManager.plugins;
var plugin;
for (plugin in plugins) {
if (plugins.hasOwnProperty(plugin)) {
Aloha.loadedPlugins.push(plugin);
}
}
}
var fired = false;
PluginManager.init(function () {
if (!fired) {
event();
fired = true;
}
next();
}, Aloha.loadedPlugins);
if (!fired) {
event();
fired = true;
}
}
/**
* Begin initialize editables.
*
* @param {function} event Event to trigger after completing tasks.
* @param {function} next Function to call after completing tasks.
*/
function initEditables(event, next) {
var i;
for (i = 0; i < Aloha.editables.length; i++) {
if (!Aloha.editables[i].ready) {
Aloha.editables[i].init();
}
}
event();
next();
}
/**
* Initialization phases.
*
* These stages denote the initialization states which Aloha will go
* through from loading to ready.
*
* Each phase object contains the following properties:
* fn : The process that is to be invoked during this phase.
* Will take two functions: event() and next().
* event : The event name, which if provided, will be fired after the
* phase has been started (optional).
* deferred : A $.Deferred() object to hold event handlers until that
* initialization phase has been done (optional).
*
* @type {Array.<phase>}
*/
var phases = [
// Phase 0: Waiting for initialization to begin. This is the state at
// load-time.
{
fn: null,
event: null,
deferred: null
},
// Phase 1: DOM is ready; performing compatibility checks, registering
// basic events, and initializing logging.
{
fn: initAloha,
event: null,
deferred: null
},
// Phase 2: Initial checks have passed; Initializing repository manger.
{
fn: initRepositoryManager,
event: null,
deferred: null
},
// Phase 3: Repository manager is ready for use; commence
// initialization of configured or default plugins.
{
fn: initPluginManager,
event: 'aloha-plugins-loaded',
deferred: null
},
// Phase 4: Plugins have all begun their initialization process, but it
// is not necessary that they have completed. Start
// initializing editable, along with their scaffolding UI.
// Editables will not be fully initialized however, until
// plugins have finished initialization.
{
fn: initEditables,
event: null,
deferred: null
},
// Phase 5: The "ready" state. Notify the system that Aloha is fully
// loaded, and ready for use.
{
fn: null,
event: 'aloha-ready',
deferred: null
}
];
/**
* Base Aloha Object
* @namespace Aloha
* @class Aloha The Aloha base object, which contains all the core functionality
* @singleton
*/
$.extend(true, Aloha, {
/**
* The Aloha Editor Version we are using
* It should be set by us and updated for the particular branch
* @property
*/
version: '${version}',
/**
* Array of editables that are managed by Aloha
* @property
* @type Array
*/
editables: [],
/**
* The currently active editable is referenced here
* @property
* @type Aloha.Editable
*/
activeEditable: null,
/**
* settings object, which will contain all Aloha settings
* @cfg {Object} object Aloha's settings
*/
settings: {},
/**
* defaults object, which will contain all Aloha defaults
* @cfg {Object} object Aloha's settings
*/
defaults: {},
/**
* Namespace for ui components
*/
ui: {},
/**
* This represents the name of the users OS. Could be:
* 'Mac', 'Linux', 'Win', 'Unix', 'Unknown'
* @property
* @type string
*/
OSName: 'Unknown',
/**
* A list of loaded plugin names, available after the STAGES.PLUGINS
* initialization phase.
*
* @type {Array.<string>}
* @internal
*/
loadedPlugins: [],
/**
* Maps names of plugins (link) to the base URL (../plugins/common/link).
*/
_pluginBaseUrlByName: {},
/**
* Start the initialization process.
*/
init: function () {
Aloha.initialize(phases);
},
/**
* Returns list of loaded plugins (without Bundle name)
*
* @return array
*/
getLoadedPlugins: function () {
return this.loadedPlugins;
},
/**
* Returns true if a certain plugin is loaded, false otherwise.
*
* @param {string} plugin Name of plugin
* @return {boolean} True if plugin with given name is load.
*/
isPluginLoaded: function (name) {
var loaded = false;
$.each(this.loadedPlugins, function (i, plugin) {
if (name === plugin.toString()) {
loaded = true;
return false;
}
});
return loaded;
},
/**
* Activates editable and deactivates all other Editables.
*
* @param {Editable} editable the Editable to be activated
*/
activateEditable: function (editable) {
// Because editables may be removed on blur, Aloha.editables.length
// is not cached.
var editables = Aloha.editables;
var i;
for (i = 0; i < editables.length; i++) {
if (editables[i] !== editable && editables[i].isActive) {
editables[i].blur(editable);
}
}
Aloha.activeEditable = editable;
PubSub.pub('aloha.editable.set-active', {
editable: editable
});
},
/**
* Returns the current Editable
* @return {Editable} returns the active Editable
*/
getActiveEditable: function () {
return Aloha.activeEditable;
},
/**
* Deactivates the active Editable.
*
* TODO: Would be better named "deactivateActiveEditable".
*/
deactivateEditable: function () {
if (Aloha.activeEditable) {
Aloha.activeEditable.blur();
Aloha.activeEditable = null;
PubSub.pub('aloha.editable.set-active', {
editable: null
});
}
},
/**
* Gets an editable by an ID or null if no Editable with that ID
* registered.
*
* @param {string} id The element id to look for.
* @return {Aloha.Editable|null} An editable, or null if none if found
* for the given id.
*/
getEditableById: function (id) {
// Because if the element is a textarea, then it's necessary to
// route to the editable div.
var $editable = $('#' + id);
if ($editable.length
&& 'textarea' === $editable[0].nodeName.toLowerCase()) {
id = id + '-aloha';
}
var i;
for (i = 0; i < Aloha.editables.length; i++) {
if (Aloha.editables[i].getId() === id) {
return Aloha.editables[i];
}
}
return null;
},
/**
* Checks whether an object is a registered Aloha Editable.
* @param {jQuery} obj the jQuery object to be checked.
* @return {boolean}
*/
isEditable: function (obj) {
var i, editablesLength;
for (i = 0, editablesLength = Aloha.editables.length; i < editablesLength; i++) {
if (Aloha.editables[i].originalObj.get(0) === obj) {
return true;
}
}
return false;
},
/**
* Gets the nearest editable parent of the DOM element contained in the
* given jQuery object.
*
* @param {jQuery} $element jQuery unit set containing DOM element.
* @return {Aloha.Editable} Editable, or null if none found.
*/
getEditableHost: (function () {
var getEditableOf = function (editable) {
var i;
for (i = 0; i < Aloha.editables.length; i++) {
if (Aloha.editables[i].originalObj[0] === editable) {
return Aloha.editables[i];
}
}
return null;
};
return function ($element) {
if (!$element || 0 === $element.length) {
return null;
}
var editable = getEditableOf($element[0]);
if (!editable) {
$element.parents().each(function (__unused__, node) {
editable = getEditableOf(node);
if (editable) {
return false;
}
});
}
return editable;
};
}()),
/**
* Logs a message to the console.
*
* @param {string} level Level of the log
* ("error", "warn" or "info", "debug").
* @param {object} component Component that calls the log.
* @param {string} message Log message.
* @hide
*/
log: function (level, component, message) {
if (typeof Aloha.Log !== 'undefined') {
Aloha.Log.log(level, component, message);
}
},
/**
* Register the given editable.
*
* @param {Editable} editable to register.
* @hide
*/
registerEditable: function (editable) {
Aloha.editables.push(editable);
},
/**
* Unregister the given editable. It will be deactivated and removed
* from editables.
*
* @param {Editable} editable The editable to unregister.
* @hide
*/
unregisterEditable: function (editable) {
var index = $.inArray(editable, Aloha.editables);
if (index !== -1) {
Aloha.editables.splice(index, 1);
}
},
/**
* Check whether at least one editable was modified.
*
* @return {boolean} True when at least one editable was modified,
* false otherwise.
*/
isModified: function () {
var i;
for (i = 0; i < Aloha.editables.length; i++) {
if (Aloha.editables[i].isModified
&& Aloha.editables[i].isModified()) {
return true;
}
}
return false;
},
/**
* Determines the Aloha Url.
*
* @return {String} Aloha's baseUrl setting or "" if not set.
*/
getAlohaUrl: function (suffix) {
return Aloha.settings.baseUrl || '';
},
/**
* Gets the plugin's url.
*
* @param {string} name The name with which the plugin was registered
* with.
* @return {string} The fully qualified url of this plugin.
*/
getPluginUrl: function (name) {
if (!name) {
return null;
}
var url = Aloha.settings._pluginBaseUrlByName[name];
if (url) {
// Check if url is absolute and attach base url if it is not.
if (!url.match("^(\/|http[s]?:).*")) {
url = Aloha.getAlohaUrl() + '/' + url;
}
}
return url;
},
/**
* Disable object resizing by executing command 'enableObjectResizing',
* if the browser supports this.
*/
disableObjectResizing: function () {
try {
// This will disable browsers image resizing facilities in
// order disable resize handles.
var supported;
try {
supported = document.queryCommandSupported('enableObjectResizing');
} catch (e) {
supported = false;
Aloha.Log.log('enableObjectResizing is not supported.');
}
if (supported) {
document.execCommand('enableObjectResizing', false, false);
Aloha.Log.log('enableObjectResizing disabled.');
}
} catch (e2) {
Aloha.Log.error(e2, 'Could not disable enableObjectResizing');
// this is just for others, who will not support disabling enableObjectResizing
}
},
/**
* Disable native table editing
*/
disableInlineTableEditing: function () { // enableInlineTableEditing
try {
// This will disable browsers native table editing facilities in
// order to disable resize handles.
var supported;
try {
supported = document.queryCommandSupported('enableInlineTableEditing');
} catch (e) {
supported = false;
Aloha.Log.log('enableInlineTableEditing is not supported.');
}
if (supported) {
document.execCommand('enableInlineTableEditing', false, false);
Aloha.Log.log('enableInlineTableEditing disabled.');
}
} catch (e2) {
Aloha.Log.error(e2, 'Could not disable enableInlineTableEditing');
// this is just for others, who will not support disabling enableInlineTableEditing
}
},
/**
* Human-readable string representation of this.
*
* @hide
*/
toString: function () {
return 'Aloha';
},
/**
* Shim to replace $.browser
*
* @hide
*/
browser: (function () {
function uaMatch(ua) {
ua = ua.toLowerCase();
var match = /(edge)[ \/]([\w.]+)/.exec(ua) ||
/(chrome)[ \/]([\w.]+)/.exec(ua) ||
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
/(msie) ([\w.]+)/.exec(ua) ||
// IE11 will only identify itself via its Trident rendering engine
/(trident)(?:.*? rv:([\w.]+))/.exec(ua) ||
(ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua)) || [];
return {
browser: match[1] || "",
version: match[2] || "0"
};
}
var matched = uaMatch(navigator.userAgent);
var browser = {};
if (matched.browser) {
browser[matched.browser] = true;
browser.version = matched.version;
}
// Chrome is Webkit, but Webkit is also Safari.
if (browser.chrome) {
browser.webkit = true;
} else if (browser.webkit) {
browser.safari = true;
}
// Browsers using the Trident rendering engine are also Internet Explorer
if (browser.trident) {
browser.msie = true;
}
return browser;
}())
});
return Aloha;
});