-
Notifications
You must be signed in to change notification settings - Fork 0
/
No-fuss LEGAL NUMBERING.js
340 lines (295 loc) · 12.3 KB
/
No-fuss LEGAL NUMBERING.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//*******************************************\\
//This toggles legal numbering at the start of each bookmark
var pattern_legal_numbering=/\b([0-9]+(\.)?[0-9]*)+\:($| )/;
var leg_num_thm_count;
function RemoveLegalNum(Bm, nLevel, oDoc, n, count){
leg_num_thm_count=leg_num_thm_count+1;
if(n!=0){
app.thermometer.value=count;
app.thermometer.text="Removing legal numbering from " + count + " out of " + n + " bookmarks.";
}
//Remove label
Bm.name=RemoveLegalNumThis(Bm);
// process children
if (Bm.children != null){
for (var i = 0; i < Bm.children.length; i++){
RemoveLegalNum(Bm.children[i], nLevel + 1, oDoc, n, count);
}
}
return true;
}
function RemoveLegalNumThisFromString(S){
return S.replace(pattern_legal_numbering,"").trim();
}
function RemoveLegalNumThis(Bm){
return Bm.name.replace(pattern_legal_numbering,"").trim();
}
function AddLegalNum(Bm, nLevel, oDoc, s, counter, nLvlMax, n){
//console.println("Level and Max: " + nLevel + ", " + nLvlMax);
//Add legal numbering
if (Bm.style!=1){ //skip italicised
leg_num_thm_count=leg_num_thm_count+1;
if(n!=0){
app.thermometer.value=leg_num_thm_count;
app.thermometer.text="Adding legal numbering to " + leg_num_thm_count + " out of " + n + " bookmarks.";
}
if(nLevel>0){
s=s+(counter+1).toString()+".";
Bm.name=s.slice(0,s.length-1) + ": "+ Bm.name.trim();
}
// process children
//console.println("Max : " + nLvlMax);
if (nLevel<nLvlMax && Bm.children != null){
var counter=0;
for (var i = 0; i < Bm.children.length; i++){
if(AddLegalNum(Bm.children[i], nLevel + 1, oDoc, s, counter, nLvlMax, n)) counter=counter+1;
}
}
s=""; //reset string
return true;
}else{
return false;
}
}
function GetLegalNumberFromString(S){
//returns a string of the legal numbering
var a=S.match(pattern_legal_numbering);
//if (a.length==0) return null;
if(a!=null){
a[0]=a[0].replace(":","");
a[0]=a[0].toString().trim();
//console.println(Bm.name + " legal no " + a[0]);
return a[0];
}
return null;
}
function GetLegalNumber(Bm){
//returns a string of the legal numbering
var a=Bm.name.match(pattern_legal_numbering);
//if (a.length==0) return null;
if(a!=null){
a[0]=a[0].replace(":","");
a[0]=a[0].toString().trim();
//console.println(Bm.name + " legal no " + a[0]);
return a[0];
}
return null;
}
function LegalNumPresent(Bm, nLevel, oDoc){
//see if there is match
if(pattern_legal_numbering.test(Bm.name)) return true;
// process children
if (Bm.children != null){
for (var i = 0; i < Bm.children.length; i++){
if(LegalNumPresent(Bm.children[i], nLevel + 1, oDoc)==true) return true;
}
}
return false;
}
function RefreshLegalNum(oDoc){
//Refreshes the legal numbering if present
var nDepth = FindMaxBkDepth(oDoc.bookmarkRoot);
var n=FindNumBks(oDoc.bookmarkRoot.children,nDepth)
if (nDepth>0){
if(LegalNumPresent(oDoc.bookmarkRoot,0,oDoc)){
if(RemoveLegalNum(oDoc.bookmarkRoot, 0, oDoc, n)){
oDoc.info.LegalNumExists=false;
if(oDoc.info.TOCnLevel){ //keep persistent value within bounds
if(oDoc.info.LNnLevel<1) oDoc.info.LNnLevel=1;
if(oDoc.info.LNnLevel>nDepth) oDoc.info.LNnLevel=nDepth;
}else{
oDoc.info.TOCnLevel=nDepth; //set to default if doesn't exist
}
app.thermometer.duration=n;
app.thermometer.begin();
leg_num_thm_count=0;
AddLegalNum(oDoc.bookmarkRoot,0,oDoc, "", 0, oDoc.info.LNnLevel, n); //Add the legal numbering
app.thermometer.end();
oDoc.info.LegalNumExists=true;
}
}
return true;
}else{
//no bookmarks
}
}
// // // // // // // // // // // // ///
//
// This is the main function for legal numbering
//
var DoLEGAL_NUM = app.trustedFunction(function(oDoc,oDlg)
{
if(!CheckPermitted())return;
app.beginPriv();
var Pg=oDoc.pageNum; //note page number
var nDepth = FindMaxBkDepth(oDoc.bookmarkRoot);
if(nDepth > 0){
if(LegalNumPresent(oDoc.bookmarkRoot,0,oDoc)){
var n=FindNumBks(oDoc.bookmarkRoot.children,nDepth)
//console.println("Num remove " + n);
app.thermometer.duration=n;
app.thermometer.begin();
leg_num_thm_count=0;
RemoveLegalNum(oDoc.bookmarkRoot, 0, oDoc, n); //Remove the legal numbering
app.thermometer.end();
oDoc.info.LegalNumExists=false;
}else{
LegalNoDlg.strTitle = "";
LegalNoDlg.nLvlMax = nDepth.toString();
//Keep the persistent nLevel within bounds
if(oDoc.info.TOCnLevel){
if(oDoc.info.LNnLevel<1) oDoc.info.LNnLevel=1;
if(oDoc.info.LNnLevel>nDepth) oDoc.info.LNnLevel=nDepth;
}
LegalNoDlg.nLevel = (oDoc.info.LNnLevel ? oDoc.info.LNnLevel.toString() : nDepth.toString());
if("ok" == app.execDialog(LegalNoDlg)){
//console.println("Max " + LegalNoDlg.nLvlMax);
oDoc.info.LNnLevel=parseInt(LegalNoDlg.nLevel,10);
var n=FindNumBks(oDoc.bookmarkRoot.children,LegalNoDlg.nLevel)
//console.println("Num add " + n);
app.thermometer.duration=n;
app.thermometer.begin();
leg_num_thm_count=0;
AddLegalNum(oDoc.bookmarkRoot,0,oDoc, "", 0, LegalNoDlg.nLevel, n); //Add the legal numbering
app.thermometer.end();
oDoc.info.LegalNumExists=true;
}
}
}
oDoc.pageNum=Pg; //reset page number to where it started
// return nFound;
return 0;
app.endPriv();
});
//</CodeAbove>
var LegalNoDlg =
{
strTitle:"",
nLevel:"1",
nLvlMax:"1",
nPgMax:"2",
nInsertAt:"2",
initialize: function(dialog)
{
var dlgInit =
{
"nlvl": this.nLevel,
"Titl": this.strTitle,
};
dialog.load(dlgInit);
},
commit: function(dialog)
{
var oRslt = dialog.store();
this.strTitle = oRslt["Titl"];
this.nLevel = oRslt["nlvl"];
},
nlvl: function(dialog)
{
var oRslt = dialog.store();
if (oRslt.nlvl < 1)
dialog.load({nlvl:"1"});
else if (oRslt.nlvl > this.nLvlMax)
dialog.load({nlvl:this.nLvlMax});
},
description:
{
name: "Legal numbering tool",
elements:
[
{
type: "view",
elements:
[
{
type: "view",
alignment: "align_fill",
elements:
[
{
type: "view",
alignment: "align_fill",
align_children: "align_row",
elements:
[
{
type: "static_text",
item_id: "sta1",
name: "Number of bookmark levels to number:",
},
{
type: "edit_text",
item_id: "nlvl",
variable_Name: "nLevel",
char_width: 3,
SpinEdit: "true",
},
]
},
]
},
{
type: "ok_cancel",
},
]
},
]
}
};
//<JSCodeSnippet name="ImageData7">
var strData7LEGAL_NUM =
"000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000cf000000ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000bf000000ff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000ff000000000000000000000010000000af000000bf000000bf000000bf000000bf000000bf000000bf000000bf000000bf000000bf000000bf000000bf000000af000000100000000000000040000000ff000000000000000000000080000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff00000080000000000000002000000080000000000000000000000040000000ef000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ef0000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000040000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000df000000df000000ef00000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000020000000ff000000200000000000000000000000700000008000000080000000800000008000000080000000800000008000000080000000800000008000000080000000700000000000000010000000cf0000009f000000000000000000000070000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff00000070000000cf000000ef00000080000000200000000000000050000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff00000050000000800000008000000080000000200000000000000000000000300000004000000040000000400000004000000040000000400000004000000040000000400000004000000040000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cf000000ff000000ef00000010000000000000000000000030000000400000004000000040000000400000004000000040000000400000004000000040000000400000004000000030000000000000003000000050000000ff000000200000000000000060000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff0000006000000030000000bf000000ef000000200000000000000070000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000ff000000700000007000000050000000ff00000040000000000000000000000070000000800000008000000080000000800000008000000080000000800000008000000080000000800000008000000070000000000000009f000000bf000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
//</JSCodeSnippet>
// Icon Generic Stream Object
//<JSCodeSnippet name="ButtonIconDef">
var oIconLEGAL_NUM = null;
//if(app.viewerVersion < 7){
//}else{
oIconLEGAL_NUM = {count: 0, width: 20, height: 20,
read: function(nBytes){return strData7LEGAL_NUM.slice(this.count, this.count += nBytes);}};
//}
//</JSCodeSnippet>
//<JSCodeSnippet name="EventCode">
var DoCmdLEGAL_NUM =
"DoLEGAL_NUM(event.target);"
//</JSCodeSnippet>
//<JSCodeSnippet name="ButtonObjDef">
var oButObjLEGAL_NUM =
{cName: "LEGAL_NUM",
cExec: DoCmdLEGAL_NUM,
cEnable: "event.rc = (app.doc != null) && (app.doc.bookmarkRoot) && app.doc.bookmarkRoot.children && (app.doc.bookmarkRoot.children.length > 0)",
//cMarked: "event.rc = LegalNumPresent(app.doc.bookmarkRoot,0,app.doc)",
cMarked: "event.rc = (app.doc != null) && app.doc.info.LegalNumExists",
cTooltext: "Toggle legal numbering labels on Bookmarks",
cLabel: "Legal numbering",
nPos: 0};
//</JSCodeSnippet>
if(oIconLEGAL_NUM != null)
oButObjLEGAL_NUM.oIcon = oIconLEGAL_NUM;
try{app.removeToolButton("LEGAL_NUM");}catch(e){}
//<JSCodeSnippet name="TryAddBut">
try
{
//</JSCodeSnippet>
//<JSCodeSnippet name="AddButtonfn">
app.addToolButton(oButObjLEGAL_NUM);
//</JSCodeSnippet>
// if((event.type == "Doc") && (app.viewerVersion >= 7))
// global["LEGAL_NUM_InDoc"] = "1:17:2011:17:55:45";
//else
// global["LEGAL_NUM_InDoc"] = "1:17:2011:17:55:45";
//<JSCodeSnippet name="CatchAddBut">
}catch(e)
{
if((global.bReportNameCollision != null) && (global.bReportNameCollision == true))
{
var strError = 'Cannot Install AcroButton "oButObjLEGAL_NUM"\n';
strError += ':' + e.fileName + '\n';
strError += 'Error: ' + e.name + '\n';
strError += e.message + '\n';
strError += 'Possible Name conflict';
app.alert(strError,0,0,'AcroButton Error');
}
}
//</JSCodeSnippet>
//</AcroButtons>