1 | //+----------------------------------------------------------------------------- |
---|
2 | //+ Isidorus |
---|
3 | //+ (c) 2008-2009 Marc Kuester, Christoph Ludwig, Lukas Giessmann |
---|
4 | //+ |
---|
5 | //+ Isidorus is freely distributable under the LGPL 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/LGPL-LICENSE.txt and |
---|
9 | //+ in trunk/src/ajax/javascripts/external/MIT-LICENSE.txt. |
---|
10 | //+----------------------------------------------------------------------------- |
---|
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 | // --- if the validation succeeded the fragment will be sent to the server |
---|
135 | var tPsis = topic.getContent().subjectIdentifiers; |
---|
136 | if(!tPsis || tPsis.length === 0) tPsis = "null"; |
---|
137 | else tPsis = tPsis.toJSON() |
---|
138 | var referencedTopics = topic.getReferencedTopics(); |
---|
139 | if(associations){ |
---|
140 | var ePsis = null; |
---|
141 | if(contents && contents.topic && contents.topic.subjectIdentifiers && contents.topic.subjectIdentifiers.length !== 0){ |
---|
142 | ePsis = contents.topic.subjectIdentifiers; |
---|
143 | } |
---|
144 | |
---|
145 | var aStubs = associations.getReferencedTopics(); |
---|
146 | if(aStubs && aStubs.length !== 0){ |
---|
147 | aStubs = aStubs.without(CURRENT_TOPIC).uniq(); |
---|
148 | for(var i = 0; ePsis && i !== ePsis.length; ++i) aStubs = aStubs.without(ePsis[i]); |
---|
149 | } |
---|
150 | referencedTopics = referencedTopics.concat(aStubs); |
---|
151 | } |
---|
152 | |
---|
153 | function onSuccessHandler(topicStubs){ |
---|
154 | var tsStr = "null"; |
---|
155 | if(topicStubs && topicStubs.length !== 0){ |
---|
156 | tsStr = "["; |
---|
157 | for(var i = 0; i !== topicStubs.length; ++i){ |
---|
158 | tsStr += topicStubs[i]; |
---|
159 | if(i !== topicStubs.length - 1) tsStr += ","; |
---|
160 | } |
---|
161 | tsStr += "]"; |
---|
162 | } |
---|
163 | var jTopic = "\"topic\":" + topic.toJSON(); |
---|
164 | var jTopicStubs = "\"topicStubs\":" + tsStr; |
---|
165 | var jAssociations = "\"associations\":" + (associations ? associations.toJSON().gsub("\\[\"" + CURRENT_TOPIC_ESCAPED + "\"\\]", tPsis) : "null"); |
---|
166 | var jTmId = "\"tmIds\":" + tmId.toJSON(); |
---|
167 | var json = "{" + jTopic + "," + jTopicStubs + "," + jAssociations + "," + jTmId + "}"; |
---|
168 | commitFragment(json, function(xhr){ alert("The fragment was committed succesfully!"); }, null); |
---|
169 | } |
---|
170 | |
---|
171 | function onErrorHandler(){ |
---|
172 | // --- currently there is not needed a special handling for errors |
---|
173 | // --- occurred during this operation |
---|
174 | } |
---|
175 | getTopicStubs(referencedTopics, onSuccessHandler, onErrorHandler); |
---|
176 | }); |
---|
177 | |
---|
178 | var liCB = new Element("li", {"class" : CLASSES.commitButton()}); |
---|
179 | liCB.insert({"top" : validateButton}); |
---|
180 | liCB.insert({"top" : commitButton}); |
---|
181 | liTm.insert({"after" : liCB}); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | // --- removes old elements from the fragment frame |
---|
186 | function clearFragment() |
---|
187 | { |
---|
188 | var items = $$("li." + CLASSES.topicFrame()); |
---|
189 | for(var i = 0; i != items.length; ++i){ |
---|
190 | items[i].remove(); |
---|
191 | } |
---|
192 | |
---|
193 | items = $$("li." + CLASSES.associationContainer()); |
---|
194 | for(var i = 0; i != items.length; ++i){ |
---|
195 | items[i].remove(); |
---|
196 | } |
---|
197 | |
---|
198 | items = $$("li." + CLASSES.tmIdFrame()); |
---|
199 | for(var i = 0; i !== items.length; ++i){ |
---|
200 | items[i].remove(); |
---|
201 | } |
---|
202 | |
---|
203 | items = $$("li." + CLASSES.commitButton()); |
---|
204 | for(var i = 0; i !== items.length; ++i){ |
---|
205 | items[i].remove(); |
---|
206 | } |
---|
207 | } |
---|