Mercurial > hg > rr-repo
comparison sites/all/modules/wysiwyg/editors/js/whizzywig.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 | |
2 var wysiwygWhizzywig = { currentField: null, fields: {} }; | |
3 var buttonPath = null; | |
4 | |
5 /** | |
6 * Override Whizzywig's document.write() function. | |
7 * | |
8 * Whizzywig uses document.write() by default, which leads to a blank page when | |
9 * invoked in jQuery.ready(). Luckily, Whizzywig developers implemented a | |
10 * shorthand w() substitute function that we can override to redirect the output | |
11 * into the global wysiwygWhizzywig variable. | |
12 * | |
13 * @see o() | |
14 */ | |
15 var w = function (string) { | |
16 if (string) { | |
17 wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] += string; | |
18 } | |
19 return wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField]; | |
20 }; | |
21 | |
22 /** | |
23 * Override Whizzywig's document.getElementById() function. | |
24 * | |
25 * Since we redirect the output of w() into a temporary string upon attaching | |
26 * an editor, we also have to override the o() shorthand substitute function | |
27 * for document.getElementById() to search in the document or our container. | |
28 * This override function also inserts the editor instance when Whizzywig | |
29 * tries to access its IFRAME, so it has access to the full/regular window | |
30 * object. | |
31 * | |
32 * @see w() | |
33 */ | |
34 var o = function (id) { | |
35 // Upon first access to "whizzy" + id, Whizzywig tries to access its IFRAME, | |
36 // so we need to insert the editor into the DOM. | |
37 if (id == 'whizzy' + wysiwygWhizzywig.currentField && wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField]) { | |
38 jQuery('#' + wysiwygWhizzywig.currentField).after('<div id="' + wysiwygWhizzywig.currentField + '-whizzywig"></div>'); | |
39 // Iframe's .contentWindow becomes null in Webkit if inserted via .after(). | |
40 jQuery('#' + wysiwygWhizzywig.currentField + '-whizzywig').html(w()); | |
41 // Prevent subsequent invocations from inserting the editor multiple times. | |
42 wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] = ''; | |
43 } | |
44 // If id exists in the regular window.document, return it. | |
45 if (jQuery('#' + id).size()) { | |
46 return jQuery('#' + id).get(0); | |
47 } | |
48 // Otherwise return id from our container. | |
49 return jQuery('#' + id, w()).get(0); | |
50 }; | |
51 | |
52 (function($) { | |
53 | |
54 /** | |
55 * Attach this editor to a target element. | |
56 */ | |
57 Drupal.wysiwyg.editor.attach.whizzywig = function(context, params, settings) { | |
58 // Assign button images path, if available. | |
59 if (settings.buttonPath) { | |
60 window.buttonPath = settings.buttonPath; | |
61 } | |
62 // Create Whizzywig container. | |
63 wysiwygWhizzywig.currentField = params.field; | |
64 wysiwygWhizzywig.fields[wysiwygWhizzywig.currentField] = ''; | |
65 // Whizzywig needs to have the width set 'inline'. | |
66 $field = $('#' + params.field); | |
67 var originalValues = Drupal.wysiwyg.instances[params.field]; | |
68 originalValues.originalStyle = $field.attr('style'); | |
69 $field.css('width', $field.width() + 'px'); | |
70 | |
71 // Attach editor. | |
72 makeWhizzyWig(params.field, (settings.buttons ? settings.buttons : 'all')); | |
73 // Whizzywig fails to detect and set initial textarea contents. | |
74 $('#whizzy' + params.field).contents().find('body').html(tidyD($field.val())); | |
75 }; | |
76 | |
77 /** | |
78 * Detach a single or all editors. | |
79 */ | |
80 Drupal.wysiwyg.editor.detach.whizzywig = function (context, params, trigger) { | |
81 var detach = function (index) { | |
82 var id = whizzies[index], $field = $('#' + id), instance = Drupal.wysiwyg.instances[id]; | |
83 | |
84 // Save contents of editor back into textarea. | |
85 $field.val(instance.getContent()); | |
86 // If the editor is just being serialized (not detached), our work is done. | |
87 if (trigger == 'serialize') { | |
88 return; | |
89 } | |
90 // Remove editor instance. | |
91 $('#' + id + '-whizzywig').remove(); | |
92 whizzies.splice(index, 1); | |
93 | |
94 // Restore original textarea styling. | |
95 $field.removeAttr('style').attr('style', instance.originalStyle); | |
96 }; | |
97 | |
98 if (typeof params != 'undefined') { | |
99 for (var i = 0; i < whizzies.length; i++) { | |
100 if (whizzies[i] == params.field) { | |
101 detach(i); | |
102 break; | |
103 } | |
104 } | |
105 } | |
106 else { | |
107 while (whizzies.length > 0) { | |
108 detach(0); | |
109 } | |
110 } | |
111 }; | |
112 | |
113 /** | |
114 * Instance methods for Whizzywig. | |
115 */ | |
116 Drupal.wysiwyg.editor.instance.whizzywig = { | |
117 insert: function (content) { | |
118 // Whizzywig executes any string beginning with 'js:'. | |
119 insHTML(content.replace(/^js:/, 'js:')); | |
120 }, | |
121 | |
122 setContent: function (content) { | |
123 var $field = $('#' + this.field); | |
124 // Whizzywig shows the original textarea in source mode. | |
125 if ($field.css('display') == 'block') { | |
126 $field.val(content); | |
127 } | |
128 else { | |
129 var doc = $('#whizzy' + this.field).contents()[0]; | |
130 doc.open(); | |
131 doc.write(content); | |
132 doc.close(); | |
133 } | |
134 }, | |
135 | |
136 getContent: function () { | |
137 var $field = $('#' + this.field), | |
138 // Whizzywig shows the original textarea in source mode. | |
139 content = ($field.css('display') == 'block' ? | |
140 $field.val() : $('#whizzy' + this.field).contents().find('body').html() | |
141 ); | |
142 | |
143 content = tidyH(content); | |
144 // Whizzywig's get_xhtml() addon, if defined, expects a DOM node. | |
145 if ($.isFunction(window.get_xhtml)) { | |
146 var pre = document.createElement('pre'); | |
147 pre.innerHTML = content; | |
148 content = get_xhtml(pre); | |
149 } | |
150 return content.replace(location.href + '#', '#'); | |
151 } | |
152 }; | |
153 | |
154 })(jQuery); |