/** * Helper object for cookie handling (get by name, get all and set). * * @type {{get: Function, set: Function}} */ Cookie = { get: function(name) { var data = document.cookie.split(";"); var cookies = {}; for(var i = 0; i < data.length; ++i) { var tmp = data[i].split("="); cookies[tmp[0].trim()] = tmp[1].trim(); } if (name) { return (cookies[name.trim()] || null); } else { return cookies; } }, set: function(name, value) { document.cookie = name.trim() + "=" + value.trim(); } }; /** * This method returns true if the user is logged in, false if not. * * @returns {*} */ function isUserLoggedIn() { var loggedInCookie = Cookie.get('CipSessionActive'); return ((loggedInCookie != null) && (loggedInCookie == 'true')); } /** * This method return the default login url. * * @returns {string} */ function getDefaultLoginUrl() { return '/mein_bereich/login_mein_bereich.html'; } /** * This method returns the lifetime of a session; * The time before a logged in users will be logged out. */ function getSessionLifetimeInMilliseconds() { return '900000'; } /** * this method starts the session timeout counter. */ function startSessionTimeoutCounter() { if (isUserLoggedIn()) { window.setTimeout(function(){ location.href=getDefaultLoginUrl(); }, getSessionLifetimeInMilliseconds() + 100); } } /** * Lazy-Loading: Replaces a placeholder with the actual content. * * @param id - the id of the descriptor to deliver the content for. * @param successCallback - the callback function to place the content. * @param errorCallback - the callback in case of an error. */ function loadLazyDescriptorContent(id, successCallback, errorCallback) { var formData = new FormData(); formData.append("cipDescId", id); formData.append("cipExOut", 'true'); formData.append("cipLazy", 'true'); jQuery.ajax({ url: window.location.pathname, type: 'POST', data: formData, processData: false, contentType: false, cache: false, success: successCallback, error: errorCallback}); } /** * Adds an item to the watchlist. */ function watchlistAddItem(title, requestUrl, type, description, state, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/add.app', {title: title, requestUrl: requestUrl, type: type, description: description, state: state}, callback ); } /** * Adds an item to the watchlist with attachments/files. There are separate callbacks for complete, success and error scenario. */ function watchlistAddItemExtended(title, requestUrl, type, description, state, files, completeCallback, successCallback, errorCallback) { var formData = new FormData(); for (var i = 0; files && i < files.length; i++){ formData.append('fileData' + i, files[i]); } formData.append("title", title); formData.append("type", type); formData.append("description", description); formData.append("requestUrl", requestUrl); jQuery.each(state, function(index1, value1){ jQuery.each(value1, function(index2, value2){ formData.append("state[" + index1 + "][" + index2 + "]", value2); }); }); jQuery.ajax( {url: '/pfb/content/cip/util/watchlist/add.app', type: 'POST', data: formData, cache: false, dataType: 'json', processData: false, contentType: false, success: successCallback, error: errorCallback, complete: completeCallback}); } /** * Adds an item to the watchlist with large binary/text data as file/blob. * WARNING: JSON Data must be sent as String e.g, JSON.stringify(fileData). */ function watchlistAddItemBinary(title, requestUrl, type, description, state, fileType, fileName, fileEncoding, fileData, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/add.app', {title: title, requestUrl: requestUrl, type: type, description: description, state: state, fileType: fileType, fileName: fileName, fileEncoding: fileEncoding, fileData: fileData}, callback); } /** * Edits an item in the watchlist. */ function watchlistEditItem(id, title, description, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/edit.app', {id: id, title: title, description: description}, callback ); } /** * Returns the state of an item in the watchlist. */ function watchlistGetItem(id, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/getItem.app', {cipWlState: id}, callback ); } /** * Returns the state of an item in the watchlist. */ function watchlistGetItemState(id, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/getState.app', {cipWlState: id}, callback ); } /** * Returns all items in the watchlist */ function watchlistGetItemBinaries(id, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/getBinaries.app', {cipWlState: id}, callback ); } /** * Removes an item from the watchlist. */ function watchlistRemoveItem(id, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/remove.app', {cipWlState: id}, callback ); } /** * Returns all items in the watchlist */ function watchlistGetItems(callback) { jQuery.post( '/pfb/content/cip/util/watchlist/show.app', {}, callback ); } /** * Sends all given items via email. function watchlistSend(email, item_ids, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/send.app', {email: email, 'item[]': item_ids}, callback ); } */ /** * Sets the flag cipPageInWatchlist. true if the currently visited page is already * in the watchlist, false if not. * The flag is also false, if there is no session. */ function watchlistContainsPage(url, callback) { jQuery.post( '/pfb/content/cip/util/watchlist/containsPage.app', {url: url}, callback ); } /** * Saves the settings of the user. */ function saveUserSettings(settings, callback) { jQuery.post( '/pfb/content/cip/util/member/saveUserSettings.app', {settings: settings}, callback ); } /** * Returns the settings from the user. */ function getUserSettings(callback) { jQuery.post( '/pfb/content/cip/util/member/getUserSettings.app', {}, callback ); } /** * Delets all settings of the user. */ function deleteUserSettings(callback) { jQuery.post( '/pfb/content/cip/util/member/deleteUserSettings.app', {}, callback ); } // todo: saveUserImage /** * Deletes the profile image of the user. */ function saveUserImage(file, successCallback, errorCallback, completeCallback) { var formData = new FormData(); formData.append('fileData', file); jQuery.ajax({ url: '/pfb/content/cip/util/member/saveUserImage.app', type: 'POST', data: formData, cache: false, dataType: 'json', processData: false, contentType: false, success: successCallback, error: errorCallback, complete: completeCallback}); } /** * Deletes the profile image of the user. */ function deleteUserImage(callback) { jQuery.post( '/pfb/content/cip/util/member/deleteUserImage.app', {}, callback ); } /** * Returns the general opt-in of the user. */ function isAllowedOptIn(callback) { jQuery.post( '/pfb/content/cip/util/member/isAllowedOptIn.app', {}, callback ); } /** * Returns the Ebi-Id */ function getEbiId(callback) { jQuery.post( '/pfb/content/cip/util/member/getEbiId.app', {}, callback, 'json' ); } /** * Sets the Ebi-Id */ function setEbiId(ebiId, callback) { jQuery.post( '/pfb/content/cip/util/member/setEbiId.app', {ebiId: ebiId}, callback, 'json' ); } /** * Returns the data form the current user. */ function getUserData(callback) { jQuery.post( '/pfb/content/cip/util/member/getUserData.app', {}, callback, 'json' ); } /** * Returns the server time. */ function getServerTime() { var serverTime = null; jQuery.ajax( {url: '/pfb/content/cip/util/serverTime.app', global: false, async: false, cache: false, dataType: 'json', type: 'POST', success: function(data, textStatus, jqXHR) { serverTime = new Date(); serverTime.setTime(data.time); }} ); return serverTime; } /** * Object for history operations. */ var History = new function() { var request = function(url, data, callback) { jQuery.post(url, data, callback); } this.add = function(title, url, callback) { request('/pfb/content/cip/util/history/add.app', {title: title, url: url}, callback); } this.list = function(callback) { request('/pfb/content/cip/util/history/show.app'); } };