source: gs3-extensions/seaweed-debug/trunk/src/actions/IndentAction.js@ 25160

Last change on this file since 25160 was 25160, checked in by sjm84, 12 years ago

Initial cut at a version of seaweed for debugging purposes. Check it out live into the web/ext folder

File size: 5.6 KB
Line 
1/*
2 * file: IndentAction.js
3 *
4 * @BEGINLICENSE
5 * Copyright 2010 Brook Novak (email : [email protected])
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * @ENDLICENSE
18 */
19
20// @DEPENDS: UndoMan
21bootstrap.provides("actions.IndentAction");
22
23(function() {
24
25 /**
26 * The amount of pixels to adjust margins when increasing and decreasing indents.
27 * @type Number
28 */
29 var INDENT_WIDTH = 20,
30 undentableBlocks = $createLookupMap("dt,dd,caption,colgroup,col,thead,tfoot,tbody,tr,td,th,legend,optgroup,option,area,frame");
31
32 _registerAction("Indent", {
33 /**
34 * An undoable indentation action. Increases or decreases left indents on container elements in a given range.
35 * If nodes in the given range should be indented but do not have a container, a new container is created for them.
36 *
37 * @author Brook Novak
38 *
39 * @param {Boolean} increase True to increase indents in range, false to decrease indents.
40 *
41 * @param {Node} startNode (Optional) The starting dom node of the range to align.
42 * If not provided then the current selection will be used.
43 * If provieded must also provide endNode
44 *
45 * @param {Node} endNode (Optional) The ending dom node of the range to align. Can be the same as start node
46 * If not provided then the current selection will be used.
47 *
48 *
49 */
50 exec : function(increase, startNode, endNode) {
51
52 // Auto-set range if not provided.
53 if (!startNode) {
54
55 if (!this.selBefore)
56 return; // Nothing to select
57
58 if (this.selBefore.endNode) {
59 startNode = this.selBeforeOrdered.startNode;
60 endNode = this.selBeforeOrdered.endNode;
61 } else
62 startNode = endNode = this.selBefore.startNode;
63
64 }
65
66 debug.assert(endNode, "Supplied start node but not the end node");
67
68 var containers;
69
70 // First check for special case: If the ranges first block level common ancestor
71 // is a list item, then indent the list item rather than normalizing within a list item.
72 var ca = _getCommonAncestor(startNode, endNode);
73 for (var level = 0; level < 2; level++) {
74 while (ca != docBody && !_isBlockLevel(ca)) {
75 ca = ca.parentNode;
76 }
77
78 if (ca == docBody)
79 break;
80
81 if (_nodeName(ca) == "li") {
82 containers = [ca];
83 break;
84 }
85 ca = ca.parentNode;
86 }
87
88 if (!containers)
89 // Normalize containers in range and get list of all the containers
90 containers = _getNormalizedContainerRange(startNode, endNode);
91
92 // Record all indentable containers in the given range
93 for (var i in containers) {
94 if (!undentableBlocks[_nodeName(containers[i])]) {
95
96 var con = containers[i];
97
98 // Determine the containers left margin in pixels
99 var marginToParse = (con.style.marginLeft && con.style.marginLeft.toLowerCase() != "auto") ?
100 con.style.marginLeft : null;
101
102 // If margin is default/auto, get the computed style to make sure
103 if (!marginToParse)
104 marginToParse = _getComputedStyle(con, "margin-left");
105
106 marginToParse = (marginToParse && marginToParse.toLowerCase() != "auto") ?
107 marginToParse : "0";
108
109 // Parse intentation numberic value
110 var marginPixels;
111 if (marginToParse.indexOf("%") != -1) {
112 // Convert percentage into pixels by using the containers relative offset
113 marginPixels = con.offsetLeft;
114
115 } else marginPixels = parseInt(marginToParse);
116
117 var newMarginLen = marginPixels + ((increase ? 1 : -1) * INDENT_WIDTH);
118
119 // Clip margin to zero
120 if (newMarginLen < 0)
121 newMarginLen = 0;
122
123 // Assign new margin
124 if (marginPixels != newMarginLen)
125 _execOp(_Operation.SET_CSS_STYLE, con, "marginLeft", newMarginLen + "px");
126
127 }
128 }
129
130 this.selAfter = this.selBefore;
131
132 }
133 });
134
135})();
Note: See TracBrowser for help on using the repository browser.