/GroovyFlow/note/new/war/js/Spring-Dojo.js.uncompressed.js

http://groovyflow.googlecode.com/ · JavaScript · 416 lines · 328 code · 59 blank · 29 comment · 81 complexity · 60d52777a61a251afab1b957a430b8c7 MD5 · raw file

  1. /*
  2. * Copyright 2004-2008 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. dojo.declare("Spring.DefaultEquals", null, {
  17. equals : function(/*Object*/other){
  18. if (other.declaredClass && other.declaredClass == this.declaredClass) {
  19. return true;
  20. }else{
  21. return false;
  22. }
  23. }
  24. });
  25. dojo.declare("Spring.ElementDecoration", [Spring.AbstractElementDecoration, Spring.DefaultEquals], {
  26. constructor : function(config) {
  27. this.copyFields = new Array('name', 'value', 'type', 'checked', 'selected', 'readOnly', 'disabled', 'alt', 'maxLength', 'class', 'title');
  28. dojo.mixin(this, config);
  29. if(this.widgetModule == "") {
  30. this.widgetModule = this.widgetType;
  31. }
  32. },
  33. apply : function(){
  34. if (dijit.byId(this.elementId)) {
  35. dijit.byId(this.elementId).destroyRecursive(false);
  36. }
  37. var element = dojo.byId(this.elementId);
  38. if (!element) {
  39. console.error("Could not apply " + this.widgetType + " decoration. Element with id '" + this.elementId + "' not found in the DOM.");
  40. }
  41. else {
  42. var datePattern = this.widgetAttrs['datePattern'];
  43. if (datePattern && this.widgetType == 'dijit.form.DateTextBox') {
  44. if (!this.widgetAttrs['value']) {
  45. this.widgetAttrs['value'] = dojo.date.locale.parse(element.value, {selector : "date", datePattern : datePattern});
  46. }
  47. if (!this.widgetAttrs['serialize']) {
  48. this.widgetAttrs['serialize'] = function(d, options){
  49. return dojo.date.locale.format(d, {selector : "date", datePattern : datePattern});
  50. }
  51. }
  52. }
  53. for (var copyField in this.copyFields) {
  54. copyField = this.copyFields[copyField];
  55. if (!this.widgetAttrs[copyField] && element[copyField] &&
  56. (typeof element[copyField] != 'number' ||
  57. (typeof element[copyField] == 'number' && element[copyField] >= 0))) {
  58. this.widgetAttrs[copyField] = element[copyField];
  59. }
  60. }
  61. if(element['style'] && element['style'].cssText){
  62. this.widgetAttrs['style'] = element['style'].cssText;
  63. }
  64. dojo.require(this.widgetModule);
  65. var widgetConstructor = dojo.eval(this.widgetType);
  66. this.widget = new widgetConstructor(this.widgetAttrs, element);
  67. this.widget.startup();
  68. }
  69. //return this to support method chaining
  70. return this;
  71. },
  72. validate : function(){
  73. if (!this.widget.isValid) {
  74. // some widgets cannot be validated
  75. return true;
  76. }
  77. var isValid = this.widget.isValid(false);
  78. if (!isValid) {
  79. this.widget.state = "Error";
  80. this.widget._setStateClass();
  81. }
  82. return isValid;
  83. }
  84. });
  85. dojo.declare("Spring.ValidateAllDecoration", [Spring.AbstractValidateAllDecoration, Spring.DefaultEquals], {
  86. constructor : function(config) {
  87. this.originalHandler = null;
  88. this.connection = null;
  89. dojo.mixin(this, config);
  90. },
  91. apply : function() {
  92. var element = dojo.byId(this.elementId);
  93. if (!element) {
  94. console.error("Could not apply ValidateAll decoration. Element with id '" + this.elementId + "' not found in the DOM.");
  95. } else {
  96. this.originalHandler = element[this.event];
  97. var context = this;
  98. element[this.event] = function(event){
  99. context.handleEvent(event, context);
  100. };
  101. }
  102. return this;
  103. },
  104. cleanup : function(){
  105. dojo.disconnect(this.connection);
  106. },
  107. handleEvent : function(event, context){
  108. if (!Spring.validateAll()) {
  109. dojo.publish(this.elementId+"/validation", [false]);
  110. dojo.stopEvent(event);
  111. } else {
  112. dojo.publish(this.elementId+"/validation", [true]);
  113. if(dojo.isFunction(context.originalHandler)) {
  114. var result = context.originalHandler(event);
  115. if (result == false) {
  116. dojo.stopEvent(event);
  117. }
  118. }
  119. }
  120. }
  121. });
  122. dojo.declare("Spring.AjaxEventDecoration", [Spring.AbstractAjaxEventDecoration, Spring.DefaultEquals], {
  123. constructor : function(config){
  124. this.validationSubscription = null;
  125. this.connection = null;
  126. this.allowed = true;
  127. dojo.mixin(this, config);
  128. },
  129. apply : function() {
  130. var element = dojo.byId(this.elementId);
  131. if (!element) {
  132. console.error("Could not apply AjaxEvent decoration. Element with id '" + this.elementId + "' not found in the DOM.");
  133. } else {
  134. this.validationSubscription = dojo.subscribe(this.elementId+"/validation", this, "_handleValidation");
  135. this.connection = dojo.connect(element, this.event, this, "submit");
  136. }
  137. return this;
  138. },
  139. cleanup : function(){
  140. dojo.unsubscribe(this.validationSubscription);
  141. dojo.disconnect(this.connection);
  142. },
  143. submit : function(event){
  144. if (this.sourceId == ""){
  145. this.sourceId = this.elementId;
  146. }
  147. if(this.formId == ""){
  148. Spring.remoting.getLinkedResource(this.sourceId, this.params, this.popup);
  149. } else {
  150. if (this.allowed){
  151. Spring.remoting.submitForm(this.sourceId, this.formId, this.params);
  152. }
  153. }
  154. dojo.stopEvent(event);
  155. },
  156. _handleValidation : function(success){
  157. if (!success) {
  158. this.allowed=false;
  159. } else {
  160. this.allowed=true;
  161. }
  162. }
  163. });
  164. dojo.declare("Spring.RemotingHandler", Spring.AbstractRemotingHandler, {
  165. constructor : function(){},
  166. submitForm : function(/*String */ sourceId, /*String*/formId, /*Object*/ params) {
  167. var content = new Object();
  168. for (var key in params) {
  169. content[key] = params[key];
  170. }
  171. var sourceComponent = dojo.byId(sourceId);
  172. if (sourceComponent != null){
  173. if(sourceComponent.value != undefined && sourceComponent.type && ("button,submit,reset").indexOf(sourceComponent.type) < 0) {
  174. content[sourceId] = sourceComponent.value;
  175. }
  176. else if(sourceComponent.name != undefined) {
  177. content[sourceComponent.name] = sourceComponent.name;
  178. } else {
  179. content[sourceId] = sourceId;
  180. }
  181. }
  182. if (!content['ajaxSource']) {
  183. content['ajaxSource'] = sourceId;
  184. }
  185. dojo.xhrPost({
  186. content: content,
  187. form: formId,
  188. handleAs: "text",
  189. headers: {"Accept" : "text/html;type=ajax"},
  190. // The LOAD function will be called on a successful response.
  191. load: this.handleResponse,
  192. // The ERROR function will be called in an error case.
  193. error: this.handleError
  194. });
  195. },
  196. getLinkedResource: function(/*String */ linkId, /*Object*/params, /*boolean*/ modal) {
  197. this.getResource(dojo.byId(linkId).href, params, modal);
  198. },
  199. getResource: function(/*String */ resourceUri, /*Object*/params, /*boolean*/ modal) {
  200. dojo.xhrGet({
  201. url: resourceUri,
  202. content: params,
  203. handleAs: "text",
  204. headers: {"Accept" : "text/html;type=ajax"},
  205. load: this.handleResponse,
  206. error: this.handleError,
  207. modal: modal
  208. });
  209. },
  210. handleResponse: function(response, ioArgs) {
  211. //First check if this response should redirect
  212. var redirectURL = ioArgs.xhr.getResponseHeader('Spring-Redirect-URL');
  213. var modalViewHeader = ioArgs.xhr.getResponseHeader('Spring-Modal-View');
  214. var modalView = ((dojo.isString(modalViewHeader) && modalViewHeader.length > 0) || ioArgs.args.modal);
  215. if (dojo.isString(redirectURL) && redirectURL.length > 0) {
  216. if (modalView) {
  217. //render a popup with the new URL
  218. Spring.remoting.renderURLToModalDialog(redirectURL, ioArgs);
  219. return response;
  220. }
  221. else {
  222. if (redirectURL.indexOf("/") >= 0) {
  223. window.location = window.location.protocol + "//" + window.location.host + redirectURL;
  224. } else {
  225. var location = window.location.protocol + "//" + window.location.host + window.location.pathname;
  226. var appendIndex = location.lastIndexOf("/");
  227. location = location.substr(0,appendIndex+1) + redirectURL;
  228. if (location == window.location) {
  229. Spring.remoting.getResource(location, ioArgs.args.content, false);
  230. }
  231. else {
  232. window.location = location;
  233. }
  234. }
  235. return response;
  236. }
  237. }
  238. //Extract and store all <script> elements from the response
  239. var scriptPattern = '(?:<script(.|[\n|\r])*?>)((\n|\r|.)*?)(?:<\/script>)';
  240. var extractedScriptNodes = [];
  241. var matchAll = new RegExp(scriptPattern, 'img');
  242. var matchOne = new RegExp(scriptPattern, 'im');
  243. var scriptNodes = response.match(matchAll);
  244. if (scriptNodes != null)
  245. {
  246. for (var i=0; i<scriptNodes.length; i++)
  247. {
  248. var script = (scriptNodes[i].match(matchOne) || ['','',''])[2];
  249. script = script.replace(/<!--/mg,'').replace(/\/\/-->/mg,'').replace(/<!\[CDATA\[(\/\/>)*/mg,'').replace(/(<!)*\]\]>/mg,'');
  250. extractedScriptNodes.push(script);
  251. }
  252. }
  253. response = response.replace(matchAll, '');
  254. //Extract the new DOM nodes from the response
  255. var tempSpan = dojo.doc.createElement("span");
  256. tempSpan.id="ajaxResponse";
  257. tempSpan.style.visibility= "hidden";
  258. document.body.appendChild(tempSpan);
  259. tempSpan.innerHTML=response;
  260. var tempContainer = new dojo.NodeList(tempSpan);
  261. var newNodes = tempContainer.query("#ajaxResponse > *").orphan();
  262. tempContainer.orphan();
  263. //For a modal view, just dump the new nodes into a modal dialog
  264. if (modalView) {
  265. Spring.remoting.renderNodeListToModalDialog(newNodes);
  266. }
  267. else {
  268. //Insert the new DOM nodes and update the Form's action URL
  269. newNodes.forEach(function(item){
  270. if (item.id != null && item.id != "") {
  271. var target = dojo.byId(item.id);
  272. if (!target) {
  273. console.error("An existing DOM elment with id '" + item.id + "' could not be found for replacement.");
  274. } else {
  275. target.parentNode.replaceChild(item, target);
  276. }
  277. }
  278. });
  279. }
  280. //Evaluate any script code
  281. dojo.forEach(extractedScriptNodes, function(script){
  282. dojo.eval(script);
  283. });
  284. return response;
  285. },
  286. handleError: function(response, ioArgs) {
  287. dojo.require("dijit.Dialog");
  288. console.error("HTTP status code: ", ioArgs.xhr.status);
  289. if (Spring.debug && ioArgs.xhr.status != 200) {
  290. var dialog = new dijit.Dialog({});
  291. dojo.connect(dialog, "hide", dialog, function(){
  292. this.destroyRecursive(false);
  293. });
  294. dialog.domNode.style.width = "80%";
  295. dialog.domNode.style.height = "80%";
  296. dialog.domNode.style.textAlign = "left";
  297. dialog.setContent(ioArgs.xhr.responseText);
  298. dialog.show();
  299. }
  300. return response;
  301. },
  302. renderURLToModalDialog: function(url, ioArgs) {
  303. url = url + "&"+dojo.objectToQuery(ioArgs.args.content);
  304. Spring.remoting.getResource(url, {}, true);
  305. },
  306. renderNodeListToModalDialog: function(nodes) {
  307. dojo.require("dijit.Dialog");
  308. var dialog = new dijit.Dialog({});
  309. dialog.setContent(nodes);
  310. dojo.connect(dialog, "hide", dialog, function(){
  311. this.destroyRecursive(false);
  312. });
  313. dialog.show();
  314. }
  315. });
  316. dojo.declare("Spring.CommandLinkDecoration", [Spring.AbstractCommandLinkDecoration, Spring.DefaultEquals], {
  317. constructor : function(config){
  318. dojo.mixin(this, config);
  319. },
  320. apply : function(){
  321. var advisedNode = dojo.byId(this.elementId);
  322. if (!dojo.hasClass(advisedNode, "progressiveLink")) {
  323. //Node must be replaced
  324. var nodeToReplace = new dojo.NodeList(advisedNode);
  325. nodeToReplace.addContent(this.linkHtml, "after").orphan("*");
  326. //Get the new node
  327. advisedNode = dojo.byId(this.elementId);
  328. }
  329. advisedNode.submitFormFromLink = this.submitFormFromLink;
  330. //return this to support method chaining
  331. return this;
  332. },
  333. submitFormFromLink : function(/*String*/ formId, /*String*/ sourceId, /*Array of name,value params*/ params){
  334. var addedNodes = [];
  335. var formNode = dojo.byId(formId);
  336. var linkNode = document.createElement("input");
  337. linkNode.name = sourceId;
  338. linkNode.value = "submitted";
  339. addedNodes.push(linkNode);
  340. dojo.forEach(params, function(param){
  341. var paramNode = document.createElement("input");
  342. paramNode.name=param.name;
  343. paramNode.value=param.value;
  344. addedNodes.push(paramNode);
  345. });
  346. dojo.forEach(addedNodes, function(nodeToAdd){
  347. dojo.addClass(nodeToAdd, "SpringLinkInput");
  348. dojo.place(nodeToAdd, formNode, "last");
  349. });
  350. if ((formNode.onsubmit ? !formNode.onsubmit() : false) || !formNode.submit()) {
  351. dojo.forEach(addedNodes, function(hiddenNode){
  352. formNode.removeChild(hiddenNode);
  353. });
  354. }
  355. }
  356. });
  357. dojo.addOnLoad(Spring.initialize);