1 | //+----------------------------------------------------------------------------- |
---|
2 | //+ Isidorus |
---|
3 | //+ (c) 2008-2010 Marc Kuester, Christoph Ludwig, Lukas Georgieff |
---|
4 | //+ |
---|
5 | //+ Isidorus is freely distributable under the LLGPL license. |
---|
6 | //+ This ajax module uses the frameworks PrototypeJs and Scriptaculous, both |
---|
7 | //+ are distributed under the MIT license. |
---|
8 | //+ You can find a detailed description in trunk/docs/LLGPL-LICENSE.txt, and |
---|
9 | //+ trunk/docs/LGPL-LICENSE.txt in |
---|
10 | //+ trunk/src/ajax/javascripts/external/MIT-LICENSE.txt. |
---|
11 | //+----------------------------------------------------------------------------- |
---|
12 | |
---|
13 | // --- Creates the "create"-sub-page. |
---|
14 | function makeCreate(psi) |
---|
15 | { |
---|
16 | var content = new Element("div", {"class" : CLASSES.content()}); |
---|
17 | var header = new Element("h1").update("Create a Topic"); |
---|
18 | content.insert({"bottom" : header}); |
---|
19 | $(CLASSES.subPage()).insert({"bottom" : content}); |
---|
20 | |
---|
21 | try{ |
---|
22 | var fragmentFrame = new Element("ul", {"class" : CLASSES.fragmentFrame()}); |
---|
23 | content.insert({"bottom" : fragmentFrame}); |
---|
24 | var liTopicSelect = new Element("li", {"class" : CLASSES.instanceOfFrame()}); |
---|
25 | fragmentFrame.insert({"bottom" : liTopicSelect}); |
---|
26 | |
---|
27 | function innerMakeFragment(psis, constraints){ |
---|
28 | makeFragment(liTopicSelect, psis, constraints, null); |
---|
29 | } |
---|
30 | |
---|
31 | function onSuccessHandler(xhr){ |
---|
32 | var json = null; |
---|
33 | try{ |
---|
34 | json = xhr.responseText.evalJSON(); |
---|
35 | } |
---|
36 | catch(innerErr){ |
---|
37 | alert("Got bad JSON data from " + xhr.request.url + "\n\n" + innerErr); |
---|
38 | } |
---|
39 | var instanceOf = null; |
---|
40 | try{ |
---|
41 | if(json === null){ |
---|
42 | var err = new Element("div", {"class" : CLASSES.error()}).update("There exist no valid topic types!<br/>Please update the TMCL-model."); |
---|
43 | liTopicSelect.insert({"bottom" : err}); |
---|
44 | } |
---|
45 | else { |
---|
46 | instanceOf = new InstanceOfC(json.flatten().sort(), innerMakeFragment, psi); |
---|
47 | liTopicSelect.insert({"bottom" : instanceOf.getFrame()}); |
---|
48 | } |
---|
49 | } |
---|
50 | catch(innerErr){ |
---|
51 | alert("There occurred an error by creating an InstanceOfC frame, please reload this page!\n\n" + innerErr); |
---|
52 | } |
---|
53 | } //onSuccessHandler |
---|
54 | |
---|
55 | getPsis(onSuccessHandler, null, {"types" : true}); |
---|
56 | }catch(err){ |
---|
57 | alert("From makeCreate(): " + err); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | // --- Creates the sub-elemts Topic, Associations and Topic Maps ID of a Fragment element. |
---|
63 | function makeFragment(context, psis, constraints, contents){ |
---|
64 | clearFragment(); |
---|
65 | |
---|
66 | var topicContent = null; |
---|
67 | var associationsContent = null; |
---|
68 | var tmContent = null; |
---|
69 | if(contents){ |
---|
70 | topicContent = contents.topic; |
---|
71 | associationsContent = contents.associations; |
---|
72 | tmContent = contents.tmIds; |
---|
73 | } |
---|
74 | |
---|
75 | var instanceOfs = new Array(); |
---|
76 | for(var i = 0; psis && i !== psis.length; ++i){ |
---|
77 | instanceOfs.push(new Array(psis[i])); |
---|
78 | } |
---|
79 | if(contents) { |
---|
80 | if(topicContent) instanceOfs = topicContent.instanceOfs; |
---|
81 | else instanceOfs = new Array(); |
---|
82 | } |
---|
83 | var topic = new TopicC(topicContent, (constraints ? constraints.topicConstraints : null), instanceOfs); |
---|
84 | var liT = new Element("li", {"class" : CLASSES.topicFrame()}).update(topic.getFrame()); |
---|
85 | context.insert({"after" : liT}); |
---|
86 | |
---|
87 | var liA = null; |
---|
88 | var associations = null; |
---|
89 | if((constraints && constraints.associationsConstraints && constraints.associationsConstraints.length !== 0) || associationsContent && associationsContent.length !== 0){ |
---|
90 | addTopicAsPlayer((constraints ? constraints.associationsConstraints : null), topic.getContent().instanceOfs); |
---|
91 | associations = new AssociationContainerC(associationsContent, (constraints ? constraints.associationsConstraints : null)); |
---|
92 | liA = new Element("li", {"class" : CLASSES.associationContainer()}).update(associations.getFrame()); |
---|
93 | liT.insert({"after" : liA}); |
---|
94 | } |
---|
95 | else { |
---|
96 | liA = liT; |
---|
97 | } |
---|
98 | |
---|
99 | var tmId = new TmIdC(tmContent); |
---|
100 | var liTm = new Element("li", {"class" : CLASSES.tmIdFrame()}).update(tmId.getFrame()); |
---|
101 | liA.insert({"after" : liTm}); |
---|
102 | |
---|
103 | // --- validates the data if there is any content |
---|
104 | if(contents){ |
---|
105 | topic.isValid(); |
---|
106 | if(associations) associations.isValid(); |
---|
107 | tmId.isValid(); |
---|
108 | } |
---|
109 | |
---|
110 | var validateButton = new Element("input", {"type" : "button", "value" : "validate fragment"}); |
---|
111 | validateButton.observe("click", function(event){ |
---|
112 | var ret = true; |
---|
113 | if(topic.isValid() === false) ret = false; |
---|
114 | if(associations && associations.isValid() === false) ret = false; |
---|
115 | if(tmId.isValid() === false) ret = false; |
---|
116 | |
---|
117 | if(ret === true) alert("Fragment is valid!"); |
---|
118 | else alert("Fragment is not valid!"); |
---|
119 | }); |
---|
120 | |
---|
121 | var commitButton = new Element("input", {"type" : "button", "value" : "commit fragment"}) |
---|
122 | commitButton.observe("click", function(event){ |
---|
123 | // --- validates the given data |
---|
124 | var ret = true; |
---|
125 | if(topic.isValid() === false) ret = false; |
---|
126 | if(associations && associations.isValid() === false) ret = false; |
---|
127 | if(tmId.isValid() === false) ret = false; |
---|
128 | |
---|
129 | if(ret === false){ |
---|
130 | alert("The fragment wasn't committed - Please correct your input data!"); |
---|
131 | return; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | // --- if the validation succeeded the fragment will be sent to the server |
---|
136 | var tPsis = topic.getContent().subjectIdentifiers; |
---|
137 | if(!tPsis || tPsis.length === 0) tPsis = "null"; |
---|
138 | else tPsis = tPsis.toJSON() |
---|
139 | var referencedTopics = topic.getReferencedTopics(); |
---|
140 | if(associations){ |
---|
141 | var ePsis = null; |
---|
142 | if(contents && contents.topic && contents.topic.subjectIdentifiers && contents.topic.subjectIdentifiers.length !== 0){ |
---|
143 | ePsis = contents.topic.subjectIdentifiers; |
---|
144 | } |
---|
145 | |
---|
146 | var aStubs = associations.getReferencedTopics(); |
---|
147 | if(aStubs && aStubs.length !== 0){ |
---|
148 | aStubs = aStubs.without(CURRENT_TOPIC).uniq(); |
---|
149 | for(var i = 0; ePsis && i !== ePsis.length; ++i) aStubs = aStubs.without(ePsis[i]); |
---|
150 | } |
---|
151 | referencedTopics = referencedTopics.concat(aStubs); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | function onSuccessHandler(topicStubs){ |
---|
156 | var tsStr = "null"; |
---|
157 | if(topicStubs && topicStubs.length !== 0){ |
---|
158 | tsStr = "["; |
---|
159 | for(var i = 0; i !== topicStubs.length; ++i){ |
---|
160 | tsStr += topicStubs[i]; |
---|
161 | if(i !== topicStubs.length - 1) tsStr += ","; |
---|
162 | } |
---|
163 | tsStr += "]"; |
---|
164 | } |
---|
165 | |
---|
166 | var jTopic = "\"topic\":" + topic.toJSON(); |
---|
167 | var jTopicStubs = "\"topicStubs\":" + tsStr; |
---|
168 | var jAssociations = "\"associations\":" + (associations ? associations.toJSON().gsub("\\[\"" + CURRENT_TOPIC_ESCAPED + "\"\\]", tPsis) : "null"); |
---|
169 | var jTmId = "\"tmIds\":" + tmId.toJSON(); |
---|
170 | var json = "{" + jTopic + "," + jTopicStubs + "," + jAssociations + "," + jTmId + "}"; |
---|
171 | |
---|
172 | commitFragment(json, function(xhr){ alert("The fragment was committed succesfully!"); }, null); |
---|
173 | } |
---|
174 | |
---|
175 | function onErrorHandler(){ |
---|
176 | // --- currently there is not needed a special handling for errors |
---|
177 | // --- occurring during this operation |
---|
178 | } |
---|
179 | getTopicStubs(referencedTopics, onSuccessHandler, onErrorHandler); |
---|
180 | }); |
---|
181 | |
---|
182 | var liCB = new Element("li", {"class" : CLASSES.commitButton()}); |
---|
183 | liCB.insert({"top" : validateButton}); |
---|
184 | liCB.insert({"top" : commitButton}); |
---|
185 | liTm.insert({"after" : liCB}); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | // --- removes old elements from the fragment frame |
---|
190 | function clearFragment() |
---|
191 | { |
---|
192 | var items = $$("li." + CLASSES.topicFrame()); |
---|
193 | for(var i = 0; i != items.length; ++i){ |
---|
194 | items[i].remove(); |
---|
195 | } |
---|
196 | |
---|
197 | items = $$("li." + CLASSES.associationContainer()); |
---|
198 | for(var i = 0; i != items.length; ++i){ |
---|
199 | items[i].remove(); |
---|
200 | } |
---|
201 | |
---|
202 | items = $$("li." + CLASSES.tmIdFrame()); |
---|
203 | for(var i = 0; i !== items.length; ++i){ |
---|
204 | items[i].remove(); |
---|
205 | } |
---|
206 | |
---|
207 | items = $$("li." + CLASSES.commitButton()); |
---|
208 | for(var i = 0; i !== items.length; ++i){ |
---|
209 | items[i].remove(); |
---|
210 | } |
---|
211 | } |
---|