/**
* Copyright (c) 2013/2014, Intel Performance Learning Solutions Ltd, Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
**/
/**
* Contains functions commonly used in the UI
*
* created by Sridhar Voorakkara (vsridhar420@gmail.com)
*/
//does a ajax post to the server without using jquery libraries
function xmlhttpPost(strURL, querystring, callback, authKey) {
var xmlHttpReq;
// Mozilla/Safari
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('POST', strURL, true);
xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if (authKey) {
xmlHttpReq.setRequestHeader('authenticationKey', authKey);
}
//add a callback
xmlHttpReq.onreadystatechange = function() {
if (xmlHttpReq.readyState == 4) { //if request is completed, go to callback function
if (xmlHttpReq.status == 200)
callback(null, xmlHttpReq.responseText);
else {
var err = {'errorText':xmlHttpReq.statusText,
'errorDetail': xmlHttpReq.responseText,
'errorCode' : xmlHttpReq.status};
callback(err);
}
}
};
//send the data
xmlHttpReq.send(querystring);
}
//does a ajax get to the server without using jquery libraries
function xmlhttpGet(strURL, callback, authKey) {
var xmlHttpReq;
// Mozilla/Safari
if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('GET', strURL, true);
if (authKey) {
xmlHttpReq.setRequestHeader('authenticationKey', authKey);
}
//add a callback
xmlHttpReq.onreadystatechange = function() {
if (xmlHttpReq.readyState == 4) { //if request is completed, go to callback function
if (xmlHttpReq.status == 200)
callback(null, xmlHttpReq.responseText);
else {
var err = {'errorText':xmlHttpReq.statusText,
'errorDetail': xmlHttpReq.responseText,
'errorCode' : xmlHttpReq.status};
callback(err);
}
}
};
//send the data
xmlHttpReq.send();
}
//cookie functions
var docCookies = {
get: function (key) { //get a cookie
return encodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;\\s*)" + decodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*)|.*"), "$1")) || null;
},
set: function (key, val, end, path, domain, secure) { //set a cookie
if (!key || /^(?:expires|max\-age|path|domain|secure)$/i.test(key)) { //check input is valid
return false;
}
//determine expiration date
var expires = "";
if (end) {
switch (end.constructor) {
case Number:
expires = (end === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + end);
break;
case String:
expires = "; expires=" + end;
break;
case Date:
expires = "; expires=" + end.toGMTString();
break;
}
}
document.cookie = decodeURIComponent(key) + "=" + decodeURIComponent(val) + expires + (domain ? "; domain=" + domain : "") + (path ? "; path=" + path : "") + (secure ? "; secure" : "");
return true;
},
remove: function (key, path) { //remove a cookie
if (!key || !this.exists(key)) { //check inputs
return false;
}
document.cookie = decodeURIComponent(key) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (path ? "; path=" + path : "");
return true;
},
exists: function (key) { //check if a cookie exists
return (new RegExp("(?:^|;\\s*)" + decodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
}
};
//remove a user feedback message
var clearMessage = function () {
$("#feedback").html('');
};
//show a user feedback message
var displayMessage = function (msg) {
$("#feedback").html('<br><br>' + msg);
};
//show a wait graphic to the user
var displayWait = function () {
$("#feedback").html("<img src='resources/images/ajaxloader.gif'>");
};
//create jquery autocomplete (called after a user logs in)
var setupAutocompleteOnLogin = function () {
for (var key in autocompletionList) {
var autoCompleteObj = autocompletionList[key];
$( "#"+autoCompleteObj.name ).autocomplete({
source: autoCompleteObj.source.replace('user', userStatus().user),
minLength: 2,
autoFocus: true,
change: autoCompleteObj.change,
delay:250
});
}
};
//populate a business process selection list in the UI
var populateBpSelectList = function (target) {
var selectedBp = target.value;
getBpList(function (bplist) {
if (bplist.length > 0) {
target.innerHTML = '';
var o=document.createElement("option");
o.text='--Please select Business Process--';
o.value='';
target.add(o, null);
}
for (var i = 0; i < bplist.length; i++) {
o = document.createElement("option");
o.text=bplist[i].label;
o.value=bplist[i].value;
if (selectedBp.length > 0 && o.value === selectedBp)
o.selected = true;
target.add(o, null);
}
});
};
//populate alternate selection list in a UI
var populateAlternateList = function (target, bpId) {
if (bpId == "") {
target.innerHTML = '';
var o=document.createElement("option");
o.text='--Please select business process first--';
o.value='';
target.add(o, null);
return;
}
getAlternateList(bpId, function (altList) {
if (altList.length > 0) {
target.innerHTML = '';
var o=document.createElement("option");
o.text='--Please select alternative--';
o.value='';
target.add(o, null);
}
for (var i = 0; i < altList.length; i++) {
var o = document.createElement("option");
o.text=altList[i].label;
o.value=altList[i].value;
target.add(o, null);
}
});
};
//populate resources selection list in the UI
var populateResourceSelectList = function(bpId, target, callback) {
if (typeof target === 'string') //if name is passed
target = document.getElementById(target);
getResources(bpId, function(resources) {
target.innerHTML = '';
var o=document.createElement("option");
o.text='--Please select Resource--';
o.value='';
target.add(o, null);
for (var i = 0; i < resources.length; i++) {
o = document.createElement("option");
var name = resources[i].name;
var id = resources[i].id;
o.text=id + ' : ' + name;
o.value=id;
target.add(o, null);
}
if (callback)
callback(target);
});
};
//populate artifact selection list in the UI
var populateArtifactSelectList = function (bpid, target) {
if (typeof target === 'string') //if name is passed
target = document.getElementById(target);
if (bpid=='') {
target.innerHTML = '';
var o=document.createElement("option");
o.text='--Please select a business process first--';
o.value='';
target.add(o, null);
return;
}
getArtifacts(bpid, function(artifacts) {
target.innerHTML = '';
var o=document.createElement("option");
o.text='--Please select Artifact--';
o.value='';
target.add(o, null);
for (var i = 0; i < artifacts.length; i++) {
o = document.createElement("option");
var name = artifacts[i].name;
var id = artifacts[i].id;
o.text=id + ' : ' + name;
o.value=id;
target.add(o, null);
}
});
};
//display a message in a jquery dialog
var toDialog = function (messages, dialog, titleTxt) {
if (messages.length > 0) {
var msg = "<ul style='text-align:left'>";
for (var i = 0; i < messages.length; i++) {
msg = msg + "<li>" + messages[i].message + "</li>";
}
msg = msg + "</ul>";
$("#"+dialog).html(msg);
}
$("#"+dialog).dialog({autoOpen: false, title: titleTxt, width: 'auto', modal: true,
show: {effect: "blind", duration: 500}});
$("#"+dialog).dialog('open');
};
//check for errors in server response and pass on the appropriate data to the callback
var checkFOrErrorAndSendData = function(err, data, callback) {
if (err) {
var errTxt = "Server Error : " + err.errorCode + "\n\n"
+ err.errorText + "\n\n" + err.errorDetail.substring(0,800);
alert(errTxt);
}
if (data)
data = JSON.parse(data);
callback(data);
};