-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_dom-writers.js
144 lines (120 loc) · 4.25 KB
/
_dom-writers.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
define(["_dom-readers", "_array-helpers", "_object-helpers", "_tag-builders"], function(readers, arrays, objects, builders){
var writerFunctions = {
appendTextToElement: appendTextToElement,
appendToBody: appendToBody,
appendTags: appendTags,
appendToFirstArticle: appendToFirstArticle,
insertNameDfnToPlaceHolders: insertNameDfnToPlaceHolders,
insertSanskritDfnToPlaceHolders: insertSanskritDfnToPlaceHolders,
readWordsWriteDfnTags: readWordsWriteDfnTags,
writeDfnTags: writeDfnTags,
writeLetter: writeLetter,
writeTagsFromBuilderSets: writeTagsFromBuilderSets,
writeTemplates: writeTemplates,
};
return writerFunctions;
function appendTags(id, tags, position){
var element = readers.readElement(id);
if(element == undefined){ return; }
tags.forEach(appendTagToElementBuilder(element, position));
}
function appendTagToElementBuilder(element, position){
return function(tag){
var relPosition = (position) ? position : "beforeEnd";
element.insertAdjacentElement(relPosition, tag);
};
}
function appendToFirstArticle(element){
var lastArticle = readers.readFirstArticle();
lastArticle.insertAdjacentElement('beforeEnd', element);
}
function appendToBody(element){
readers.readBody().insertAdjacentElement('beforeEnd', element);
}
function writeLetter(contentsMap){
appendToBody(builders.buildLetter(contentsMap));
}
function writeTagsFromBuilderSets(builderSets){
objects.forEach(builderSets, writeTagFromBuilderSet);
}
function writeTagFromBuilderSet(set){
var tag = buildContentOrPlaceHolderTag(set.builder, set.readerId, set.tagId);
appendTags(set.writerId, [tag]);
}
function buildContentOrPlaceHolderTag(tagBuilder, readerId, tagId){
if (tagId){
return tagBuilder(tagId);
}else{
var content = readers.readTagContents(readerId);
return tagBuilder(content);
}
}
function readWordsWriteDfnTags(wordSets){
objects.forEach(wordSets, readWriteFromSet);
}
function readWriteFromSet(set){
set.words = readWords(set);
if(set.words.length == 0){ return; }
writeDfnTags(set);
}
function writeDfnTags(wordSet){
var writerId = wordSet.writerId;
var wordTags = buildWordTags(wordSet);
var tagStack = builders.stackBuilder(wordTags);
appendTags(writerId, [tagStack]);
}
function buildWordTags(wordSet){
var wordTags = wordSet.words.map(builders.buildWordDfnTag);
insertSeparators(wordSet, wordTags);
return wordTags;
}
function insertSeparators(wordSet, wordTags){
var seperator = wordSet.seperator;
if(seperator){ arrays.insertSeparators(wordTags, seperator); }
}
function readWords(wordSet){
var readerId = wordSet.readerId;
return readers.readWords([readerId]);
}
function writeTemplates(readerId, writerId){
var templates = readers.readWords([readerId]);
templates.forEach(writeTemplateTags(writerId));
}
function writeTemplateTags(writerId){
return function(template){
var templatePath = ["_tmpl-", template].join("");
require([templatePath], function(tags){
appendTags(writerId, tags);
});
};
}
function insertNameDfnToPlaceHolders(names){
names.forEach(findNameClassesAndInsertDfnTags);
}
function findNameClassesAndInsertDfnTags(name){
var placeHolders = readers.readClassNameElements(name);
[].forEach.call(placeHolders, insertNameDfnToPlaceHolder(name));
}
function insertNameDfnToPlaceHolder(name){
return function(placeHolder){
var dfnTag = builders.buildNameDfnTag(name);
placeHolder.insertAdjacentElement('beforeEnd', dfnTag);
}
}
function insertSanskritDfnToPlaceHolders(words){
words.forEach(findSanskritClassesAndInsertDfnTags);
}
function findSanskritClassesAndInsertDfnTags(word){
var placeHolders = readers.readClassNameElements(word.replace(" ", "_"));
[].forEach.call(placeHolders, insertSanskritDfnToPlaceHolder(word));
}
function insertSanskritDfnToPlaceHolder(word){
return function(placeHolder){
var dfnTag = builders.buildSanskritDfnTag(word);
placeHolder.insertAdjacentElement('beforeEnd', dfnTag);
}
}
function appendTextToElement(id, text){
readers.readElement(id).insertAdjacentText("beforeEnd", text);
}
});