[isidorus-cvs] r42 - trunk/src/ajax/javascripts

Author: lgiessmann Date: Tue Jun 16 06:04:53 2009 New Revision: 42 Log: ajax-client: by commiting a fragment, there will be requested all needed topicStubs for a valid fragment by the ajax as a "background process", so the user does not need to care about the referenced topicStubs Modified: trunk/src/ajax/javascripts/constants.js trunk/src/ajax/javascripts/create.js trunk/src/ajax/javascripts/datamodel.js trunk/src/ajax/javascripts/requests.js Modified: trunk/src/ajax/javascripts/constants.js ============================================================================== --- trunk/src/ajax/javascripts/constants.js (original) +++ trunk/src/ajax/javascripts/constants.js Tue Jun 16 06:04:53 2009 @@ -31,6 +31,7 @@ var ANY_URI = "http://www.w3.org/2001/XMLSchema#anyURI"; var STRING = "http://www.w3.org/2001/XMLSchema#string"; var CURRENT_TOPIC = "**current-topic**"; +var CURRENT_TOPIC_ESCAPED = "\\*\\*current-topic\\*\\*"; // --- Contains most css classes used in this project // --- There should be called only the function to be sure to don't override Modified: trunk/src/ajax/javascripts/create.js ============================================================================== --- trunk/src/ajax/javascripts/create.js (original) +++ trunk/src/ajax/javascripts/create.js Tue Jun 16 06:04:53 2009 @@ -56,7 +56,31 @@ var commitButton = new Element("input", {"type" : "button", "value" : "commit fragment", "style" : "float: right; margin-top: -10px;"}) commitButton.observe("click", function(event){ - alert("commit fragment"); + try{ + var tPsis = topic.getContent().subjectIdentifiers; + var referencedTopics = topic.getReferencedTopics().concat(associations.getReferencedTopics()).without(CURRENT_TOPIC).uniq(); + + function onSuccessHandler(topicStubs){ + var str = "null"; + if(topicStubs && topicStubs.length !== 0){ + str = "["; + for(var i = 0; i !== topicStubs.length; ++i){ + str += topicStubs[i]; + if(i !== topicStubs.length - 1) str += ","; + } + str += "]"; + } + var json = "{\"topic\":" + topic.toJSON() + ",\"topicStubs\":" + str + ",\"associations\":" + associations.toJSON().gsub(CURRENT_TOPIC_ESCAPED, tPsis) + ",\"tmIds\":" + "[\"myTM\"]}"; + alert(json); + } + + function onErrorHandler(){ + // --- currently there is not neede a special handling for errors + // --- occurred during this operation + } + + getTopicStubs(referencedTopics, onSuccessHandler, onErrorHandler); + }catch(err){ alert("test: " + err); } }); var liCB = new Element("li", {"class" : CLASSES.commitButton()}); liCB.update(commitButton); Modified: trunk/src/ajax/javascripts/datamodel.js ============================================================================== --- trunk/src/ajax/javascripts/datamodel.js (original) +++ trunk/src/ajax/javascripts/datamodel.js Tue Jun 16 06:04:53 2009 @@ -1018,7 +1018,9 @@ }, "getContent" : function(){ if(this.isUsed() === false) return null; + var type = this.__type__.__frames__[0].getContent(); return {"itemIdentities" : this.__itemIdentity__.getContent(true, true), + "type" : type ? new Array(type) : null, "scopes" : this.__scope__.getContent(), "value" : this.__value__.__frames__[0].getContent(), "variants" : this.__variants__.getContent()}; @@ -1470,6 +1472,42 @@ }, "hasPsi" : function(){ return this.__subjectIdentifier__.getContent(true, true).length !== 0; + }, + "getReferencedTopics" : function(){ + var referencedTopics = new Array(); + var names = this.getContent().names; + if(names){ + for(var i = 0; i !== names.length; ++i){ + // TODO: variant (-scope topicStubs) + var type = names[i].type; + if(type){ + if(referencedTopics.indexOf(type[0]) === -1) referencedTopics.push(type[0]); + } + var scopes = names[i].scopes; + if(scopes){ + for(var j = 0; j !== scopes.length; ++j){ + if(referencedTopics.indexOf(scopes[j]) === -1) referencedTopics.push(scopes[j]); + } + } + } + } + + var occurrences = this.getContent().occurrences; + if(occurrences){ + for(var i = 0; i !== occurrences.length; ++i){ + var type = occurrences[i].type; + if(type){ + if(referencedTopics.indexOf(type[0]) === -1) referencedTopics.push(type[0]); + } + var scopes = occurrences[i].scopes; + if(scopes){ + for(var j = 0; j !== scopes.length; ++j){ + if(referencedTopics.indexOf(scopes[j]) === -1) referencedTopics.push(scopes[j]); + } + } + } + } + return referencedTopics; }}); @@ -2302,8 +2340,9 @@ }, "getContent" : function(){ if(!this.isUsed()) return null; + var type = this.__type__.__frames__[0].getContent(); return {"itemIdentities" : this.__itemIdentity__.getContent(true, true), - "type" : new Array(this.__type__.__frames__[0].getContent()), + "type" : type ? new Array(type) : null, "scopes" : this.__scope__.getContent(), "roles" : this.__roles__.getContent()}; }, @@ -2412,6 +2451,32 @@ else rows[i].show(); } this.__minimized__ = !this.__minimized__; + }, + "getReferencedTopics" : function(){ + var referencedTopics = new Array(); + var associations = this.getContent(); + if(associations){ + for(var i = 0; i !== associations.length; ++i){ + var assType = associations[i].type; + if(referencedTopics.indexOf(assType[0]) === -1) referencedTopics.push(assType[0]); + var scopes = associations[i].scopes; + if(scopes){ + for(var j = 0; j !== scopes.length; ++j){ + if(referencedTopics.indexOf(scopes[j]) === -1) referencedTopics.push(scopes[j]); + } + } + var roles = associations[i].roles; + if(roles){ + for(var j = 0; j !== roles.length; ++j){ + var roleType = roles[j].type; + if(roleType && referencedTopics.indexOf(roleType[0]) === -1) referencedTopics.push(roleType[0]); + var player = roles[j].topicRef; + if(player && referencedTopics.indexOf(player[0]) === -1) referencedTopics.push(player[0]); + } + } + } + } + return referencedTopics; }}); Modified: trunk/src/ajax/javascripts/requests.js ============================================================================== --- trunk/src/ajax/javascripts/requests.js (original) +++ trunk/src/ajax/javascripts/requests.js Tue Jun 16 06:04:53 2009 @@ -52,4 +52,57 @@ catch(err){ alert("Could not request contraints, please try again!\n\n" + err); } +} + + +// --- gets all topicStubs information for the passed psis and +// --- executes the onSuccessHandler or the on FailureHandler +// --- if all stubs are requested or one request fails. +function getTopicStubs(psis, onSuccessHandler, onFailureHandler) +{ + if(!onSuccessHandler || !onFailureHandler) throw "From getTopicStubs(): onsuccessHandler and onFailureHandler must be set!"; + try{ + var topicStubs = new Array(); + + if(psis){ + for(var i = 0; i !== psis.length; ++i){ + var url = GET_STUB_PREFIX + psis[i].gsub("#", "%23"); + new Ajax.Request(url, { + "method" : "get", + "requestHeaders" : ["If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT"], + "onSuccess" : function(xhr){ + if(xhr.responseText.length === 0 || xhr.responseText.isJSON() === false) errorHandler("Got bad JSON-Data for \"" + psis[i] + "\"!"); + else topicStubs.push(xhr.responseText); + }, + "onFailure" : function(xhr){ + alert("From getTopicStubs(): Could not equest topicStub information for \"" + xhr.request.url + "\"!!!"); + onFailureHandler(); + }}); + } + } + + // --- Checks the requested value. If there are all values requested, there will be called the + // --- onSuccessHandler - otherwise (after the maximum time out or an faild request) there will + // --- be called the onErrorHandler. + var maxTimeout = psis.length * TIMEOUT; + var neededTime = 0; + function checkRequests(){ + var delta = 100; + neededTime += delta; + if(delta > maxTimeout){ + alert("From getTopicStubs(): Please check your network-connection - the request timed out!!!"); + onFailureHandler(); + return; + } + + if(topicStubs.length === psis.length) onSuccessHandler(topicStubs); + else setTimeout(checkRequests, delta); + } + + checkRequests(); + + } + catch(err){ + alert("From getTopicStubs(): Could not request topicStubs information for: " + psis + "\n\n" + err); + } } \ No newline at end of file
participants (1)
-
Lukas Giessmann