Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / editor / plugins / Blockquote.js

History | View | Annotate | Download (7.6 KB)

1
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6

    
7

    
8
if(!dojo._hasResource["dojox.editor.plugins.Blockquote"]){
9
dojo._hasResource["dojox.editor.plugins.Blockquote"]=true;
10
dojo.provide("dojox.editor.plugins.Blockquote");
11
dojo.require("dijit._editor._Plugin");
12
dojo.require("dijit.form.Button");
13
dojo.require("dojo.i18n");
14
dojo.requireLocalization("dojox.editor.plugins","Blockquote",null,"ROOT,ro");
15
dojo.declare("dojox.editor.plugins.Blockquote",dijit._editor._Plugin,{iconClassPrefix:"dijitAdditionalEditorIcon",_initButton:function(){
16
this._nlsResources=dojo.i18n.getLocalization("dojox.editor.plugins","Blockquote");
17
this.button=new dijit.form.ToggleButton({label:this._nlsResources["blockquote"],showLabel:false,iconClass:this.iconClassPrefix+" "+this.iconClassPrefix+"Blockquote",tabIndex:"-1",onClick:dojo.hitch(this,"_toggleQuote")});
18
},setEditor:function(_1){
19
this.editor=_1;
20
this._initButton();
21
this.connect(this.editor,"onNormalizedDisplayChanged","updateState");
22
_1.customUndo=true;
23
},_toggleQuote:function(_2){
24
try{
25
var ed=this.editor;
26
ed.focus();
27
var _3=this.button.get("checked");
28
var _4=dijit.range.getSelection(ed.window);
29
var _5,_6,_7,_8;
30
if(_4&&_4.rangeCount>0){
31
_5=_4.getRangeAt(0);
32
}
33
if(_5){
34
ed.beginEditing();
35
if(_3){
36
var bq,_9;
37
if(_5.startContainer===_5.endContainer){
38
if(this._isRootInline(_5.startContainer)){
39
_7=_5.startContainer;
40
while(_7&&_7.parentNode!==ed.editNode){
41
_7=_7.parentNode;
42
}
43
while(_7&&_7.previousSibling&&(this._isTextElement(_7)||(_7.nodeType===1&&this._isInlineFormat(this._getTagName(_7))))){
44
_7=_7.previousSibling;
45
}
46
if(_7&&_7.nodeType===1&&!this._isInlineFormat(this._getTagName(_7))){
47
_7=_7.nextSibling;
48
}
49
if(_7){
50
bq=ed.document.createElement("blockquote");
51
dojo.place(bq,_7,"after");
52
bq.appendChild(_7);
53
_8=bq.nextSibling;
54
while(_8&&(this._isTextElement(_8)||(_8.nodeType===1&&this._isInlineFormat(this._getTagName(_8))))){
55
bq.appendChild(_8);
56
_8=bq.nextSibling;
57
}
58
}
59
}else{
60
var _a=_5.startContainer;
61
while((this._isTextElement(_a)||this._isInlineFormat(this._getTagName(_a))||this._getTagName(_a)==="li")&&_a!==ed.editNode&&_a!==ed.document.body){
62
_a=_a.parentNode;
63
}
64
if(_a!==ed.editNode&&_a!==_a.ownerDocument.documentElement){
65
bq=ed.document.createElement("blockquote");
66
dojo.place(bq,_a,"after");
67
bq.appendChild(_a);
68
}
69
}
70
if(bq){
71
dojo.withGlobal(ed.window,"selectElementChildren",dijit._editor.selection,[bq]);
72
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
73
}
74
}else{
75
var _b;
76
_7=_5.startContainer;
77
_8=_5.endContainer;
78
while(_7&&this._isTextElement(_7)&&_7.parentNode!==ed.editNode){
79
_7=_7.parentNode;
80
}
81
_b=_7;
82
while(_b.nextSibling&&dojo.withGlobal(ed.window,"inSelection",dijit._editor.selection,[_b])){
83
_b=_b.nextSibling;
84
}
85
_8=_b;
86
if(_8===ed.editNode||_8===ed.document.body){
87
bq=ed.document.createElement("blockquote");
88
dojo.place(bq,_7,"after");
89
_9=this._getTagName(_7);
90
if(this._isTextElement(_7)||this._isInlineFormat(_9)){
91
var _c=_7;
92
while(_c&&(this._isTextElement(_c)||(_c.nodeType===1&&this._isInlineFormat(this._getTagName(_c))))){
93
bq.appendChild(_c);
94
_c=bq.nextSibling;
95
}
96
}else{
97
bq.appendChild(_7);
98
}
99
return;
100
}
101
_8=_8.nextSibling;
102
_b=_7;
103
while(_b&&_b!==_8){
104
if(_b.nodeType===1){
105
_9=this._getTagName(_b);
106
if(_9!=="br"){
107
if(!window.getSelection){
108
if(_9==="p"&&this._isEmpty(_b)){
109
_b=_b.nextSibling;
110
continue;
111
}
112
}
113
if(this._isInlineFormat(_9)){
114
if(!bq){
115
bq=ed.document.createElement("blockquote");
116
dojo.place(bq,_b,"after");
117
bq.appendChild(_b);
118
}else{
119
bq.appendChild(_b);
120
}
121
_b=bq;
122
}else{
123
if(bq){
124
if(this._isEmpty(bq)){
125
bq.parentNode.removeChild(bq);
126
}
127
}
128
bq=ed.document.createElement("blockquote");
129
dojo.place(bq,_b,"after");
130
bq.appendChild(_b);
131
_b=bq;
132
}
133
}
134
}else{
135
if(this._isTextElement(_b)){
136
if(!bq){
137
bq=ed.document.createElement("blockquote");
138
dojo.place(bq,_b,"after");
139
bq.appendChild(_b);
140
}else{
141
bq.appendChild(_b);
142
}
143
_b=bq;
144
}
145
}
146
_b=_b.nextSibling;
147
}
148
if(bq){
149
if(this._isEmpty(bq)){
150
bq.parentNode.removeChild(bq);
151
}else{
152
dojo.withGlobal(ed.window,"selectElementChildren",dijit._editor.selection,[bq]);
153
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
154
}
155
bq=null;
156
}
157
}
158
}else{
159
var _d=false;
160
if(_5.startContainer===_5.endContainer){
161
_6=_5.endContainer;
162
while(_6&&_6!==ed.editNode&&_6!==ed.document.body){
163
var tg=_6.tagName?_6.tagName.toLowerCase():"";
164
if(tg==="blockquote"){
165
_d=true;
166
break;
167
}
168
_6=_6.parentNode;
169
}
170
if(_d){
171
var _e;
172
while(_6.firstChild){
173
_e=_6.firstChild;
174
dojo.place(_e,_6,"before");
175
}
176
_6.parentNode.removeChild(_6);
177
if(_e){
178
dojo.withGlobal(ed.window,"selectElementChildren",dijit._editor.selection,[_e]);
179
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
180
}
181
}
182
}else{
183
_7=_5.startContainer;
184
_8=_5.endContainer;
185
while(_7&&this._isTextElement(_7)&&_7.parentNode!==ed.editNode){
186
_7=_7.parentNode;
187
}
188
var _f=[];
189
var _10=_7;
190
while(_10&&_10.nextSibling&&dojo.withGlobal(ed.window,"inSelection",dijit._editor.selection,[_10])){
191
if(_10.parentNode&&this._getTagName(_10.parentNode)==="blockquote"){
192
_10=_10.parentNode;
193
}
194
_f.push(_10);
195
_10=_10.nextSibling;
196
}
197
var _11=this._findBlockQuotes(_f);
198
while(_11.length){
199
var bn=_11.pop();
200
if(bn.parentNode){
201
while(bn.firstChild){
202
dojo.place(bn.firstChild,bn,"before");
203
}
204
bn.parentNode.removeChild(bn);
205
}
206
}
207
}
208
}
209
ed.endEditing();
210
}
211
ed.onNormalizedDisplayChanged();
212
}
213
catch(e){
214
}
215
},updateState:function(){
216
var ed=this.editor;
217
if(!ed||!ed.isLoaded){
218
return;
219
}
220
if(this.button){
221
var _12;
222
var _13=false;
223
var sel=dijit.range.getSelection(ed.window);
224
if(sel&&sel.rangeCount>0){
225
var _14=sel.getRangeAt(0);
226
if(_14){
227
_12=_14.endContainer;
228
}
229
}
230
while(_12&&_12!==ed.editNode&&_12!==ed.document){
231
var tg=_12.tagName?_12.tagName.toLowerCase():"";
232
if(tg==="blockquote"){
233
_13=true;
234
break;
235
}
236
_12=_12.parentNode;
237
}
238
this.button.set("checked",_13);
239
}
240
},_findBlockQuotes:function(_15){
241
var _16=[];
242
if(_15){
243
var i;
244
for(i=0;i<_15.length;i++){
245
var _17=_15[i];
246
if(_17.nodeType===1){
247
if(this._getTagName(_17)==="blockquote"){
248
_16.push(_17);
249
}
250
if(_17.childNodes&&_17.childNodes.length>0){
251
_16=_16.concat(this._findBlockQuotes(_17.childNodes));
252
}
253
}
254
}
255
}
256
return _16;
257
},_getTagName:function(_18){
258
var tag="";
259
if(_18&&_18.nodeType===1){
260
tag=_18.tagName?_18.tagName.toLowerCase():"";
261
}
262
return tag;
263
},_isRootInline:function(_19){
264
var ed=this.editor;
265
if(this._isTextElement(_19)&&_19.parentNode===ed.editNode){
266
return true;
267
}else{
268
if(_19.nodeType===1&&this._isInlineFormat(_19)&&_19.parentNode===ed.editNode){
269
return true;
270
}else{
271
if(this._isTextElement(_19)&&this._isInlineFormat(this._getTagName(_19.parentNode))){
272
_19=_19.parentNode;
273
while(_19&&_19!==ed.editNode&&this._isInlineFormat(this._getTagName(_19))){
274
_19=_19.parentNode;
275
}
276
if(_19===ed.editNode){
277
return true;
278
}
279
}
280
}
281
}
282
return false;
283
},_isTextElement:function(_1a){
284
if(_1a&&_1a.nodeType===3||_1a.nodeType===4){
285
return true;
286
}
287
return false;
288
},_isEmpty:function(_1b){
289
if(_1b.childNodes){
290
var _1c=true;
291
var i;
292
for(i=0;i<_1b.childNodes.length;i++){
293
var n=_1b.childNodes[i];
294
if(n.nodeType===1){
295
if(this._getTagName(n)==="p"){
296
if(!dojo.trim(n.innerHTML)){
297
continue;
298
}
299
}
300
_1c=false;
301
break;
302
}else{
303
if(this._isTextElement(n)){
304
var nv=dojo.trim(n.nodeValue);
305
if(nv&&nv!=="&nbsp;"&&nv!==" "){
306
_1c=false;
307
break;
308
}
309
}else{
310
_1c=false;
311
break;
312
}
313
}
314
}
315
return _1c;
316
}else{
317
return true;
318
}
319
},_isInlineFormat:function(tag){
320
switch(tag){
321
case "a":
322
case "b":
323
case "strong":
324
case "s":
325
case "strike":
326
case "i":
327
case "u":
328
case "em":
329
case "sup":
330
case "sub":
331
case "span":
332
case "font":
333
case "big":
334
case "cite":
335
case "q":
336
case "img":
337
case "small":
338
return true;
339
default:
340
return false;
341
}
342
}});
343
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){
344
if(o.plugin){
345
return;
346
}
347
var _1d=o.args.name.toLowerCase();
348
if(_1d==="blockquote"){
349
o.plugin=new dojox.editor.plugins.Blockquote({});
350
}
351
});
352
}