comparison sites/all/modules/wysiwyg/editors/js/nicedit.js @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 (function($) {
2
3 /**
4 * Attach this editor to a target element.
5 */
6 Drupal.wysiwyg.editor.attach.nicedit = function(context, params, settings) {
7 // Intercept and ignore submit handlers or they will revert changes made
8 // since the instance was removed. The handlers are anonymous and hidden out
9 // of scope in a closure so we can't unbind them. The same operations are
10 // performed when the instance is detached anyway.
11 var oldAddEvent = bkLib.addEvent;
12 bkLib.addEvent = function(obj, type, fn) {
13 if (type != 'submit') {
14 oldAddEvent(obj, type, fn);
15 }
16 }
17 // Attach editor.
18 var editor = new nicEditor(settings);
19 editor.panelInstance(params.field);
20 // The old addEvent() must be restored after creating a new instance, as
21 // plugins with dialogs use it to bind submit handlers to their forms.
22 bkLib.addEvent = oldAddEvent;
23 editor.addEvent('focus', function () {
24 Drupal.wysiwyg.activeId = params.field;
25 });
26 };
27
28 /**
29 * Detach a single or all editors.
30 *
31 * See Drupal.wysiwyg.editor.detach.none() for a full description of this hook.
32 */
33 Drupal.wysiwyg.editor.detach.nicedit = function (context, params, trigger) {
34 if (typeof params != 'undefined') {
35 var instance = nicEditors.findEditor(params.field);
36 if (instance) {
37 if (trigger == 'serialize') {
38 instance.saveContent();
39 }
40 else {
41 instance.ne.removeInstance(params.field);
42 instance.ne.removePanel();
43 }
44 }
45 }
46 else {
47 for (var e in nicEditors.editors) {
48 // Save contents of all editors back into textareas.
49 var instances = nicEditors.editors[e].nicInstances;
50 for (var i = 0; i < instances.length; i++) {
51 if (trigger == 'serialize') {
52 instances[i].saveContent();
53 }
54 else {
55 instances[i].remove();
56 }
57 }
58 // Remove all editor instances.
59 if (trigger != 'serialize') {
60 nicEditors.editors[e].nicInstances = [];
61 }
62 }
63 }
64 };
65
66 /**
67 * Instance methods for nicEdit.
68 */
69 Drupal.wysiwyg.editor.instance.nicedit = {
70 insert: function (content) {
71 var instance = nicEditors.findEditor(this.field);
72 var editingArea = instance.getElm();
73 var sel = instance.getSel();
74 // IE.
75 if (document.selection) {
76 editingArea.focus();
77 sel.createRange().pasteHTML(content);
78 }
79 else {
80 // Convert selection to a range.
81 var range;
82 // W3C compatible.
83 if (sel.getRangeAt) {
84 range = sel.getRangeAt(0);
85 }
86 // Safari.
87 else {
88 range = editingArea.ownerDocument.createRange();
89 range.setStart(sel.anchorNode, sel.anchorOffset);
90 range.setEnd(sel.focusNode, userSeletion.focusOffset);
91 }
92 // The code below doesn't work in IE, but it never gets here.
93 var fragment = editingArea.ownerDocument.createDocumentFragment();
94 // Fragments don't support innerHTML.
95 var wrapper = editingArea.ownerDocument.createElement('div');
96 wrapper.innerHTML = content;
97 while (wrapper.firstChild) {
98 fragment.appendChild(wrapper.firstChild);
99 }
100 range.deleteContents();
101 // Only fragment children are inserted.
102 range.insertNode(fragment);
103 }
104 },
105
106 setContent: function (content) {
107 nicEditors.findEditor(this.field).setContent(content);
108 },
109
110 getContent: function () {
111 return nicEditors.findEditor(this.field).getContent();
112 }
113 };
114
115 })(jQuery);