Javascript and jQuery stuff - Part1

var arrayOfIds = document.querySelectorAll('*[id^="workspaceRightsOfUsage_entity_releaseFormSubjName"]'); //returns array of ids that match

It returns all the divs which have 'workspaceRightsOfUsage_entity_releaseFormSubjName' as substring in the id.
Ex: when multiple input fields exists with same div id but each div id is suffixed with incremented index,

workspaceRightsOfUsage_entity_releaseFormSubjName_0
workspaceRightsOfUsage_entity_releaseFormSubjName_1
workspaceRightsOfUsage_entity_releaseFormSubjName_2

then the querySelectorAll helps as regex to find all such div's

- CSS

.disableRightsOfUsage{
    pointer-events: none;
    cursor: default;
    opacity: 0.4;
}

Used to disable the html element. pointer events will be disabled. If we set opacity, the area will be shaded out

How to make an ajax using jQuery ??????

// INFO
// Used to create a AJAX call and for rendering the result.
// Will trigger the events beginAjax and endAjax
//
// Constructor parameters
// Action: URL to the action; String
//
// Request Parameters: Parameters that will be used by the
// HTTP request. Usage e.g.
// {
// fromClient1: "Hello",
// fromClient2: "Server"
// }
//
// HTML Generator: A mmWidget.
//
// Methods
// Empty

function mmAjaxCall(action, requestParameters, htmlGenerator, errorCallback){
$.event.trigger('beginAjax');

var jqxhr = $.ajax({
url: action,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
  dataType: 'json',
  type: 'POST',
  data: requestParameters,
  success: htmlGenerator }
).complete(function() {  
$.event.trigger('endAjax');
});
if(errorCallback != null){
jqxhr.error(errorCallback);
}
}


Learnt the following JS code from my current project. 

var currentImportWidget;

//#########################################################
//##################### FileMapEntry ######################
//#########################################################
function FileMapEntry(params) {
var paramsDefault = {
data: null,
id: null,
state: 0,
validationInformation: {},
validationWarningInformation: {},
assetId: null,
// assetId and renditionFileType are for customRrenditionWidget
renditionFileType: null
};
var mergedParams = $.extend({}, paramsDefault, params);
this.params = mergedParams;
}

FileMapEntry.prototype.setData = function(data) {
this.params = data;
}

FileMapEntry.prototype.getData = function() {
return this.params.data;
}

FileMapEntry.prototype.setId = function(id) {
this.params.id = id;
}

FileMapEntry.prototype.getId = function() {
return this.params.id;
}

FileMapEntry.prototype.setState = function(state) {
this.params.state = state;
}

FileMapEntry.prototype.getState = function() {
// 0 - Ready for processing. File has not been uploaded/downloaded.
// 1 - File is currently being uploaded/downloaded.
// 2 - Upload/download was completed sucessfully.
// 3 - Upload/download was stopped.
// 4 - Error occured.
return this.params.state;
}

FileMapEntry.prototype.isStateValid = function() { 
return this.params.state === 0;
}

FileMapEntry.prototype.isValidationInformationSet = function() {
return typeof this.params.validationInformation.title !== 'undefined';
}

FileMapEntry.prototype.getValidationInformation = function() {
return this.params.validationInformation;
}

FileMapEntry.prototype.getValidationInformationMessage = function() {
if(this.isValidationInformationSet()) {
return this.getValidationInformation().message;
}
return null;
}

FileMapEntry.prototype.getValidationInformationTitle = function() {
if(this.isValidationInformationSet()) {
return this.getValidationInformation().title;
}
return null;
}

// {title: title, message: message}
FileMapEntry.prototype.setValidationInformation = function(validationInformation) {
this.params.validationInformation = validationInformation;
}

FileMapEntry.prototype.isValidationWarningInformationSet = function() {
return typeof this.params.validationWarningInformation.title !== 'undefined';
}

FileMapEntry.prototype.getValidationWarningInformation = function() {
return this.params.validationWarningInformation;
}

FileMapEntry.prototype.getValidationWarningInformationMessage = function() {
if(this.isValidationWarningInformationSet()) {
return this.getValidationWarningInformation().message;
}
return null;
}

FileMapEntry.prototype.getValidationWarningInformationTitle = function() {
if(this.isValidationWarningInformationSet()) {
return this.getValidationWarningInformation().title;
}
return null;
}

// {title: title, message: message}
FileMapEntry.prototype.setValidationWarningInformation = function(validationWarningInformation) {
this.params.validationWarningInformation = validationWarningInformation;
}
FileMapEntry.prototype.setAssetId = function(id) {
this.params.assetId = id;
}

FileMapEntry.prototype.getAssetId = function() {
return this.params.assetId;
}
FileMapEntry.prototype.setRenditionFileType= function(type) {
this.params.renditionFileType = type;
}

FileMapEntry.prototype.getRenditionFileType = function() {
return this.params.renditionFileType;
}

//#########################################################
//####################### FileMap #########################
//#########################################################

function FileMap() {
this.fileMap = {};
}

FileMap.prototype.get = function(id) {
return this.fileMap[id];
}

FileMap.prototype.add = function(fileMapEntry) {
this.fileMap[fileMapEntry.getId()] = fileMapEntry;
}

FileMap.prototype.remove = function(id) {
delete this.fileMap[id]; 
}

FileMap.prototype.clear = function() {
this.fileMap = {};
}

FileMap.prototype.getKeys = function() {
var keys = [];
for(var key in this.fileMap){
if(this.get(key).isStateValid()) {
keys.push(key);
}
return keys;
}

FileMap.prototype.getIterator = function() {
return new FileMapIterator(this);
}

//#########################################################
//#################### FileMapIterator ####################
//#########################################################

function FileMapIterator(fileMap) {
this.fileMap = fileMap;
this.keys = fileMap.getKeys();
this.index = -1;
}


Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights