Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (12.2 KB)

1 9 andrej.cim
/*
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.NormalizeIndentOutdent"]){
9
dojo._hasResource["dojox.editor.plugins.NormalizeIndentOutdent"]=true;
10
dojo.provide("dojox.editor.plugins.NormalizeIndentOutdent");
11
dojo.require("dijit._editor._Plugin");
12
dojo.require("dijit._editor.selection");
13
dojo.experimental("dojox.editor.plugins.NormalizeIndentOutdent");
14
dojo.declare("dojox.editor.plugins.NormalizeIndentOutdent",dijit._editor._Plugin,{indentBy:40,indentUnits:"px",setEditor:function(_1){
15
this.editor=_1;
16
_1._indentImpl=dojo.hitch(this,this._indentImpl);
17
_1._outdentImpl=dojo.hitch(this,this._outdentImpl);
18
if(!_1._indentoutdent_queryCommandEnabled){
19
_1._indentoutdent_queryCommandEnabled=_1.queryCommandEnabled;
20
}
21
_1.queryCommandEnabled=dojo.hitch(this,this._queryCommandEnabled);
22
_1.customUndo=true;
23
},_queryCommandEnabled:function(_2){
24
var c=_2.toLowerCase();
25
var ed,_3,_4,_5,_6,_7;
26
var _8="marginLeft";
27
if(!this._isLtr()){
28
_8="marginRight";
29
}
30
if(c==="indent"){
31
ed=this.editor;
32
_3=dijit.range.getSelection(ed.window);
33
if(_3&&_3.rangeCount>0){
34
_4=_3.getRangeAt(0);
35
_5=_4.startContainer;
36
while(_5&&_5!==ed.document&&_5!==ed.editNode){
37
_6=this._getTagName(_5);
38
if(_6==="li"){
39
_7=_5.previousSibling;
40
while(_7&&_7.nodeType!==1){
41
_7=_7.previousSibling;
42
}
43
if(_7&&this._getTagName(_7)==="li"){
44
return true;
45
}else{
46
return false;
47
}
48
}else{
49
if(this._isIndentableElement(_6)){
50
return true;
51
}
52
}
53
_5=_5.parentNode;
54
}
55
if(this._isRootInline(_4.startContainer)){
56
return true;
57
}
58
}
59
}else{
60
if(c==="outdent"){
61
ed=this.editor;
62
_3=dijit.range.getSelection(ed.window);
63
if(_3&&_3.rangeCount>0){
64
_4=_3.getRangeAt(0);
65
_5=_4.startContainer;
66
while(_5&&_5!==ed.document&&_5!==ed.editNode){
67
_6=this._getTagName(_5);
68
if(_6==="li"){
69
return this.editor._indentoutdent_queryCommandEnabled(_2);
70
}else{
71
if(this._isIndentableElement(_6)){
72
var _9=_5.style?_5.style[_8]:"";
73
if(_9){
74
_9=this._convertIndent(_9);
75
if(_9/this.indentBy>=1){
76
return true;
77
}
78
}
79
return false;
80
}
81
}
82
_5=_5.parentNode;
83
}
84
if(this._isRootInline(_4.startContainer)){
85
return false;
86
}
87
}
88
}else{
89
return this.editor._indentoutdent_queryCommandEnabled(_2);
90
}
91
}
92
return false;
93
},_indentImpl:function(_a){
94
var ed=this.editor;
95
var _b=dijit.range.getSelection(ed.window);
96
if(_b&&_b.rangeCount>0){
97
var _c=_b.getRangeAt(0);
98
var _d=_c.startContainer;
99
var _e,_f,end,div;
100
if(_c.startContainer===_c.endContainer){
101
if(this._isRootInline(_c.startContainer)){
102
_f=_c.startContainer;
103
while(_f&&_f.parentNode!==ed.editNode){
104
_f=_f.parentNode;
105
}
106
while(_f&&_f.previousSibling&&(this._isTextElement(_f)||(_f.nodeType===1&&this._isInlineFormat(this._getTagName(_f))))){
107
_f=_f.previousSibling;
108
}
109
if(_f&&_f.nodeType===1&&!this._isInlineFormat(this._getTagName(_f))){
110
_f=_f.nextSibling;
111
}
112
if(_f){
113
div=ed.document.createElement("div");
114
dojo.place(div,_f,"after");
115
div.appendChild(_f);
116
end=div.nextSibling;
117
while(end&&(this._isTextElement(end)||(end.nodeType===1&&this._isInlineFormat(this._getTagName(end))))){
118
div.appendChild(end);
119
end=div.nextSibling;
120
}
121
this._indentElement(div);
122
dojo.withGlobal(ed.window,"selectElementChildren",dijit._editor.selection,[div]);
123
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
124
}
125
}else{
126
while(_d&&_d!==ed.document&&_d!==ed.editNode){
127
_e=this._getTagName(_d);
128
if(_e==="li"){
129
this._indentList(_d);
130
return;
131
}else{
132
if(this._isIndentableElement(_e)){
133
this._indentElement(_d);
134
return;
135
}
136
}
137
_d=_d.parentNode;
138
}
139
}
140
}else{
141
var _10;
142
_f=_c.startContainer;
143
end=_c.endContainer;
144
while(_f&&this._isTextElement(_f)&&_f.parentNode!==ed.editNode){
145
_f=_f.parentNode;
146
}
147
while(end&&this._isTextElement(end)&&end.parentNode!==ed.editNode){
148
end=end.parentNode;
149
}
150
if(end===ed.editNode||end===ed.document.body){
151
_10=_f;
152
while(_10.nextSibling&&dojo.withGlobal(ed.window,"inSelection",dijit._editor.selection,[_10])){
153
_10=_10.nextSibling;
154
}
155
end=_10;
156
if(end===ed.editNode||end===ed.document.body){
157
_e=this._getTagName(_f);
158
if(_e==="li"){
159
this._indentList(_f);
160
}else{
161
if(this._isIndentableElement(_e)){
162
this._indentElement(_f);
163
}else{
164
if(this._isTextElement(_f)||this._isInlineFormat(_e)){
165
div=ed.document.createElement("div");
166
dojo.place(div,_f,"after");
167
var _11=_f;
168
while(_11&&(this._isTextElement(_11)||(_11.nodeType===1&&this._isInlineFormat(this._getTagName(_11))))){
169
div.appendChild(_11);
170
_11=div.nextSibling;
171
}
172
this._indentElement(div);
173
}
174
}
175
}
176
return;
177
}
178
}
179
end=end.nextSibling;
180
_10=_f;
181
while(_10&&_10!==end){
182
if(_10.nodeType===1){
183
_e=this._getTagName(_10);
184
if(dojo.isIE){
185
if(_e==="p"&&this._isEmpty(_10)){
186
_10=_10.nextSibling;
187
continue;
188
}
189
}
190
if(_e==="li"){
191
if(div){
192
if(this._isEmpty(div)){
193
div.parentNode.removeChild(div);
194
}else{
195
this._indentElement(div);
196
}
197
div=null;
198
}
199
this._indentList(_10);
200
}else{
201
if(!this._isInlineFormat(_e)&&this._isIndentableElement(_e)){
202
if(div){
203
if(this._isEmpty(div)){
204
div.parentNode.removeChild(div);
205
}else{
206
this._indentElement(div);
207
}
208
div=null;
209
}
210
_10=this._indentElement(_10);
211
}else{
212
if(this._isInlineFormat(_e)){
213
if(!div){
214
div=ed.document.createElement("div");
215
dojo.place(div,_10,"after");
216
div.appendChild(_10);
217
_10=div;
218
}else{
219
div.appendChild(_10);
220
_10=div;
221
}
222
}
223
}
224
}
225
}else{
226
if(this._isTextElement(_10)){
227
if(!div){
228
div=ed.document.createElement("div");
229
dojo.place(div,_10,"after");
230
div.appendChild(_10);
231
_10=div;
232
}else{
233
div.appendChild(_10);
234
_10=div;
235
}
236
}
237
}
238
_10=_10.nextSibling;
239
}
240
if(div){
241
if(this._isEmpty(div)){
242
div.parentNode.removeChild(div);
243
}else{
244
this._indentElement(div);
245
}
246
div=null;
247
}
248
}
249
}
250
},_indentElement:function(_12){
251
var _13="marginLeft";
252
if(!this._isLtr()){
253
_13="marginRight";
254
}
255
var tag=this._getTagName(_12);
256
if(tag==="ul"||tag==="ol"){
257
var div=this.editor.document.createElement("div");
258
dojo.place(div,_12,"after");
259
div.appendChild(_12);
260
_12=div;
261
}
262
var _14=_12.style?_12.style[_13]:"";
263
if(_14){
264
_14=this._convertIndent(_14);
265
_14=(parseInt(_14,10)+this.indentBy)+this.indentUnits;
266
}else{
267
_14=this.indentBy+this.indentUnits;
268
}
269
dojo.style(_12,_13,_14);
270
return _12;
271
},_outdentElement:function(_15){
272
var _16="marginLeft";
273
if(!this._isLtr()){
274
_16="marginRight";
275
}
276
var _17=_15.style?_15.style[_16]:"";
277
if(_17){
278
_17=this._convertIndent(_17);
279
if(_17-this.indentBy>0){
280
_17=(parseInt(_17,10)-this.indentBy)+this.indentUnits;
281
}else{
282
_17="";
283
}
284
dojo.style(_15,_16,_17);
285
}
286
},_outdentImpl:function(_18){
287
var ed=this.editor;
288
var sel=dijit.range.getSelection(ed.window);
289
if(sel&&sel.rangeCount>0){
290
var _19=sel.getRangeAt(0);
291
var _1a=_19.startContainer;
292
var tag;
293
if(_19.startContainer===_19.endContainer){
294
while(_1a&&_1a!==ed.document&&_1a!==ed.editNode){
295
tag=this._getTagName(_1a);
296
if(tag==="li"){
297
return this._outdentList(_1a);
298
}else{
299
if(this._isIndentableElement(tag)){
300
return this._outdentElement(_1a);
301
}
302
}
303
_1a=_1a.parentNode;
304
}
305
ed.document.execCommand("outdent",false,_18);
306
}else{
307
var _1b=_19.startContainer;
308
var end=_19.endContainer;
309
while(_1b&&_1b.nodeType===3){
310
_1b=_1b.parentNode;
311
}
312
while(end&&end.nodeType===3){
313
end=end.parentNode;
314
}
315
end=end.nextSibling;
316
var _1c=_1b;
317
while(_1c&&_1c!==end){
318
if(_1c.nodeType===1){
319
tag=this._getTagName(_1c);
320
if(tag==="li"){
321
this._outdentList(_1c);
322
}else{
323
if(this._isIndentableElement(tag)){
324
this._outdentElement(_1c);
325
}
326
}
327
}
328
_1c=_1c.nextSibling;
329
}
330
}
331
}
332
return null;
333
},_indentList:function(_1d){
334
var ed=this.editor;
335
var _1e,li;
336
var _1f=_1d.parentNode;
337
var _20=_1d.previousSibling;
338
while(_20&&_20.nodeType!==1){
339
_20=_20.previousSibling;
340
}
341
var _21=null;
342
var tg=this._getTagName(_1f);
343
if(tg==="ol"){
344
_21="ol";
345
}else{
346
if(tg==="ul"){
347
_21="ul";
348
}
349
}
350
if(_21){
351
if(_20&&_20.tagName.toLowerCase()=="li"){
352
var _22;
353
if(_20.childNodes){
354
var i;
355
for(i=0;i<_20.childNodes.length;i++){
356
var n=_20.childNodes[i];
357
if(n.nodeType===3){
358
if(dojo.trim(n.nodeValue)){
359
if(_22){
360
break;
361
}
362
}
363
}else{
364
if(n.nodeType===1&&!_22){
365
if(_21===n.tagName.toLowerCase()){
366
_22=n;
367
}
368
}else{
369
break;
370
}
371
}
372
}
373
}
374
if(_22){
375
_22.appendChild(_1d);
376
}else{
377
_1e=ed.document.createElement(_21);
378
dojo.style(_1e,{paddingTop:"0px",paddingBottom:"0px"});
379
li=ed.document.createElement("li");
380
dojo.style(li,{listStyleImage:"none",listStyleType:"none"});
381
_20.appendChild(_1e);
382
_1e.appendChild(_1d);
383
}
384
dojo.withGlobal(ed.window,"selectElementChildren",dijit._editor.selection,[_1d]);
385
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
386
}
387
}
388
},_outdentList:function(_23){
389
var ed=this.editor;
390
var _24=_23.parentNode;
391
var _25=null;
392
var tg=_24.tagName?_24.tagName.toLowerCase():"";
393
var li;
394
if(tg==="ol"){
395
_25="ol";
396
}else{
397
if(tg==="ul"){
398
_25="ul";
399
}
400
}
401
var _26=_24.parentNode;
402
var _27=this._getTagName(_26);
403
if(_27==="li"||_27==="ol"||_27==="ul"){
404
if(_27==="ol"||_27==="ul"){
405
var _28=_24.previousSibling;
406
while(_28&&(_28.nodeType!==1||(_28.nodeType===1&&this._getTagName(_28)!=="li"))){
407
_28=_28.previousSibling;
408
}
409
if(_28){
410
_28.appendChild(_24);
411
_26=_28;
412
}else{
413
li=_23;
414
var _29=_23;
415
while(li.previousSibling){
416
li=li.previousSibling;
417
if(li.nodeType===1&&this._getTagName(li)==="li"){
418
_29=li;
419
}
420
}
421
if(_29!==_23){
422
dojo.place(_29,_24,"before");
423
_29.appendChild(_24);
424
_26=_29;
425
}else{
426
li=ed.document.createElement("li");
427
dojo.place(li,_24,"before");
428
li.appendChild(_24);
429
_26=li;
430
}
431
dojo.style(_24,{paddingTop:"0px",paddingBottom:"0px"});
432
}
433
}
434
var _2a=_23.previousSibling;
435
while(_2a&&_2a.nodeType!==1){
436
_2a=_2a.previousSibling;
437
}
438
var _2b=_23.nextSibling;
439
while(_2b&&_2b.nodeType!==1){
440
_2b=_2b.nextSibling;
441
}
442
if(!_2a){
443
dojo.place(_23,_26,"after");
444
_23.appendChild(_24);
445
}else{
446
if(!_2b){
447
dojo.place(_23,_26,"after");
448
}else{
449
var _2c=ed.document.createElement(_25);
450
dojo.style(_2c,{paddingTop:"0px",paddingBottom:"0px"});
451
_23.appendChild(_2c);
452
while(_23.nextSibling){
453
_2c.appendChild(_23.nextSibling);
454
}
455
dojo.place(_23,_26,"after");
456
}
457
}
458
if(_24&&this._isEmpty(_24)){
459
_24.parentNode.removeChild(_24);
460
}
461
if(_26&&this._isEmpty(_26)){
462
_26.parentNode.removeChild(_26);
463
}
464
dojo.withGlobal(ed.window,"selectElementChildren",dijit._editor.selection,[_23]);
465
dojo.withGlobal(ed.window,"collapse",dijit._editor.selection,[true]);
466
}else{
467
ed.document.execCommand("outdent",false,null);
468
}
469
},_isEmpty:function(_2d){
470
if(_2d.childNodes){
471
var _2e=true;
472
var i;
473
for(i=0;i<_2d.childNodes.length;i++){
474
var n=_2d.childNodes[i];
475
if(n.nodeType===1){
476
if(this._getTagName(n)==="p"){
477
if(!dojo.trim(n.innerHTML)){
478
continue;
479
}
480
}
481
_2e=false;
482
break;
483
}else{
484
if(this._isTextElement(n)){
485
var nv=dojo.trim(n.nodeValue);
486
if(nv&&nv!=="&nbsp;"&&nv!==" "){
487
_2e=false;
488
break;
489
}
490
}else{
491
_2e=false;
492
break;
493
}
494
}
495
}
496
return _2e;
497
}else{
498
return true;
499
}
500
},_isIndentableElement:function(tag){
501
switch(tag){
502
case "p":
503
case "div":
504
case "h1":
505
case "h2":
506
case "h3":
507
case "center":
508
case "table":
509
case "ul":
510
case "ol":
511
return true;
512
default:
513
return false;
514
}
515
},_convertIndent:function(_2f){
516
var _30=12;
517
_2f=_2f+"";
518
_2f=_2f.toLowerCase();
519
var _31=(_2f.indexOf("px")>0)?"px":(_2f.indexOf("em")>0)?"em":"px";
520
_2f=_2f.replace(/(px;?|em;?)/gi,"");
521
if(_31==="px"){
522
if(this.indentUnits==="em"){
523
_2f=Math.ceil(_2f/_30);
524
}
525
}else{
526
if(this.indentUnits==="px"){
527
_2f=_2f*_30;
528
}
529
}
530
return _2f;
531
},_isLtr:function(){
532
var _32=this.editor.document.body;
533
return dojo.withGlobal(this.editor.window,function(){
534
var cs=dojo.getComputedStyle(_32);
535
return cs?cs.direction=="ltr":true;
536
});
537
},_isInlineFormat:function(tag){
538
switch(tag){
539
case "a":
540
case "b":
541
case "strong":
542
case "s":
543
case "strike":
544
case "i":
545
case "u":
546
case "em":
547
case "sup":
548
case "sub":
549
case "span":
550
case "font":
551
case "big":
552
case "cite":
553
case "q":
554
case "img":
555
case "small":
556
return true;
557
default:
558
return false;
559
}
560
},_getTagName:function(_33){
561
var tag="";
562
if(_33&&_33.nodeType===1){
563
tag=_33.tagName?_33.tagName.toLowerCase():"";
564
}
565
return tag;
566
},_isRootInline:function(_34){
567
var ed=this.editor;
568
if(this._isTextElement(_34)&&_34.parentNode===ed.editNode){
569
return true;
570
}else{
571
if(_34.nodeType===1&&this._isInlineFormat(_34)&&_34.parentNode===ed.editNode){
572
return true;
573
}else{
574
if(this._isTextElement(_34)&&this._isInlineFormat(this._getTagName(_34.parentNode))){
575
_34=_34.parentNode;
576
while(_34&&_34!==ed.editNode&&this._isInlineFormat(this._getTagName(_34))){
577
_34=_34.parentNode;
578
}
579
if(_34===ed.editNode){
580
return true;
581
}
582
}
583
}
584
}
585
return false;
586
},_isTextElement:function(_35){
587
if(_35&&_35.nodeType===3||_35.nodeType===4){
588
return true;
589
}
590
return false;
591
}});
592
dojo.subscribe(dijit._scopeName+".Editor.getPlugin",null,function(o){
593
if(o.plugin){
594
return;
595
}
596
var _36=o.args.name.toLowerCase();
597
if(_36==="normalizeindentoutdent"){
598
o.plugin=new dojox.editor.plugins.NormalizeIndentOutdent({indentBy:("indentBy" in o.args)?(o.args.indentBy>0?o.args.indentBy:40):40,indentUnits:("indentUnits" in o.args)?(o.args.indentUnits.toLowerCase()=="em"?"em":"px"):"px"});
599
}
600
});
601
}