This repository was archived by the owner on Jan 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtl.js
1984 lines (1874 loc) · 73.1 KB
/
tl.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
/*!
* CSS Template Layout
* Copyright (c) 2011 Pablo Escalada
* MIT Licensed
*/
(function (global) {
var log = wef.logger("templateLayout"),
templateLayout,
buffer = {},
tom,
parser;
/**
* Create the prototype main class
*
* @param {string|strings[]}[templateSource] template source.
* Supports 0..* strings containing valid CSS text or URL.
* </p>
* Empty constructor searches parent HTML file for STYLE tags and uses its
* CSS content as template source.
* </p>
* String params are analyzed and loaded in this way:
* <ul>
* <li>"htttp[s]://..." entries are loaded as files and content extracted</li>
* <li>"file://..." entries are loaded as files and content extracted</li>
* <li>Unmatched entries are loaded as CSS text</li>
* </ul>
* Multiple strings are first analyzed and then concatenated
*
* @class TemplateLayout is a CSS Template Layout prototype that implements
* some basic features defined in W3C working draft "Template Layout Module".
* Features:
* <ul>
* <li>basic template definition: letters, . (dot) and @</li>
* <li>column width in pixels and %</li>
* <li>row height imn pixels and %</li>
* </ul>
*/
templateLayout = function (templateSource) {
log.info("create templateLayout...");
return new templateLayout.prototype.init(arguments);
};
templateLayout.prototype = {
constructor:templateLayout,
/**
* Version number
*/
version:"1.0.0",
/**
* Template sources store
*/
templateSources:[],
/**
* Constant object that stores CSS properties names used as triggers
* </p>
* Currently used:
* <ul>
* <li>constants.DISPLAY = "display"</li>
* <li>constants.POSITION = "position"</li>
* </ul>
*/
constants:{
DISPLAY:"display",
POSITION:"position"
},
/**
* Template compiler
*/
compiler:null,
/**
* Template output generator
*/
generator:null,
/**
* @ignore
* see templateLayout constructor
*/
init:function (templateSources) {
var args, firstSource, internalSources = [];
log.debug("sources:", templateSources);
log.debug("init subsystems...");
parser = wef.cssParser();
log.debug("subsystems... [OK]");
args = Array.prototype.slice.call(templateSources);
firstSource = args[0];
//templateLayout()
if (!firstSource) {
log.info("no external template loaded!!!");
Array.prototype.forEach.call(document.styleSheets, function (sheet) {
if (sheet.href !== null) {
//load external CSS
log.info("load external CSS", sheet.href);
internalSources.push(sheet.href);
}
else {
var text = sheet.ownerNode.innerHTML;
log.info("load style tag", text);
internalSources.push(text);
}
});
this.templateSources = internalSources.map(getContent);
log.info("templateLayout... [OK]");
return this;
}
//templateLayout("aString") and templateLayout("aString", "anotherString", ...)
if (args.length >= 1 && args.every(function (element) {
return typeof element == "string";
})) {
this.templateSources = args.map(getContent);
log.info("templateLayout... [OK]");
return this;
}
log.error("Invalid argument");
throw new Error("Invalid argument");
},
/**
* Reads, compiles and generates the template
*
* @param {string}[options=all] Only for testing purposes.
* Supported values [none|parse|compile]
* </p>
* Stops transform process at different points:
* <ul>
* <li>none: transform does nothing</li>
* <li>parse: transform only parses template source</li>
* <li>compile: transform parses source and compiles the template</li>
* </ul>
*/
transform:function () {
log.debug("transform...");
var options = parseTransformOptions(arguments);
if (options.parse) {
log.info("Step 1: parse");
log.group();
parser.whenStart(this.parserStarts);
parser.whenProperty(this.propertyFound);
parser.whenStop(this.parserDone);
parser.parse(this.templateSources.reduce(function (previous, source) {
return previous + source.sourceText;
}, ""));
log.groupEnd();
log.info("Step 1: parse... [OK]");
}
if (options.compile) {
log.info("Step 2: compile");
log.group();
tom = this.compiler().compile(buffer);
// log.info("TOM: ", tom);
log.groupEnd();
log.info("Step 2: compile... [OK]");
}
if (options.generate) {
log.info("Step 3: generate");
log.group();
this.generator(tom).patchDOM();
log.groupEnd();
log.info("Step 3: generate... [OK]");
}
log.info("transform... [OK]");
return this;
},
/**
* Returns the info from the parsing step
* @returns {ParserBufferEntry[]}buffer
*/
getBuffer:function () {
return buffer;
},
/**
* Returns TOM (Template Object Model)
* @returns {rootTemplate}tom
*/
getTOM:function () {
return tom;
},
/**
* "Parser start" callback. Prints start time and resets buffer
*
* @param o Information sent by parser
* @param o.time Start time in milliseconds
*/
parserStarts:function (o) {
log.info("start parsing at", new Date(o.time).toLocaleTimeString());
buffer = {};
},
/**
* "Property has been found" callback. Stores in buffer valid properties
*
* @param {CSSParserProperty}property found property information
*/
propertyFound:function (property) {
log.info("templateLayout listens: property found");
if (templateLayout.fn.isSupportedProperty(property)) {
store(property);
}
},
/**
* "Parser stop" callback. Prints stop time
* @param {StopCallbackData}o Information sent by parser
*/
parserDone:function (o) {
log.info("parsing done at", new Date(o.time).toLocaleTimeString());
},
/**
* Checks if given property is a valid one.
* If property name exists in constants then is a valid one
*
* @param {CSSParserProperty}property the property
* @returns {boolean}true if exists constants[???] == property.declaration.property
*/
isSupportedProperty:function (property) {
var iterator;
for (iterator in templateLayout.fn.constants) {
if (templateLayout.fn.constants.hasOwnProperty(iterator)) {
if (templateLayout.fn.constants[iterator] == property.declaration.property) {
log.info("supported property found: ", property.declaration.property);
return true;
}
}
}
return false;
}
};
templateLayout.fn = templateLayout.prototype;
templateLayout.fn.init.prototype = templateLayout.fn;
function getSourceType(templateSource) {
var rxHttp = /^http[s]?:\/\/.*\.css$/i, rxFile = /^file:\/\/.*\.css$/i, rxPath = /^(?!\s*.*(http[s]?|file))(\.){0,2}(\/.*)*.*\.css$/i;
if (rxHttp.exec(templateSource)) {
return "http";
}
if (rxPath.exec(templateSource) || rxFile.exec(templateSource)) {
return "file";
}
return "css";
}
function getContent(templateSource) {
var type = getSourceType(templateSource);
if (type == "http" || type == "file") {
return {
type:type,
sourceText:readFile(templateSource)
};
}
if (type == "css") {
return {
type:type,
sourceText:templateSource
};
} else {
throw new Error("unknown sourceType");
}
}
function parseTransformOptions(args) {
var options = {parse:true, compile:true, generate:true};
if (args.length === 0) {
return options;
}
if (args[0].action == "none") {
options.parse = options.compile = options.generate = false;
}
if (args[0].action == "parse") {
options.compile = options.generate = false;
}
if (args[0].action == "compile") {
options.generate = false;
}
return options;
}
function readFile(url) {
var templateText="";
try {
log.info("reading file...");
wef.net.ajax(url, {
asynchronous:false,
success:function (request) {
templateText = request.responseText;
}
});
log.info("template loaded... [OK]");
return templateText;
} catch (e) {
log.error("Operation not supported", e);
throw new Error("Operation not supported", e);
}
}
function store(rule) {
if (!buffer[rule.selectorText]) {
buffer[rule.selectorText] =
/**
* @namespace Data format of parser buffer entry
* @name ParserBufferEntry
*/
/**
* @lends ParserBufferEntry#
*/
{
/**
* property selector text
* @type string
*/
selectorText:rule.selectorText,
/**
* array of declarations (property_name:property_value)
* @type string[]
*/
declaration:{}
};
}
buffer[rule.selectorText].declaration[rule.declaration.property] = rule.declaration.valueText;
log.info("property stored: ", rule.declaration.property);
}
global.templateLayout = templateLayout;
})(window);/*!
* templateLayout.compiler
* Copyright (c) 2011 Pablo Escalada
* MIT Licensed
*/
(function (templateLayout) {
var compiler, log, rootTemplate;
log = wef.logger("templateLayout.compiler");
log.info("load compiler module");
function parseDisplay(displayValue) {
/*
* Name: ‘display’
* New value: <display-type>? && [ [ <string> [ / <row-height> ]? ]+ ] <col-width>*
* Percentages: N/A
* Computed value: specified value
*
* The <display-type> is one of the following keywords. An omitted keyword is equivalent to ‘block’.
* <display-type> = inline | block | list-item | inline-block | table | inline-table
* | table-row-group | table-header-group | table-footer-group | table-row
* | table-column-group | table-column | table-cell | table-caption | none
*
* Each <string> consist of one or more at signs (“@”), letters (or digits, see <letter> below),
* periods (“.”) and spaces
*
* Each <row-height> sets the height of the preceding row. The default is ‘auto’.
* The values can be as follows:
* <length> An explicit height for that row. Negative values make the template illegal. If the length is
* expressed in ‘gr’ units, these refer to the inherited grid, not the grid defined by the template itself.
* auto The row's height is determined by its contents.
* * (asterisk) All rows with an asterisk will be of equal height.
*
* Each <col-width> can be one of the following:
* <length> An explicit width for that column. Negative values make the template illegal.
* * (asterisk.) All columns with a ‘*’ have the same width. See the algorithm below.
* max-content, min-content, minmax(p,q), fit-content
*/
log.info("compiling display...");
log.debug("display source: ", displayValue);
/**
* @namespace Preprocessed template display info
* @name DisplayMetadata
*/
var displayMetadata =
/**
* @lends DisplayMetadata#
*/
{
/**
* Display type. Currently unused
* @type string
*/
displayType:undefined,
/**
* Array of rows. strings are cleaned
* @type string[]
*/
grid:[],
/**
* Array of columnns widths
* @type string[]
*/
widths:[]
},
allRefExp = /\s*(none|inline)?\s*(:?(\"[A-Za-z0-9\.@ ]+\")\s*(:?\/ ?(\*|\d+(:?px|%)))?)\s*((:?(:?(:?\d+(:?px|%))|\*)\s*)*)/gi,
found,
displayTypeFound,
gridNotFound,
completed;
//all without displayType (:?(\"[A-Za-z0-9\.@ ]+\")\s*(:?\/(\*|\d+(:?em|px|%)))?\s*)((:?(:?(:?\d+(:?em|px|%))|\*) *)*)
// /\s*(inline|block|list-item|inline-block|table|inline-table|table-row-group|table-header-group|table-footer-group|table-row|table-column-group|table-column|table-cell|table-caption|none)?\s*(:?(\"[A-Za-z0-9\.@ ]+\")\s*(:?\/(\*|\d+(:?em|px|%)))?\s*)((:?(:?(:?\d+(:?em|px|%))|\*) *)*)/g
if (displayValue !== undefined) {
while ((found = allRefExp.exec(displayValue)) !== null) {
if (completed) {
log.error("Invalid template, invalid width definition");
throw new Error("Invalid template, width definition");
}
if (found[1]) {
if (displayTypeFound) {
log.error("Invalid template, multiple display type");
throw new Error("Invalid template, multiple display type");
}
displayMetadata.displayType = found[1];
displayTypeFound = true;
}
if (found[3]) {
if (gridNotFound) {
log.error("Invalid template, invalid grid definition");
throw new Error("Invalid template, invalid grid definition");
}
displayMetadata.grid.push({rowText:found[3].replace(/"/g, "").replace(/\s*/, ""), height:undefined});
} else {
gridNotFound = true;
}
if (found[5]) {
if (!displayMetadata.grid[displayMetadata.grid.length - 1]) {
log.error("Invalid template, invalid height definition");
throw new Error("Invalid template, height definition");
}
displayMetadata.grid[displayMetadata.grid.length - 1].height = found[5];
}
if (found[7]) {
displayMetadata.widths = found[7].split(/\s+/);
completed = true;
}
}
}
log.info("display result: ", displayMetadata);
return displayMetadata;
}
/**
* @namespace Top level template. Used as "fake page template"
*/
rootTemplate = function () {
var that =
/**
* @lends rootTemplate#
*/
{
/**
* Inserts given template into TOM.
* If template "isRoot", inserts here, else looks inside TOM and
* inserts in place.
*
* @param {Template}aTemplate the template
*/
insert:function (aTemplate) {
log.info("add template ", aTemplate.selectorText);
if (aTemplate.isRoot()) {
log.debug("insert as root", aTemplate);
that.rows.push(aTemplate);
return true;
} else {
log.debug("search position :", aTemplate.position.position);
return that.rows.some(function (element) {
return element.insert(aTemplate);
});
}
},
/**
* Templates stored into the root template
* @type gridRow[]
*/
rows:[]
};
return that;
}();
function parsePosition(positionValue) {
var positionMetadata, matched, positionRegExp;
/*
* Name: position
* New value: <letter> | same
* Percentages: N/A
* Computed value: ‘<letter>’ or ‘static’; see text
*
* <letter> must be a single letter or digit, with category Lu, Ll, Lt or Nd in Unicode [UNICODE]),
* or a “@” symbol
*/
log.info("compiling position...");
log.debug("position source: ", positionValue);
/**
* @namespace Preprocessed template position info
* @name PositionMetadata
*/
positionMetadata =
/**
* @lends PositionMetadata#
*/
{
/**
* Position string
* @type string
*/
position:null
};
positionRegExp = /^\s*(same|[a-zA-Z0-9])\s*$/i;
if (positionValue !== undefined) {
matched = positionValue.match(positionRegExp);
if (matched === null) {
log.info("Unexpected value at ", positionValue);
//throw new Error("Unexpected value at ", positionValue);
return positionMetadata;
}
positionMetadata.position = matched[1];
}
log.info("position result: ", positionMetadata);
return positionMetadata;
}
function parseProperties(rule) {
var preProcessTemplate = {};
log.info("compiling properties...");
log.debug("properties source: ", rule);
preProcessTemplate.selectorText = rule.selectorText;
preProcessTemplate.display = parseDisplay(rule.declaration[templateLayout.fn.constants.DISPLAY]);
preProcessTemplate.position = parsePosition(rule.declaration[templateLayout.fn.constants.POSITION]);
log.info("properties result: ", preProcessTemplate);
return preProcessTemplate;
}
/**
* Creates a compiler
*
* @class CSS template compiler
*/
compiler = function () {
return new compiler.prototype.init();
};
/**
* Extension point.
* Elements added to compiler.fn extend compiler functionality
*/
compiler.fn = compiler.prototype;
compiler.prototype = {
constructor:compiler,
/**
* @ignore
*/
init:function () {
return this;
},
/**
* Compiles given parser data</p>
*
* @param {ParserBufferEntry[]}buffer parser generated data
* @returns {rootTemplate} Template Object Model
*/
compile:function (buffer) {
var selectorText, preProcessTemplate, inserted, template;
log.info("compile...");
log.debug("buffer: ", buffer);
for (selectorText in buffer) {
if (buffer.hasOwnProperty(selectorText)) {
log.debug("next buffer element: ", selectorText);
log.group();
preProcessTemplate = parseProperties(buffer[selectorText]);
if (this.isEmptyDisplay(preProcessTemplate.display) && this.isEmptyPosition(preProcessTemplate.position)) {
log.groupEnd();
log.info("preProcess: empty template", preProcessTemplate);
} else {
log.debug("preProcess:", preProcessTemplate);
template = compiler.fn.templateBuilder(preProcessTemplate).createTemplate();
inserted = rootTemplate.insert(template);
log.groupEnd();
log.info("element insertion...", inserted ? "[OK]" : "ERROR!");
}
}
}
log.debug("compile... OK");
return rootTemplate;
},
/**
* Checks if display is empty
* @param {DisplayMetadata}display compiled display
* @returns {boolean}true if display.grid.length === 0
*/
isEmptyDisplay:function (display) {
return display.grid.length === 0;
},
/**
* Checks is position is empty
* @param {PositionMetadata}position compiled position
* @returns {boolean}true if position.position === null
*/
isEmptyPosition:function (position) {
return position.position === null;
}
};
compiler.prototype.init.prototype = compiler.prototype;
templateLayout.fn.compiler = compiler;
(function (global) {
var gridSlot;
log.info("load gridSlot module...");
/**
* Creates a slot.
*
* @param {string}slotText slot identifier
* @param {integer}rowIndex row index
* @param {integer}colIndex column index
* @param {Object}[options] optional initialization
* @param {integer}options.rowSpan row span number
* @param {integer}options.colSpan column span number
* @param {boolean}options.allowDisconnected row span number
* @param {boolean}options.allowColSpan row span number
* @param {boolean}options.allowRowSpan row span number
*
* @class Template slot.
* Features:
* <ul>
* <li>column and row span</li>
* <li>disconnected regions</li>
* </ul>
*/
gridSlot = function (slotText, rowIndex, colIndex, options) {
log.debug("slot", slotText + "...");
return new gridSlot.prototype.init(slotText, rowIndex, colIndex, options);
};
gridSlot.prototype = {
constructor:gridSlot,
/**
* slot identifier
* @type string
*/
slotText:undefined,
/**
* row index. If row spans, topmost row index
* @type integer
*/
rowIndex:undefined,
/**
* column index. If column spans, leftmost column index
* @type integer
*/
colIndex:undefined,
/**
* row span number
* @type integer
*/
rowSpan:1,
/**
* column span number
* @type integer
*/
colSpan:1,
/**
* Can exists more than one group of slots with the same identifier?
* @type boolean
*/
allowDisconnected:false,
/**
* is column span allowed?
* @type boolean
*/
allowColSpan:false,
/**
* is row span allowed?
* @type boolean
*/
allowRowSpan:false,
/**
* HTML node that maps the slot
* @type HTMLElement
*/
htmlNode:undefined,
/**
* Stores the sum of children heights
* @type integer
*/
contentHeight:0,
/**
* @ignore
* see gridSlot.constructor
*/
init:function (slotText, rowIndex, colIndex, options) {
this.slotText = slotText;
this.rowIndex = rowIndex;
this.colIndex = colIndex;
this.contentHeight = 0;
//options
this.rowSpan = 1;
this.colSpan = 1;
this.height = "auto";
this.allowDisconnected = false;
this.allowColSpan = true;
this.allowRowSpan = true;
this.htmlNode = undefined;
wef.extend(this, options, ["rowSpan", "colSpan", "allowDisconnected", "allowColSpan", "allowRowSpan"]);
}
};
/**
* Extension point
*/
gridSlot.fn = gridSlot.prototype;
gridSlot.prototype.init.prototype = gridSlot.prototype;
global.gridSlot = gridSlot;
log.info("load gridSlot module... [OK]");
})(compiler.fn);
(function (global) {
var gridRow;
log.info("load gridRow module...");
/**
* Creates a row
*
* @param {string}rowText row slots identifiers
* @param {integer}rowIndex row index
* @param {gridSlot[]}slots row gridSlot elements
* @param {Object}[options] optional initialization
* @param {string}options.height row height as string
*
* @class Template row. Store {@link gridSlot} elements
*/
gridRow = function (rowText, rowIndex, slots, options) {
log.debug("row...");
return new gridRow.prototype.init(rowText, rowIndex, slots, options);
};
gridRow.prototype = {
constructor:gridRow,
/**
* Row slots identifiers
* @type string
*/
rowText:undefined,
/**
* Row index
* @type integer
*/
rowIndex:undefined,
/**
* Slots row gridSlot elements
* @type gridSlot[]
*/
slots:[],
/**
* Number of slots in row
* @type integer
*/
length:undefined,
/**
* Row height as string
* @type string
*/
height:undefined,
/**
* @ignore
* see constructor
*/
init:function (rowText, rowIndex, slots, options) {
this.rowText = rowText;
this.rowIndex = rowIndex;
this.slots = slots;
this.length = this.rowText.length;
//options
this.height = undefined;
wef.extend(this, options, ["height"]);
}
};
/**
* Extension point
*/
gridRow.fn = gridRow.prototype;
gridRow.prototype.init.prototype = gridRow.prototype;
global.gridRow = gridRow;
log.info("load gridRow module... [OK]");
})(compiler.fn);
(function (global) {
var grid;
log.info("load grid module...");
/**
* Creates a template grid
*
* @param {gridRow[]}rows template rows
* @param [options] optional initialization
*
* @class Template grid, represented as a tabular structure
*/
grid = function (rows, options) {
log.debug("grid...");
return new grid.prototype.init(rows, options);
};
grid.prototype = {
constructor:grid,
/**
* Template rows
* @type gridRow[]
*/
rows:undefined,
/**
* Hash table like structure that stores nested Template objects.
* <ul>
* <li>key = template position</li>
* <li>value = array of template objects</li>
* <ul>
*
* @type Object
*/
filledSlots:undefined,
/**
* columns widths
* @type string[]
*/
widths:[],
/**
* minimums columns widths
* @type string[]
*/
minWidths:[],
/**
* preferred columns widths
* @type string[]
*/
preferredWidths:[],
/**
* Number of rows
* @type integer
*/
rowNumber:undefined,
/**
* Number of columns
* @type integer
*/
colNumber:undefined,
/**
* @ignore
*/
init:function (rows, options) {
this.rows = rows;
this.filledSlots = {};
//options
this.widths = [];
this.minWidths = [];
this.preferredWidths = [];
wef.extend(this, options, ["widths", "minWidths", "preferredWidths"]);
this.colNumber = this.widths.length;
this.rowNumber = rows.length;
},
/**
* Checks if grid contains specified slot identifier
*
* @param {string}slotIdentifier slot identifier
* @returns {boolean} true if rows[i].rowText contains slotIdentifier,
* else if not
*/
hasSlot:function hasSlot(slotIdentifier) {
var result;
result = this.rows.some(function (row) {
var regExp = new RegExp(slotIdentifier);
return regExp.exec(row.rowText);
});
log.debug("hasSlot " + slotIdentifier + "?", result ? "yes" : "no");
return result;
},
/**
* Gets the "@" slot OR topmost left slot
*/
getDefaultSlot:function () {
var firstLetterSlot, definedDefaultSlot = false;
this.rows.forEach(function (row) {
if (definedDefaultSlot) {
return; //skip row
}
Array.prototype.some.call(row.rowText, function (slotText, slotIndex) {
if (slotText === "@") {
definedDefaultSlot = row.slots[slotIndex];
return true;
}
if (!firstLetterSlot && slotText !== ".") {
firstLetterSlot = row.slots[slotIndex];
return false; //continue searching @
}
});
});
return definedDefaultSlot || firstLetterSlot;
},
/**
* Traverses this grid and its children and insert the given
* template in place
* @param {template}aTemplate given template
* @returns {boolean} true if inserted, false if not
*/
setTemplate:function (aTemplate) {
var row, tmp, result;
if (this.hasSlot(aTemplate.position.position)) {
//push template
tmp = this.filledSlots[aTemplate.position.position] || [];
tmp.push(aTemplate);
this.filledSlots[aTemplate.position.position] = tmp;
log.debug("grid [" + aTemplate.position.position + "] =", aTemplate);
return true;
} else {
result = this.rows.some(function (row) {
var result;
result = row.slots.some(function (slotId) {
var result;
result = this.filledSlots[slotId.slotText] && this.filledSlots[slotId.slotText].some(function (currentTemplate) {
return !currentTemplate.isLeaf() && currentTemplate.insert(aTemplate);
}, this);
if (!result) {
log.debug("not found, try another slot");
}
return result;
}, this);
if (!result) {
log.debug("not found, try another row");
}
return result;
}, this);
if (!result) {
log.debug("not found, try another branch");
}
return result;
}
}
};
/**
* Extension point
*/
grid.fn = grid.prototype;
grid.prototype.init.prototype = grid.prototype;
global.grid = grid;
log.info("load grid module... [OK]");
})(compiler.fn);
(function (global) {
var template;
log.info("load template module...");
/**
* Creates a template
*
* @param {string}selectorText CSS selector
* @param {PositionMetadata}position raw position information
* @param {DisplayMetadata}display raw display information
* @param {grid}grid its physical structure
*
* @class A Template has a grid, the raw information generated by
* the preprocessor and a link to the DOM reference node
*/
template = function (selectorText, position, display, grid) {
log.debug("template...");
return new template.prototype.init(selectorText, position, display, grid);
};
template.prototype = {
constructor:template,
/**
* Link to parent template. Unused
*/
parentTemplate:undefined,
/**
* CSS selector
* @type string
*/
selectorText:undefined,
/**
* Raw display information
* @type DisplayMetadata
*/
display:undefined,
/**
* Raw position information
* @type PositionMetadata
*/
position:undefined,
/**