243 lines
7.8 KiB
JavaScript
243 lines
7.8 KiB
JavaScript
/* Created by SA
|
|
* User settings and page state management functions
|
|
* ENV-815
|
|
*/
|
|
var C_PREFERENCES_GET_URL = "/User/GetPreferences";
|
|
var C_PREFERENCES_SAVE_URL = "/User/SavePreferences";
|
|
var C_QUICKLINK_LOAD_URL = "/UserQuickLink/GetPreferences";
|
|
|
|
function getPageUrl() {
|
|
return window.location.pathname;
|
|
}
|
|
|
|
function getDataSection(element) {
|
|
var foundSection = '';
|
|
var sections = $(element).parents("*[data-section]");
|
|
|
|
if (sections && (sections.length > 0))
|
|
foundSection = $(sections).first().attr("data-section");
|
|
|
|
return foundSection;
|
|
}
|
|
|
|
function collectPreferences(dataSection) {
|
|
var items = [];
|
|
var dataSectionElement = $(document).find("*[data-section='" + dataSection + "']");
|
|
|
|
if (!dataSectionElement || (dataSectionElement.length < 1))
|
|
dataSectionElement = $(document);
|
|
|
|
var controlsToStore = $(dataSectionElement).find('*[data-key]');
|
|
|
|
for (var index = 0; index < controlsToStore.length; index++) {
|
|
var currentControl = controlsToStore[index];
|
|
var tagName = currentControl.tagName.toLowerCase();
|
|
|
|
if (tagName == 'input') {
|
|
var inputType = $(currentControl).attr('type');
|
|
tagName = tagName + '#' + inputType;
|
|
}
|
|
|
|
var dataKey = $(currentControl).attr('data-key');
|
|
var value = '';
|
|
|
|
switch (tagName) {
|
|
case 'input#checkbox':
|
|
case 'input#radio':
|
|
value = $(currentControl).prop('checked');
|
|
break;
|
|
|
|
case 'input#text':
|
|
case 'input#hidden':
|
|
value = $(currentControl).val();
|
|
break;
|
|
|
|
case 'select':
|
|
value = $(currentControl).val();
|
|
if ($(currentControl).attr('multiple') === 'multiple' && !!value && $.isArray(value))
|
|
value = value.join(',');
|
|
break;
|
|
};
|
|
|
|
items.push({
|
|
Key: dataKey,
|
|
Value: value
|
|
});
|
|
}
|
|
return items;
|
|
}
|
|
|
|
function saveUserPagePreferences(data, dataSection, customPageUrl, callback) {
|
|
var pageUrl = customPageUrl;
|
|
|
|
if (!customPageUrl || (customPageUrl.length < 1))
|
|
pageUrl = getPageUrl();
|
|
|
|
var postData = {};
|
|
postData.data = JSON.stringify(data);
|
|
postData.url = pageUrl;
|
|
postData.section = dataSection;
|
|
|
|
$.post(C_PREFERENCES_SAVE_URL, postData, function (data) {
|
|
if(callback)
|
|
callback();
|
|
}).error(function (h, e) {
|
|
console.log("Save preferences error. h = " + h + ", e = " + e);
|
|
});
|
|
}
|
|
|
|
function loadUserPagePreferences(dataSection, customPageUrl) {
|
|
var pageUrl = customPageUrl;
|
|
if (typeof quickLinkData === 'undefined' && URI().query(true).linkId) {
|
|
$.ajax(C_QUICKLINK_LOAD_URL,
|
|
{
|
|
async: false,
|
|
data: { linkId: URI().query(true).linkId },
|
|
method: "POST",
|
|
success: function (response, textStatus, jqXHR) {
|
|
if (response && response.Status === "OK" && response.Data) {
|
|
quickLinkData = JSON.parse(response.Data);
|
|
if (quickLinkData[dataSection] && quickLinkData[dataSection].length != 0) {//
|
|
return quickLinkData[dataSection];
|
|
}
|
|
else {
|
|
if (!customPageUrl || (customPageUrl.length < 1))
|
|
pageUrl = getPageUrl();
|
|
|
|
var prefs = [];
|
|
|
|
if (typeof quickLinkData !== 'undefined' && quickLinkData[dataSection]) //
|
|
return quickLinkData[dataSection];
|
|
|
|
if (!dataSection || (dataSection.length < 1))
|
|
prefs;
|
|
|
|
$.ajax(C_PREFERENCES_GET_URL,
|
|
{
|
|
async: false,
|
|
data: { url: pageUrl, section: dataSection },
|
|
method: "POST",
|
|
success: function (response, textStatus, jqXHR) {
|
|
if (response && response.Status === "OK" && response.Data) {
|
|
prefs = JSON.parse(response.Data);
|
|
}
|
|
}
|
|
});
|
|
return prefs;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
return quickLinkData[dataSection];
|
|
}
|
|
else {
|
|
if (!customPageUrl || (customPageUrl.length < 1))
|
|
pageUrl = getPageUrl();
|
|
|
|
var prefs = [];
|
|
|
|
if (typeof quickLinkData !== 'undefined' && quickLinkData[dataSection]) {
|
|
return quickLinkData[dataSection];
|
|
}
|
|
|
|
if (!dataSection || (dataSection.length < 1))
|
|
prefs;
|
|
|
|
$.ajax(C_PREFERENCES_GET_URL,
|
|
{
|
|
async: false,
|
|
data: { url: pageUrl, section: dataSection },
|
|
method: "POST",
|
|
success: function (response, textStatus, jqXHR) {
|
|
if (response && response.Status === "OK" && response.Data) {
|
|
prefs = JSON.parse(response.Data);
|
|
}
|
|
}
|
|
});
|
|
return prefs;
|
|
}
|
|
}
|
|
|
|
function loadUserPreferences(dataSection, customPageUrl) {
|
|
var pageUrl = customPageUrl;
|
|
if (!customPageUrl || (customPageUrl.length < 1))
|
|
pageUrl = getPageUrl();
|
|
|
|
var prefs = [];
|
|
$.ajax(C_PREFERENCES_GET_URL,
|
|
{
|
|
async: false,
|
|
data: { url: pageUrl, section: dataSection },
|
|
method: "POST",
|
|
success: function (response, textStatus, jqXHR) {
|
|
if (response && response.Status === "OK" && response.Data) {
|
|
prefs = JSON.parse(response.Data);
|
|
}
|
|
}
|
|
});
|
|
return prefs;
|
|
}
|
|
|
|
function restorePreferences(dataSection, data) {
|
|
if (!dataSection || (dataSection.length < 1) || typeof data === 'undefined')
|
|
return;
|
|
|
|
var dataSectionElement = $(document).find("*[data-section='" + dataSection + "']");
|
|
|
|
if (!dataSectionElement || (dataSectionElement.length < 1))
|
|
dataSectionElement = $(document);
|
|
for (var index = 0; index < data.length; index++) {
|
|
var currentRec = data[index];
|
|
var dataKey = currentRec.Key;
|
|
var value = currentRec.Value;
|
|
|
|
var prefsReceiver = $(dataSectionElement).find('*[data-key="' + dataKey + '"]');
|
|
|
|
if (prefsReceiver && (prefsReceiver.length > 0)) {
|
|
var tagName = prefsReceiver.get(0).tagName.toLowerCase();
|
|
|
|
if (tagName == 'input') {
|
|
var inputType = $(prefsReceiver).attr('type');
|
|
tagName = tagName + '#' + inputType;
|
|
}
|
|
|
|
switch (tagName) {
|
|
case 'input#checkbox':
|
|
case 'input#radio':
|
|
$(prefsReceiver).prop('checked', value);
|
|
break;
|
|
|
|
case 'input#text':
|
|
case 'input#hidden':
|
|
$(prefsReceiver).val(value);
|
|
break;
|
|
|
|
case 'select':
|
|
if ($(prefsReceiver).attr('multiple') === 'multiple' && !!value && !$.isArray(value))
|
|
value = value.split(',');
|
|
|
|
if (!!$(prefsReceiver).data("select2")) {
|
|
$(prefsReceiver).select2('val', value);
|
|
}
|
|
else {
|
|
$(prefsReceiver).val(value);
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
function generalizeUrl(url) {
|
|
var components = url.split("/");
|
|
var result = "";
|
|
|
|
for (var index = 1; index < (components.length - 1) ; index++) {
|
|
result += ("/" + components[index]);
|
|
}
|
|
|
|
return result;
|
|
} |