-
Notifications
You must be signed in to change notification settings - Fork 0
/
No-fuss CHRONO.js
1717 lines (1488 loc) · 61.8 KB
/
No-fuss CHRONO.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//********************************************************************* //
//
// **** Make chronology in a text file from bookmarks
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // ///
/*
Gather the bookmarks with dates in their name, sort, and write to a file.
*/
var MAX_FILE_PATHS=10;
var filepaths=new Array ();
var ChronoBkMks=new Array(); //stores the bookmarks data
function clear_filepaths_array(){
while(filepaths.length > 0) filepaths.pop(); //clear the array
}
function clear_chrono_array(){
while(ChronoBkMks.length>0)ChronoBkMks.pop(); //clear the array
}
function clear_file_paths(oDoc){
oDoc.info.filepath1="";
oDoc.info.filepath2="";
oDoc.info.filepath3="";
oDoc.info.filepath4="";
oDoc.info.filepath5="";
oDoc.info.filepath6="";
oDoc.info.filepath7="";
oDoc.info.filepath8="";
oDoc.info.filepath9="";
oDoc.info.filepath10="";
}
function collect_file_paths(oDoc){
clear_filepaths_array();
if (typeof(oDoc.info.filepath1)!=undefined && oDoc.info.filepath1!="") {var fl={path: oDoc.info.filepath1, rect:null, nDepth:oDoc.info.nDepth1, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath2)!=undefined && oDoc.info.filepath2!="") {var fl={path: oDoc.info.filepath2, rect:null, nDepth:oDoc.info.nDepth2, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath3)!=undefined && oDoc.info.filepath3!="") {var fl={path: oDoc.info.filepath3, rect:null, nDepth:oDoc.info.nDepth3, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath4)!=undefined && oDoc.info.filepath4!="") {var fl={path: oDoc.info.filepath4, rect:null, nDepth:oDoc.info.nDepth4, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath5)!=undefined && oDoc.info.filepath5!="") {var fl={path: oDoc.info.filepath5, rect:null, nDepth:oDoc.info.nDepth5, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath6)!=undefined && oDoc.info.filepath6!="") {var fl={path: oDoc.info.filepath6, rect:null, nDepth:oDoc.info.nDepth6, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath7)!=undefined && oDoc.info.filepath7!="") {var fl={path: oDoc.info.filepath7, rect:null, nDepth:oDoc.info.nDepth7, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath8)!=undefined && oDoc.info.filepath8!="") {var fl={path: oDoc.info.filepath8, rect:null, nDepth:oDoc.info.nDepth8, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath9)!=undefined && oDoc.info.filepath9!="") {var fl={path: oDoc.info.filepath9, rect:null, nDepth:oDoc.info.nDepth9, MaxnDepth:1}; filepaths.push(fl);}
if (typeof(oDoc.info.filepath10)!=undefined && oDoc.info.filepath10!="") {var fl={path: oDoc.info.filepath10, rect:null, nDepth:oDoc.info.nDepth10, MaxnDepth:10}; filepaths.push(fl);}
}
function set_file_paths(oDoc){
var l=filepaths.length;
if (l>0) {oDoc.info.filepath1=filepaths[0].path; oDoc.info.nDepth1=filepaths[0].nDepth;}else{oDoc.info.filepath1=""; oDoc.info.nDepth1=""};
if (l>1) {oDoc.info.filepath2=filepaths[1].path; oDoc.info.nDepth2=filepaths[1].nDepth;}else{oDoc.info.filepath2=""; oDoc.info.nDepth2=""};
if (l>2) {oDoc.info.filepath3=filepaths[2].path; oDoc.info.nDepth3=filepaths[2].nDepth;}else{oDoc.info.filepath3=""; oDoc.info.nDepth3=""};
if (l>3) {oDoc.info.filepath4=filepaths[3].path; oDoc.info.nDepth4=filepaths[3].nDepth;}else{oDoc.info.filepath4=""; oDoc.info.nDepth4=""};
if (l>4) {oDoc.info.filepath5=filepaths[4].path; oDoc.info.nDepth5=filepaths[4].nDepth;}else{oDoc.info.filepath5=""; oDoc.info.nDepth5=""};
if (l>5) {oDoc.info.filepath6=filepaths[5].path; oDoc.info.nDepth6=filepaths[5].nDepth;}else{oDoc.info.filepath6=""; oDoc.info.nDepth6=""};
if (l>6) {oDoc.info.filepath7=filepaths[6].path; oDoc.info.nDepth7=filepaths[6].nDepth;}else{oDoc.info.filepath7=""; oDoc.info.nDepth7=""};
if (l>7) {oDoc.info.filepath8=filepaths[7].path; oDoc.info.nDepth8=filepaths[7].nDepth;}else{oDoc.info.filepath8=""; oDoc.info.nDepth8=""};
if (l>8) {oDoc.info.filepath9=filepaths[8].path; oDoc.info.nDepth9=filepaths[8].nDepth;}else{oDoc.info.filepath9=""; oDoc.info.nDepth9=""};
if (l>9) {oDoc.info.filepath10=filepaths[9].path; oDoc.info.nDepth10=filepaths[9].nDepth;}else{oDoc.info.filepath10=""; oDoc.info.nDepth10=""};
}
var No_FussMakeChronoMaker = app.trustedFunction(function(oDoc)
{
if(!CheckPermitted())return;
app.beginPriv();
if (ExistingCHRONO(oDoc)) {
//console.println("This place");
if(DeleteOldChrono(oDoc)) oDoc.info.CHRONOExists=false; //i.e. set flag to false if we successfully delete TOC
return;
}
var PgNow=oDoc.pageNum;
//dob_C=null;
SetKeyDates(oDoc);
var nDepth = FindMaxBkDepth(oDoc.bookmarkRoot);
if(nDepth > 0 || filepaths.length>0)
{
ChronoDlg.strTitle = (oDoc.info.CHRONOTitle ? oDoc.info.CHRONOTitle : "Chronology: " + oDoc.documentFileName.replace(/\.pdf$/i,""));
ChronoDlg.bDayofWeek= (typeof(oDoc.info.CHRONODayofWeek) != "undefined" ? oDoc.info.CHRONODayofWeek : true);
ChronoDlg.bSepFile= (typeof(oDoc.info.CHRONOSepFile) != "undefined" ? oDoc.info.CHRONOSepFile : true);
ChronoDlg.bAge= (typeof(oDoc.info.CHRONOAge) != "undefined" ? oDoc.info.CHRONOAge : true);
ChronoDlg.bAvReps= (typeof(oDoc.info.CHRONOAvoidRepeats) != "undefined" ? oDoc.info.CHRONOAvoidRepeats : true);
ChronoDlg.nLvlMax = nDepth.toString();
//Keep the persistent nLevel within bounds
if(oDoc.info.CHRONOnLevel){
if(oDoc.info.CHRONOnLevel<1) oDoc.info.CHRONOnLevel=1;
if(oDoc.info.CHRONOnLevel>nDepth) oDoc.info.CHRONOnLevel=nDepth;
}
ChronoDlg.nLevel = (oDoc.info.CHRONOnLevel ? oDoc.info.CHRONOnLevel.toString() : nDepth.toString());
ChronoDlg.nPgMax = oDoc.numPages.toString();
ChronoDlg.nInsertAt = "1";
if("ok" == app.execDialog(ChronoDlg)){
clear_chrono_array();
oDoc.info.CHRONOTitle=ChronoDlg.strTitle;
oDoc.info.CHRONOnLevel=parseInt(ChronoDlg.nLevel,10);
oDoc.info.CHRONODayofWeek=ChronoDlg.bDayofWeek;
oDoc.info.CHRONOSepFile=ChronoDlg.bSepFile;
oDoc.info.CHRONOAge=ChronoDlg.bAge;
oDoc.info.CHRONOAvoidRepeats=ChronoDlg.bAvReps;
if(ChronoDlg.bSepFile){ //create chronology in separate file
var rep=new Report();
var oCHRONODoc = rep.open("CHRONO") //turns the rpt into a doc
var chrono_path=oDoc.path.replace(/\/[^\/]+pdf$/,"/");
//Add special linking script
var LINKING_CODE='function NFPageOpenChrono(oDoc,f,p){try{var doc=app.openDoc(f, oDoc);doc.pageNum=p;}catch (e){app.alert("Unable to open " + f);}}';
oCHRONODoc.addScript("LinkingScriptChrono", LINKING_CODE);
oDoc.disclosed=true;
oCHRONODoc.disclosed=true;
oCHRONODoc.dirty=true; //to mark it for saving
//Set the new file's info
oCHRONODoc.info.CHRONOTitle=ChronoDlg.strTitle;
oCHRONODoc.info.CHRONOnLevel=parseInt(ChronoDlg.nLevel,10);
oCHRONODoc.info.CHRONODayofWeek=ChronoDlg.bDayofWeek;
oCHRONODoc.info.CHRONOAge=ChronoDlg.bAge;
oCHRONODoc.info.CHRONOAvoidRepeats=ChronoDlg.bAvReps;
oCHRONODoc.info.CHRONOSepFile=false;
oCHRONODoc.info.date_of_birth=oDoc.info.date_of_birth;
oCHRONODoc.info.date_of_injury=oDoc.info.date_of_injury;
clear_file_paths(oCHRONODoc);
//save the file
mySaveAs(oCHRONODoc,chrono_path,"Chronology.pdf");
oCHRONODoc.collect_file_paths(oCHRONODoc);
console.println("Got here chrono1");
oCHRONODoc.AddExternalFile(oDoc.path, oCHRONODoc); //add this file as the file to link to
console.println("Got here chrono2");
oCHRONODoc.CollectAllBookMarks(oCHRONODoc, ChronoDlg.nLevel);
//if(dob_C!=null)DoAge();
oCHRONODoc.ChronoBkMks.sort(compare);
oCHRONODoc.set_file_paths(oCHRONODoc);
PrintChrono(oCHRONODoc.ChronoBkMks, oDoc);
PrintChronoOPML(ChronoBkMks, oDoc);
oCHRONODoc.info.CHRONOExists=oCHRONODoc.WriteChronoReport(oCHRONODoc.ChronoBkMks, oCHRONODoc, ChronoDlg.strTitle, ChronoDlg.bDayofWeek, ChronoDlg.bAge); //set flag to true if we successfully add CHRONO
}else{
DoChronoThisFile(oDoc, nDepth);
}
//Clear the array
clear_chrono_array();
}
}
else {
app.alert("Cannot create chronology because there are no Bookmarks",1);
}
//oDoc.pageNum=PgNow;
app.endPriv();
});
function PrintChrono(ChronoBkMks, oDoc){
var rep=new Report();
var delimiter="-";/*String.fromCharCode(35)+String.fromCharCode(64);*/
var endofline="";
//Heading
/* rep.size=1.2;
rep.writeText("\""+ ChronoDlg.strTitle+ "\"" +endofline);
rep.divide();
rep.writeText(" ");
rep.writeText(" ");
rep.size=1.0;
*/
//Chronology
var Old_Date=new Date("10/10/1066"); //set the default to a very old date
var oldNm="";
for(var i=0;i<ChronoBkMks.length;i++){
//Make the date string
//console.println("Dates : " + Old_Date + ", " + ChronoBkMks[i].D);
var DateStr="";
if(ChronoBkMks[i].D){
if(Number(ChronoBkMks[i].D)!=Number(Old_Date)){ //if this is a new date then write it, else don't
DateStr=util.printd("dd/mm/yy", ChronoBkMks[i].D);
Old_Date=ChronoBkMks[i].D;
}else{
//DateStr=delimiter;
DateStr=util.printd("dd/mm/yy", ChronoBkMks[i].D);;
}
}
//Make the page reference string
ChronoBkMks[i].nPageLabel ? PageReference=/*(ChronoDlg.bPageLabeltrue)?*/ChronoBkMks[i].nPageLabel/*:(ChronoBkMks[i].nPage).toString()*/: PageReference="";
var nm="";
if(ChronoBkMks[i].name) nm=ChronoBkMks[i].name.replace(delimiter,"_");
// console.println(nm);
// console.println(oldNm);
if(nm!=oldNm || !ChronoDlg.bAvReps){ //only print if different from last one
//console.println("Printing this one " + nm);
//Concatenate
var text_string="\""+DateStr+"\""+ delimiter + "\"" + nm + "\"" + delimiter + "\""+ PageReference + "\""+endofline ;
//console.println(text_string);
//console.println(text_string);
rep.writeText(text_string);
}else{
//console.println("Not printing " + nm);
}
oldNm=nm; //make a note of this nm
}
var chrono_path=oDoc.path.replace(/\/[^\/]+pdf$/,"/");
//console.println("This path :" + chrono_path);
//rep.save(chrono_path);
var doc=rep.open("TEST");
mySaveAs(doc,chrono_path, "chronology.txt");
doc.closeDoc(true);
}
function PrintChronoOPML(ChronoBkMks, oDoc){
var rep=new Report();
var delimiter="-";/*String.fromCharCode(35)+String.fromCharCode(64);*/
var endofline="";
var rtn="\r\n"
var text_file="";
text_file+="<?xml version=\"1.0\"?>" + rtn;
text_file+="<opml version=\"2.0\">" + rtn;
text_file+="<head>" +rtn;
text_file+="<ownerEmail>" +rtn;
text_file+="[email protected]" +rtn;
text_file+="</ownerEmail>" +rtn;
text_file+="</head>" +rtn;
text_file+="<body>" +rtn;
text_file+="<outline text=\"Chrono\">" + rtn;
//Heading
rep.size=1.2;
rep.writeText("<?xml version=\"1.0\"?>" + endofline);
rep.writeText("<opml version=\"2.0\">" + endofline);
rep.writeText("<head>" +endofline);
rep.writeText("<ownerEmail>" +endofline);
rep.writeText("[email protected]" +endofline);
rep.writeText("</ownerEmail>" +endofline);
rep.writeText("</head>" +endofline);
rep.writeText("<body>" +endofline);
rep.writeText("<outline text=\"Chrono\">" + endofline);
// rep.divide();
// rep.writeText(" ");
// rep.writeText(" ");
// rep.size=1.0;
function findClosestColorRGB(r, g, b)
{
var rgb = {r:r, g:g, b:b};
var delta = 3 * 256 * 256;
var temp = {r:0, g:0, b:0};
var nameFound = 'c-black';
var ColourTable = [
{name:'c-black', hex: '#000000'},
{name:'c-gray', hex: '#808080'},
{name:'c-orange', hex: '#FFA500'},
{name:'c-red', hex: '#FF0000'},
{name:'c-purple', hex: '#800080'},
{name:'c-pink', hex: '#FF00FF'},
{name:'c-green', hex: '#008000'},
{name:'c-yellow', hex: '#FFFF00'},
{name:'c-blue', hex: '#0000FF'},
{name:'c-teal', hex: '#008080'},
{name:'c-sky', hex: '#00FFFF'}
];
function Hex2RGB(hex) {
// Remove the # character from the beginning of the hex code
hex = hex.replace("#", "");
// Convert the red, green, and blue components from hex to decimal
// you can substring instead of slice as well
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
// Return the RGB value as an object with properties r, g, and
return {r: r, g:g, b: b};
}
for(var i=0; i<ColourTable.length; i++)
{
temp = Hex2RGB(ColourTable[i].hex);
if(Math.pow(temp.r-rgb.r,2) + Math.pow(temp.g-rgb.g,2) + Math.pow(temp.b-rgb.b,2) < delta){
delta = Math.pow(temp.r-rgb.r,2) + Math.pow(temp.g-rgb.g,2) + Math.pow(temp.b-rgb.b,2);
nameFound = ColourTable[i].name;
}
}
return nameFound;
}
//Chronology
var Old_Date=new Date("10/10/1066"); //set the default to a very old date
var oldNm="";
for(var i=0;i<ChronoBkMks.length;i++){
//Make the date string
//console.println("Dates : " + Old_Date + ", " + ChronoBkMks[i].D);
var DateStr="";
if(ChronoBkMks[i].D){
if(Number(ChronoBkMks[i].D)!=Number(Old_Date)){ //if this is a new date then write it, else don't
DateStr=util.printd("dd/mm/yy", ChronoBkMks[i].D);
Old_Date=ChronoBkMks[i].D;
}else{
//DateStr=delimiter;
DateStr=util.printd("dd/mm/yy", ChronoBkMks[i].D);
}
}
//Make the page reference string
ChronoBkMks[i].nPageLabel ? PageReference=/*(ChronoDlg.bPageLabeltrue)?*/ChronoBkMks[i].nPageLabel/*:(ChronoBkMks[i].nPage).toString()*/: PageReference="";
var nm="";
if(ChronoBkMks[i].name) nm=ChronoBkMks[i].name.replace(delimiter,"_");
if(nm!=oldNm || !ChronoDlg.bAvReps){ //only print if different from last one
var clean_nm=nm.replace("<","<").replace(">",">").replace("&"," &").replace("'","'").replace("\"",""");
var note=null;
var m=clean_nm.match(/\{([^(^).]*)\}/g);
if (m) note=m.join("; ");
//Concatenate
var text_string="<outline "
text_string+="text=\""
// <outline text="<time startYear="1988" startMonth="4" startDay="19">Tue, Apr 19, 1988</time> <a href="http://www.google.co.uk">dob</a> C = 31/35" _note="as per w/s and medical records. Instructions (18/8/88) and Thomas report (18/4/88) and Shorvon (18/4/88) get it wrong. " />
if (ChronoBkMks[i].D) {
text_string += "<time startYear="" + ChronoBkMks[i].D.getFullYear().toString() + "" ";
text_string += "startMonth="" + (ChronoBkMks[i].D.getMonth()+1).toString() + "" ";
text_string += "startDay="" + (ChronoBkMks[i].D.getDate()).toString() + "" ";
text_string += "startHour="" +(ChronoBkMks[i].D.getHours()) + "" ";
text_string += "startMinute="" +(ChronoBkMks[i].D.getMinutes()) + "" ";
// text_string += "startSecond="" +(ChronoBkMks[i].D.getSeconds()) + "" ";
text_string += ">" + ChronoBkMks[i].D.toLocaleString('default', {month: 'long'});
text_string += "</time>"
var this_color=findClosestColorRGB(ChronoBkMks[i].color[1]*256,ChronoBkMks[i].color[2]*256,ChronoBkMks[i].color[3]*256);
if (this_color!="c-black"){
text_string += "<span class="colored " + this_color + "">"
text_string += " " + clean_nm+ " [" + PageReference + "]</span>"
}else {
text_string += " " + clean_nm + " [" + PageReference + "]";
}
text_string += "\""
if(note){
text_string +=" _note=\"" + note + "\"";
}
text_string += "/>";
}
rep.writeText(text_string +rtn);
text_file+=text_string + rtn;
}else{
//console.println("Not printing " + nm);
}
oldNm=nm; //make a note of this nm
}
rep.writeText("</outline>" +endofline);
rep.writeText("</body>" +endofline);
rep.writeText("</opml>" +endofline);
text_file+="</outline>" +rtn;
text_file+="</body>" +rtn;
text_file+="</opml>" +rtn;
var chrono_path=oDoc.path.replace(/\/[^\/]+pdf$/,"/");
//console.println("This path :" + chrono_path);
//rep.save(chrono_path);
// var doc=rep.open("TEST");
// mySaveAs(doc,chrono_path, "chronology opml.txt");
this.createDataObject("chronology opml.txt", text_file);
this.exportDataObject({cName:"chronology opml.txt", nLaunch: 1});
this.removeDataObject("chronology opml.txt");
// doc.closeDoc(true);
}
var mySaveAs = app.trustedFunction(
function(oDoc,cPath,cFlName)
{
app.beginPriv();
// Ensure path has trailing "/"
cPath = cPath.replace(/([^/])$/, "$1/");
cPath=cPath.replace("'", "\'"); //escape the apostrophe
cFlName=cFlName.replace("'", "\'"); //escape the apostrophe
//console.println(cPath+cFlName);
//Check if this is a .txt or .pdf file
var pattern_txt=/.txt$/i;
var pattern_pdf=/.pdf$/i;
try{
//console.println(cPath +cFlName);
if(pattern_txt.test(cFlName)) oDoc.saveAs(cPath + cFlName, "com.adobe.acrobat.accesstext");
if(pattern_pdf.test(cFlName)) oDoc.saveAs(cPath + cFlName);
}catch(e){
app.alert("Error During Save");
return false;
}
return true;
app.endPriv();
}
);
function WriteChronoReport (ChronoBkMks, oDoc, title, DayofWeek, bAge) {
app.beginPriv();
var delimiter="-";/*String.fromCharCode(35)+String.fromCharCode(64);*/
var endofline="";
// var title="Chronology"
var ind; //how far to indent
var sw;
var heading;
if (!DayofWeek && !bAge)sw=1;
if (DayofWeek && !bAge)sw=2;
if (DayofWeek && bAge)sw=3;
if (!DayofWeek && bAge)sw=4;
switch(sw){
case 1: //just the date
ind=34;
heading="";
break;
case 2: //the date and the day of week
ind=47;
heading=" Dy";
break;
case 3: //the date, day of week and age
ind=80;
heading=" Dy Age";
break;
case 4: //the date and age
ind=60;
heading=" Age";
break;
default:
console.println("Shouldn't get here, chrono line 274");
break;
}
var pgRect=[];
pgRect[0]=0;
pgRect[1]=841.9199829101562;
pgRect[2]=595.3200073242188;
pgRect[3]=0;
var pgWidth = pgRect[2] - pgRect[0];
var pgHeight = pgRect[1] - pgRect[3];
nPgWdth=pgWidth-72;
oDoc.newPage(oDoc.numPages,pgWidth, pgHeight); //add a page of fixed size to the end for comparison
var rpt=new Report(pgRect);
var nNmLen,nFill,cTxt,aLnRct, nPage, nLastPos;
nPage=0;
nLastPos=pgHeight;
var nRptSize = 11/11;
rpt.size = nRptSize;
// Scale report text size to measuring size (Helvetica replacment)
var nRplcSize = nRptSize*nHelvReplacementScale;
var nScaledFiller = nRptSize * nFillerWidth;
//Do the title
rpt.size=2.0;
var title_rect=rpt.writeText("+ " + title);
rpt.divide();
//rpt.size=1.0;
//rpt.writeText(" ");
//Do the filepaths
rpt.size=0.7;
for (var i=0; i<filepaths.length;i++) {filepaths[i].rect=rpt.writeText("- " + filepaths[i].path); rpt.writeText(" ");}
rpt.size=1.0;
rpt.writeText(" ");
rpt.color=color.blue;
rpt.writeText(heading);
rpt.size=0.7;
rpt.writeText(" ");
rpt.size=1.0;
rpt.color=color.black;
//Do the chronology
var Old_Date=new Date("10/10/1066"); //set the default to a very old date
var aBkmkData=[];
rpt.indent(ind); //indent to allow for date
var oTxt="";
for(i=0;i<ChronoBkMks.length;i++){
var Fnt=GetFont(ChronoBkMks[i].sty);
//Make the date string
//console.println("Dates : " + Old_Date + ", " + ChronoBkMks[i].D);
var DateStr="";
if(oDoc.info.date_of_birth!=""){ //do the age calculation if dob_C not null
var mom1=moment(ChronoBkMks[i].D).startOf('day');
var mom2=moment(dob_C).startOf('day');
//console.println("Mom1 " + mom1);
//console.println("Mom2 " + mom2);
//ChronoBkMks[i].age=moment.duration(ChronoBkMks[i].D-dob_C).years();
ChronoBkMks[i].age=parseInt(mom1.diff(mom2, 'years', true),10);
//console.println("Age " + ChronoBkMks[i].age);
}
Same_Date=SameDate(ChronoBkMks[i].D,Old_Date); //set flag
if(Same_Date){ //if this is a new date then write it, else don't
DateStr=" ";
//DateStr=util.printd("dd/mm/yy", ChronoBkMks[i].D);;
}else{
if(ChronoBkMks[i].D){
DateStr=util.printd("dd/mm/yy", ChronoBkMks[i].D);
Old_Date=ChronoBkMks[i].D;
}
}
//Make the page reference string
ChronoBkMks[i].nPageLabel ? PageReference=/*(ChronoDlg.bPageLabeltrue)?*/ChronoBkMks[i].nPageLabel/*:(ChronoBkMks[i].nPage).toString()*/: PageReference="";
//Concatenate
//var text_string=DateStr+ delimiter + ChronoBkMks[i].name+ "\"" + delimiter + "\""+ PageReference + "\""+endofline ;
// var cTxt=DateStr + " " + ChronoBkMks[i].name;
var cTxt= ChronoBkMks[i].name ? ChronoBkMks[i].name: "";
cTxt!=oTxt ? ChronoBkMks[i].inc=true: ChronoBkMks[i].inc=false;
oTxt=cTxt;
if(ChronoBkMks[i].inc || !ChronoDlg.bAvReps){
nNmLen = getTextWidth(oDoc, oDoc.numPages - 1, nRplcSize, Fnt, false, cTxt);
nFill = (nPgWdth - nNmLen - ind - 10) / nScaledFiller;
for (var n = 0; n < nFill; n++)
cTxt += " .";
rpt.size = nRptSize;
//console.println(cTxt);
rpt.color = ChronoBkMks[i].color;
rpt.style = "DefaultNoteText";
if (ChronoBkMks[i].sty == 2) rpt.style = "NoteTitle";
aLnRct = rpt.writeText(cTxt);
rpt.style = "DefaultNoteText";
rpt.size = nRptSize;
// Detect Page Change
if ((aLnRct[3] > nLastPos) || (aLnRct[3] == aLnRct[1]))
nPage++;
// Correct for missed line
if (aLnRct[3] == aLnRct[1]) {
aLnRct[3] = pgHeight - 36;
aLnRct[1] = pgHeight - 36 - 11;
}
nLastPos = aLnRct[3];
rpt.size = nRptSize / 2;
aSpRct = rpt.writeText(" "); //create a small line of spaces
ChronoBkMks[i].chrono_pg = nPage;
ChronoBkMks[i].rect = aLnRct;
}
//aBkmkData.push({cName:ChronoBkMks[i].name,rect:aLnRct, nLevel:nLvl, nBkPg:arguments.callee.nPage, nTextSize:aSizes[nLvl-1]});
}
//delete the last page we used for comparison
oDoc.deletePages(oDoc.numPages-1);
//PUT THE PAGES INTO THE MAIN BUNDLE. WE STILL NEED TO ADD THE PAGE REFERENCES
var oCHRONODoc = rpt.open("CHRONO") //turns the rpt into a doc
var nCHRONOPages = oCHRONODoc.numPages;
var chrono_path=oDoc.path.replace(/\/[^\/]+pdf$/,"/");
var nStartPage=-1;
if (ExistingTOC(oDoc)){ //Slot the contents in the right place: Front Sheet, Subs, Contents, Chrono
nStartPage=GetPageNumberLastTOC(oDoc)+1;
//console.println("Page number for inserting chrono is: " + nStartPage);
}else{
if(ExistingSUBS(oDoc)){
nStartPage=GetPageNumberLastSubs(oDoc);
}else{
if(ExistingFS(oDoc)){
nStartPage=1;
}else{
nStartPage=0;
}
}
}
if (nStartPage<0) {
console.println("Error in setting page for placing CHRONO.");
nStartPage=0; //set to default
}
var NumPages=oDoc.numPages;
var start_page_at_end=0;
var NewNumPages=oDoc.numPages;
start_page_at_end=NumPages;
//place pages at the end
oDoc.insertPages ({nPage: NumPages-1, cPath: oCHRONODoc.path,});
NewNumPages=oDoc.numPages;
//place dummy copy at the end - this stops re-page labelling everything else
oDoc.insertPages ({nPage: oDoc.numPages-1,cPath: oCHRONODoc.path,});
oDoc.setPageLabels(NumPages, ["r", "CHRONO_", 1]); //set the page labels
////////
var fldRect; //for the page label
var fldRectDate; //for the date
var fldRectDay; //for the day
// First Find longest Page Label
var nMaxWdth=0, bxWdth, bxRot, oSpn;
// var mxRot = (new Matrix2D()).fromRotated(this,0).invert();
var mxRot = (new Matrix2D()).fromRotated(oDoc,0).invert();
var oTstAnt = oDoc.addAnnot({page:0, type:"Line", points:[[100,200],[110,200]],
doCaption:true,rotate:oDoc.getPageRotation(0)});
for(var i=0;i<ChronoBkMks.length;i++) {
if(typeof(ChronoBkMks[i].nPage) != "undefined"){
var bPageLb=true;
oSpn = [{textSize:11,fontFamily:Fnt}]
oSpn[0].text = ChronoBkMks[i].nPageLabel;
oTstAnt.richContents = oSpn;
// Correct for page Rotation
var bxRot = mxRot.transform(oTstAnt.rect);
bxWdth = Math.abs(bxRot[2] - bxRot[0]);
if(bxWdth > nMaxWdth) {
nMaxWdth = bxWdth;
}
if(nMaxWdth > 54) nMaxWdth = 54;
}
}
oTstAnt.destroy();
//ADD THE PAGE & DATE REFERENCE
nMaxWdth += 5;
Old_Date=new Date("10/10/1066"); //set the default to a very old date
for(var i=0;i<ChronoBkMks.length;i++) {
if (ChronoBkMks[i].inc || !ChronoDlg.bAvReps) {
var Fnt = GetFontForFields(ChronoBkMks[i].sty);
var ind = 0;
//Page label
fldRect = [pgRect[2] - 92, ChronoBkMks[i].rect[3], pgRect[2] - 92 + nMaxWdth + 25, ChronoBkMks[i].rect[1]];
oFld = oDoc.addField("CHRONOPage" + i, "text", start_page_at_end + ChronoBkMks[i].chrono_pg, fldRect);
oFld.fillColor = color.white
oFld.width = 0;
oFld.alignment = "right";
oFld.textSize = 10; //make smaller for windows
oFld.textFont = Fnt;
if (ChronoBkMks[i].color) oFld.textColor = ChronoBkMks[i].color;
if (ChronoBkMks[i].nPageLabel) oFld.value = ChronoBkMks[i].nPageLabel;
//Date & Day & Age
Same_Date = SameDate(ChronoBkMks[i].D, Old_Date); //set flag
if (Same_Date == false) { //if this is a new date then write it, else don't
//check to see if any of the entries for this date are stylised
//console.println("Big style for " + ChronoBkMks[i].name +": " + BiggestStyleInBlock(i));
Fnt = GetFontForFields(BiggestStyleInBlock(i));
//Date
fldRectDate = [pgRect[0] + 10, ChronoBkMks[i].rect[3], pgRect[0] + 10 + 60, ChronoBkMks[i].rect[1]];
oFldDt = oDoc.addField("CHRONODate" + i, "text", start_page_at_end + ChronoBkMks[i].chrono_pg, fldRectDate);
oFldDt.fillColor = color.white;
oFldDt.width = 0;
oFldDt.alignment = "left";
oFldDt.textSize = 10; //make smaller for windows
oFldDt.textFont = Fnt;
if (ChronoBkMks[i].color) oFldDt.textColor = ChronoBkMks[i].color;
var DateStr = ChronoBkMks[i].DTxt;
oFldDt.value = DateStr;
ind = ind + 58;
if (DayofWeek) {
//Day
fldRectDay = [pgRect[0] + ind, ChronoBkMks[i].rect[3], pgRect[0] + ind + 20, ChronoBkMks[i].rect[1]];
oFldDay = oDoc.addField("CHRONODay" + i, "text", start_page_at_end + ChronoBkMks[i].chrono_pg, fldRectDay);
oFldDay.fillColor = color.white;
oFldDay.width = 0;
oFldDay.alignment = "left";
oFldDay.textSize = 10; //make smaller for windows
oFldDay.textFont = Fnt;
if (ChronoBkMks[i].color) oFldDay.textColor = ChronoBkMks[i].color;
//var DayStr=GetDayStr(ChronoBkMks[i].D);
oFldDay.value = ChronoBkMks[i].DyStr;
ind = ind + 18;
}
if (bAge) {
//Age
fldRectDay = [pgRect[0] + ind, ChronoBkMks[i].rect[3], pgRect[0] + ind + 30, ChronoBkMks[i].rect[1]];
oFldDay = oDoc.addField("CHRONOAge" + i, "text", start_page_at_end + ChronoBkMks[i].chrono_pg, fldRectDay);
oFldDay.fillColor = color.white;
oFldDay.width = 0;
oFldDay.alignment = "center";
oFldDay.textSize = 10; //make smaller for windows
oFldDay.textFont = Fnt;
if (ChronoBkMks[i].color) oFldDay.textColor = ChronoBkMks[i].color;
//var DayStr=GetDayStr(ChronoBkMks[i].D);
//if(ChronoBkMks[i].age!=null) oFldDay.value = ChronoBkMks[i].age;
var age = Duration(ChronoBkMks[i].D, dob_C, 0, false);
if (age) oFldDay.value = age;
}
Old_Date = ChronoBkMks[i].D;
}
ChronoBkMks[i].nRightExtent = pgRect[2] - 72 + nMaxWdth;
}
}
oDoc.flattenPages(start_page_at_end,start_page_at_end + nCHRONOPages - 1);
//add an invisible field to the page to identify it as CHRONO
var inch=72;
var nTxtSize=20;
var wdth=getTextWidth(oDoc,0,nTxtSize,Fnt,false,"CHRONO_")
Fnt=GetFontForFields(0);
for (i=start_page_at_end;i<oCHRONODoc.numPages+start_page_at_end;i++){
var aRect = oDoc.getPageBox( {nPage: i} );
aRect[0] = aRect[2]-wdth-0.5*inch; // from upper left hand corner of page, upper left x
aRect[2] = aRect[0]+wdth; // Make it wdth wide, lower right x
aRect[1] = aRect[3]+.5*inch; // upper left y
aRect[3] = aRect[1] - 24; // and 24 points high, lower right y
oFld = oDoc.addField("CHRONO_"+i,"text", i, aRect);
oFld.fillColor = color.transparent;
oFld.width = 0;
oFld.alignment = "right";
oFld.textSize = nTxtSize;
oFld.textFont = Fnt;
oFld.readonly=true;
oFld.value = "CHRONO_";
oFld.display=display.hidden;
}
oCHRONODoc.closeDoc(true);
oDoc.bringToFront();
// Add Links over Text
var lnkRect,oLnk;
for(var i=0;i<ChronoBkMks.length;i++){
if(ChronoBkMks[i].inc || !ChronoDlg.bAvReps) {
if (typeof (ChronoBkMks[i].nRightExtent) != "undefined" || ChronoBkMks[i].rect) {
lnkRect = [ChronoBkMks[i].rect[0], ChronoBkMks[i].rect[1], ChronoBkMks[i].nRightExtent, ChronoBkMks[i].rect[3]];
oLnk = oDoc.addLink(start_page_at_end + ChronoBkMks[i].chrono_pg, lnkRect);
oLnk.borderWidth = 0;
var p = ChronoBkMks[i].nPage + nCHRONOPages;
var p2 = ChronoBkMks[i].nPage; //don't have to worry about additional chrono pages if this is external file
var code = "";
var q = String.fromCharCode(34);
if (ChronoBkMks[i].fpath != "") {
//code="NFPageOpenChrono_(this,"+rpath + ", "+p+");";
// oLnk.setAction(code);
var rpath = ChronoBkMks[i].fpath;
oLnk.setAction("NFPageOpenChrono(this,"+ "\"" + rpath + "\"," + p2 + ")");
} else {
oLnk.setAction("this.pageNum =" + p + ";");
}
}
}
}
//Add special link to the title
var oTitleLnk;
var new_title_rect=title_rect;
new_title_rect[2]=new_title_rect[0]+getTextWidth(oDoc,0,22,Fnt,false,"+")
oTitleLnk = oDoc.addLink(start_page_at_end, new_title_rect);
oTitleLnk.borderWidth = 0;
var act_str="var f=GetFilePath(app.doc); if(AddExternalFile(f, app.doc)) RefreshCHRONO(app.doc);";
oTitleLnk.setAction(act_str);
//Add special links and depthbox to the external files
for (var i=0; i<filepaths.length;i++){
//the link to external file
var oPathLnk;
var new_f_rect=filepaths[i].rect;
new_f_rect[2]=new_f_rect[0]+getTextWidth(oDoc,0,7,Fnt,false,"-")
oPathLnk = oDoc.addLink(start_page_at_end, new_f_rect);
oPathLnk.borderWidth = 0;
var act_str="DeleteExternalFile("+i.toString()+", app.doc); RefreshCHRONO(app.doc);";
oPathLnk.setAction(act_str);
//the depthbox
fldRect = filepaths[i].rect;
fldRect[2]=filepaths[i].rect[0]+getTextWidth(oDoc,0,7,Fnt,false,"-"+filepaths[i].path+"10")+20;
fldRect[0]=filepaths[i].rect[0]+getTextWidth(oDoc,0,7,Fnt,false,"-"+filepaths[i].path)+10;
oFld = oDoc.addField("ndepth"+i,"combobox", start_page_at_end, fldRect);
//load array with numbers for filling combobox
var a=new Array();
for (var x=0;x<=filepaths[i].MaxnDepth;x++) a.push(x);
if(a.length>0)oFld.setItems(a);
oFld.setAction("Keystroke","DepthFieldChange();");
oFld.fillColor = color.white;
oFld.width = 0;
oFld.textSize = 6; //make smaller for windows
oFld.textFont = Fnt;
oFld.textColor=["RGB",0,0,1 ];
//console.println("ndepth: " + filepaths[i].nDepth);
if(filepaths[i].nDepth>0) oFld.value = parseInt(filepaths[i].nDepth,10);
}
//Now delete the dummy pages
for(var i=0;i<nCHRONOPages;i++){
oDoc.deletePages(oDoc.numPages-1);
}
//Now move them to the right place
for (var i =1;i<=NewNumPages-NumPages;i++){
oDoc.movePage(NewNumPages-1,nStartPage-1);
}
//console.println("Start page " + nStartPage);
oDoc.pageNum=nStartPage;
app.endPriv();
return true;
}
function BiggestStyleInBlock(i){
//returns the highest style no in the block
var j=i+1;
var current_date=ChronoBkMks[i].D;
var current_sty=ChronoBkMks[i].sty;
do{
if(j>=ChronoBkMks.length-1) break;
if(ChronoBkMks[j].sty>current_sty)current_sty=ChronoBkMks[j].sty;
j++;
}while (SameDate(current_date, ChronoBkMks[j].D));
return current_sty;
}
function DepthFieldChange(){
if(!event.willCommitt && (event.changeEx!="")){
filepaths[parseInt(event.target.name.replace(/[^0-9\.]/g, ''), 10)].nDepth=event.changeEx;
}
set_file_paths(this);
//RefreshCHRONO(this);
}
var NFPageOpenChrono_=app.trustedFunction(function(oDoc,f, p){
app.beginPriv();
try{
var doc=app.openDoc(f, oDoc);
doc.disclosed=true;
doc.pageNum=p;
}catch (e){
app.alert("Error " + e);
}
app.endPriv();
});
function DeleteExternalFile(i, oDoc){
if(i<0 || i>filepaths.length-1) return false; //check in line
if(i<filepaths.length-1){
for (var j=i;j<filepaths.length-1;j++){
filepaths[j].path=filepaths[j+1].path; //shift the path down one
filepaths[j].nDepth=filepaths[j+1].nDepth;
filepaths[j].MaxnDepth=filepaths[j+1].MaxnDepth;
}
}
filepaths.pop(); //delete the last one
set_file_paths(oDoc);
return true;
}
var AddExternalFile=app.trustedFunction(function(f, oDoc){
app.beginPriv()
if(f=="")return false;
if(filepaths.length>=MAX_FILE_PATHS){
app.alert("You have reached maximum number of external files");
return false;
}
var fl={path: GetRelativePath(oDoc,f), rect: null, nDepth:"", MaxnDepth:1};
filepaths.push(fl);
set_file_paths(oDoc);
app.endPriv();
return true;
});
function ExistingCHRONO(oDoc){
//Function returns true if existing TOC
var pattern=/CHRONO_/;
var i=0;
while(i<oDoc.numFields+1){
var a=oDoc.getNthFieldName(i);
if(pattern.test(a)){
return true;
}
i++;
}
return false;
}
function DeleteOldChrono(oDoc){
console.println("Deleting Chrono...");
//Delete old Chronology
app.beginPriv();
var d=IdentifyPagesForDeletion(oDoc, /CHRONO_/);
DeletePages(oDoc, d);
clear_chrono_array();
//while(filepaths.length > 0) filepaths.pop();
app.endPriv();
return true;
}
var DoChronoThisFile=app.trustedFunction(function(oDoc, nDepth){
oDoc.info.CHRONOExists=false; //set flag to false if we delete TOC
collect_file_paths(oDoc);
//Keep the persistent nLevel within bounds
if(oDoc.info.CHRONOnLevel){
if(oDoc.info.CHRONOnLevel<1) oDoc.info.CHRONOnLevel=1;
if(oDoc.info.CHRONOnLevel>nDepth) oDoc.info.CHRONOnLevel=nDepth;
}
clear_chrono_array();
SetKeyDates(oDoc);
CollectAllBookMarks(oDoc, oDoc.info.CHRONOnLevel);
//console.println("Dob_C " + dob_C);
ChronoBkMks.sort(compare);
set_file_paths(oDoc);
PrintChrono(ChronoBkMks, oDoc);
PrintChronoOPML(ChronoBkMks, oDoc);
oDoc.info.CHRONOExists=WriteChronoReport(ChronoBkMks,oDoc, oDoc.info.CHRONOTitle, oDoc.info.CHRONODayofWeek, oDoc.info.CHRONOAge); //set flag if we successfully add TOC
//Clear the array
clear_chrono_array();
});
var RefreshCHRONO = app.trustedFunction (function(oDoc){
app.beginPriv();
var plat=app.platform;
// //console.println("Console: " + plat);
//Updates the CHRONO if it exists
if (ExistingCHRONO(oDoc)){
var nDepth = FindMaxBkDepth(oDoc.bookmarkRoot);
if(DeleteOldChrono(oDoc)) {
oDoc.info.CHRONOExists=false; //set flag to false if we delete TOC
DoChronoThisFile(oDoc,nDepth);
}
}
app.endPriv();
});
function IsPDFAlreadyOpen(file_path){
//returns true if the file_path is open