Enhanced validation of dojo form controls in context of partial execution events

// *********************************************************************************
// * Copyright 2014 CROSS-WOKRS AG, Adrian Osterwalder
// * Licensed under the Apache License, Version 2.0
// *
// * Written by Adrian Osterwalder, CROSS-WORKS AG, www.cross-works.net
// * Description: This JS-Code limits the XPages validation of partial execution 
// *              events to the specified exexId for all Dojo Form Controls (Dijits)
// *              of the extension library
// * 
// * Code is using the hijacking/publishing method of Tommy Valand
// * published under the Apache License
// * http://dontpanic82.blogspot.ch/2010/01/xpages-hijackingpublishing-partial.html
// *********************************************************************************

// hijack validate all function
function hijackAndPublishValidateAll() {
	require(["dojo/_base/connect"], function(connect){

		if(XSP.hijackAndPublishValidateAll != true){
			console.info("hijack the XSP.validateAll functions and publish events 'queryValidateAll' and 'postValidateAll'");

			// Hijack the partial refresh
			XSP._inheritedValidateAll = XSP.validateAll;
			XSP.validateAll = function(formId,valmode,execId) {

				// Publish before
				connect.publish('queryValidateAll', [formId,valmode,execId]);

				// call orginal function
				var result = this._inheritedValidateAll(formId,valmode,execId);

				// Publish after
				connect.publish('postValidateAll', [formId,valmode,execId]);

				return result;
			}
		}
		XSP.hijackAndPublishValidateAll = true
	});
};


// Globals
var CXW = {};
CXW.tempDisabledDijits = {};

CXW.recursivlyFindWidgets = function(domNode){
	var allWidgets = {};
	CXW._recursivlyFindWidgets(allWidgets,domNode);
	return allWidgets;
}

CXW._recursivlyFindWidgets = function(allWidgets,domNode){
	require(["dijit/registry","dojo/_base/array"], function(registry,array){
		var widgets = registry.findWidgets(domNode);
		array.forEach(widgets, function(widget, i){
			allWidgets[widget.id] = widget;
			CXW._recursivlyFindWidgets(allWidgets,widget.domNode);
		});
	});
};

// Activate session monitoring
require(["dojo/ready", "dojo/_base/connect", "dojo/_base/window", "dojo/_base/array", "dijit/registry", "dijit/form/TextBox"], function(ready, connect, window, array, registry, TextBox){
	ready(function(){

		// hijack the validation function
		hijackAndPublishValidateAll();

		// subscribe to the 'queryValidateAll' event
		connect.subscribe('queryValidateAll', null, function(formId,valmode,execId){

			// check if execId is set
			if(execId && execId != ""){

				// find all widgets in body and disable the Dijits
				var allWidgets = CXW.recursivlyFindWidgets(window.body());
				for(var key in allWidgets){
					var widget = allWidgets[key];
					if(widget.validator){
						if(widget.hasOwnProperty("disabled")){
							CXW.tempDisabledDijits[widget.id] = widget.attr("disabled");
						} else {
							CXW.tempDisabledDijits[widget.id] = false;
						}
						widget.attr("disabled", true);
					}
				}

				// find all widgets within execId and re-enable this dijits
				var validateWidgets = CXW.recursivlyFindWidgets(dojo.byId(execId));
				for(var key in validateWidgets){
					if(CXW.tempDisabledDijits.hasOwnProperty(key)){
						var widget = validateWidgets[key];
						if(widget){
							widget.attr("disabled", CXW.tempDisabledDijits[key]);
							delete CXW.tempDisabledDijits[key];
						}
					}
				}
			}
		});

		// subscribe to the 'postValidateAll' event
		connect.subscribe('postValidateAll', null, function(formId,valmode,execId){
			// restore all disabled Dijits
			for(var key in CXW.tempDisabledDijits){
				var widget = registry.byId(key);
				if(widget){
					widget.attr("disabled", CXW.tempDisabledDijits[key]);
				}
			}
			CXW.tempDisabledDijits = {};
		});
	});
});





This JS-Code limits the XPages validation of partial execution events to the specified exexId for all Dojo Form Controls (Dijits) of the extension library. Put it in a csjs script library and include this library via a theme in your XPage

JavaScript (Client)
Adrian Osterwalder
March 21, 2014 8:49 AM
Rating
6

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



No comments yetLogin first to comment...