-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate Grid by Dividing a Box.js
237 lines (211 loc) · 7.27 KB
/
Create Grid by Dividing a Box.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
File: Create Grid by dividing a box
Description: This Java Script creates a grid of boxes from an existing box
*/
//import basic checks
if(typeof(isLayoutOpen) == "undefined")
{
//import basic checks
app.importScript(app.getAppScriptsFolder() + "/Dependencies/qx_validations.js");
console.log("Loaded library for basic validation checks from application.");
}
if(typeof(setUnits) == "undefined")
{
//import measurements
app.importScript(app.getAppScriptsFolder() + "/Dependencies/qx_measurements.js");
console.log("Loaded library for measurements from application.");
}
if(isLayoutOpen())
{
if(isBoxSelected)
{
//Get all Selected Boxes from the Layout
var box = getSelectedBox();
if(!isBoxRotated(box))
{
//get the box width in pts
var boxWidth = getBoxWidth(box);
//get the box height in pts
var boxHeight = getBoxHeight(box);
if(box.getAttribute("box-content-type") == "text")
{
//get the horizontal text inset in pts
var boxTextInsetH = getBoxHorizontalInsets(box);
//get the vertical text inset in pts
var boxTextInsetV = getBoxVerticalInsets(box);
}
else
{
//set the horizontal text inset to zero
var boxTextInsetH = 0;
//set the vertical text inset to zero
var boxTextInsetV = 0;
}
//get the frame width in pts
var boxBorderWidth = getBoxBorderWidth(box);
//calculate the effective box width
var effectiveBoxWidth = boxWidth - (boxTextInsetH + boxBorderWidth);
//calculate the effective box height
var effectiveBoxHeight = boxHeight - (boxTextInsetV + boxBorderWidth);
//get the current horizontal measurement units of the layout
var currHorzUnits= getUnits(box.style.qxLeft);
//get the current vertical measurement units of the layout
var currVertUnits= getUnits(box.style.qxTop);
//input number of rows, columns and gutter widths
var colInput = getValidInput(effectiveBoxWidth, currHorzUnits, "column");
if(colInput != null)
{
var rowInput = getValidInput(effectiveBoxHeight, currVertUnits, "row");
if(rowInput != null)
{
createGrid(colInput, rowInput, box, boxWidth, boxHeight);//all measurements in points
}
}
}
}
}
/*======================Functions used in the JavaScript=============================*/
//check if the box is rotated
function isBoxRotated(box){
if(box.style.qxTransform != "rotate(0deg) skew(0deg)")
{
alert("The script cannot run on rotated or skewed boxes. \nPlease select another box!");
return true;
}
else
{
return false;
}
}
//function to calculate the width of the box in pts
function getBoxWidth(box){
return roundOff((convertAnyUnitToPoints(box.style.qxRight)) - (convertAnyUnitToPoints(box.style.qxLeft)), 1000);
}
//function to calculate the height of the box in pts
function getBoxHeight(box){
return roundOff((convertAnyUnitToPoints(box.style.qxBottom)) - (convertAnyUnitToPoints(box.style.qxTop)), 1000);
}
//function to calculate the horizontal box inset in pts
function getBoxHorizontalInsets(box){
if(box.style.qxPadding == "")//if multiple insets are applied
return roundOff((convertAnyUnitToPoints(box.style.qxPaddingLeft) + convertAnyUnitToPoints(box.style.qxPaddingRight)), 1000);
else
return roundOff((2 * convertAnyUnitToPoints(box.style.qxPadding)), 1000);
}
//function to calculate the horizontal box inset in pts
function getBoxVerticalInsets(box){
if(box.style.qxPadding == "")//if multiple insets are applied
return roundOff((convertAnyUnitToPoints(box.style.qxPaddingTop) + convertAnyUnitToPoints(box.style.qxPaddingBottom)), 1000);
else
return roundOff((2 * convertAnyUnitToPoints(box.style.qxPadding)), 1000);
}
//function to get the width of border applied on the box in pts
function getBoxBorderWidth(box){
return roundOff((2* convertAnyUnitToPoints(box.style.qxBorderWidth)), 1000);
}
//gets the valid input for count and gap
function getValidInput(effectiveBoxDim, currUnits, countOf){
var output= [];
var flag =0;
while(flag != 1){
var count = getCount(countOf);
if(count != null)
{
var inputStr = countOf+" gutter measurement\n\nExamples: 0.5in, 3pt etc.";
var defVal = "1"+currUnits;
var gap = getNumInputWithUnits(inputStr, defVal, currUnits, "0pt", effectiveBoxDim, true);
if(gap != null)
{
//validate column number and gap
if(isValidCountAndGap(count, gap, effectiveBoxDim))
{
output[0]= count;
output[1]= gap;
return output;
}
}
else
{
return null;
}
}
else
{
return null;
}
}
}
//gets valid count input for rows and columns
function getCount(countOf){
var flag =0;
while(flag != 1){
var count = prompt("How many "+countOf+"s?\nWhole numbers only. Partial boxes are not available", "2");
if(count == null)
return null;
else if(isInvalidCount(Number(count)))
flag=0;
else
return Number(count);
}
}
//checks if a given input of row and column count is integer or not
function isInvalidCount(count){
if(!isInt(count)){
alert("Please enter only whole numbers for count!");
return true;
}
return false;
}
//checks if the count and gap measurements are valid or not
function isValidCountAndGap(count, gap, maxValue){
if((count * gap) > maxValue)
{
alert("Values out of Range. Please decrease count or gap!");
return false;
}
else
{
return true;
}
}
//creates a grid of boxes on the given input
function createGrid(colInput, rowInput, box, boxWidth, boxHeight){
var columns= colInput[0];
console.log("Number of columns: "+columns);
var columnGutter= convertAnyUnitToPoints(colInput[1]);//only numeric value in pts
console.log("Column Gutter width: "+ columnGutter+ "pt");
var rows= rowInput[0];
console.log("Number of rows: "+rows);
var rowGutter= convertAnyUnitToPoints(rowInput[1]);
console.log("row Gutter width: "+ rowGutter+ "pt");
//get the content type of the box
var contentType = box.getAttribute("box-content-type");
console.log("Box content type: "+ contentType);
//get the page number of box
var boxpage = box.style.qxPage;
//calculating columnWidth and rowWidth
var columnWidth= (boxWidth- (columns-1)*columnGutter)/columns;
console.log("Column width: "+ columnWidth);
var rowWidth= (boxHeight - (rows-1)*rowGutter)/rows;
console.log("row width: "+ rowWidth);
//getting box offsets
var bTop= convertAnyUnitToPoints(box.style.qxTop);
var bLeft= convertAnyUnitToPoints(box.style.qxLeft);
//declaring an array of boxes
var boxarr= [];
//creating boxes
for(var i= 0; i<rows; i++)
{
for(var j=0; j<columns; j++)
{
boxarr[columns*i +j]= document.createElement("qx-box");
boxarr[columns*i +j].setAttribute("box-content-type", contentType);
boxarr[columns*i +j].style.qxTop= (bTop + ((rowWidth+ Number(rowGutter))*i)) + "pt";
boxarr[columns*i +j].style.qxBottom= (bTop + ((rowWidth+ Number(rowGutter))*i)+ rowWidth) + "pt";
boxarr[columns*i +j].style.qxLeft= (bLeft + ((columnWidth+ Number(columnGutter))*j)) + "pt";
boxarr[columns*i +j].style.qxRight= (bLeft + ((columnWidth+ Number(columnGutter))*j)+ columnWidth) + "pt";
boxarr[columns*i +j].style.qxPage = boxpage;
box.parentNode.appendChild(boxarr[columns*i +j]);
}
}
}