API Docs for:
Show:

File: generic-app.js

/*******************************************************************************
 * Copyright (c) 2017 Genialist Software Ltd.
 * All rights reserved.
 ******************************************************************************/

const pText = {
	create_folder: { 
		name: 'Folder name:',
		msg_start: 'Creating $N Folder...',
		msg_409: 'A folder already exists with this name on the server, please try another name...'
	},
	create_playlist: {
		name: 'Playlist name:',
		msg_start: 'Creating playlist $N...',
		msg_end: 'Creating playlist $N... Done.',
		msg_409: 'A playlist already exists with this name, please try another name...',
		failure: 'Failed to create playlist, the server returned an error:<br/>$S'
	},
	remove_playlist: {
		question: 'Are you sure to delete this playlist from the library?'
	},
	rename_parent_folder: {
		name: 'New name:',
		msg_start: 'Renaming Parent Folder to $N...',
		msg_end: 'Renaming Parent Folder to $N... Done.', 
		msg_409: 'A folder already exists with the same name: $N',
		failure: 'Failed to rename parent folder, the server returned an error:<br/>$S'
	},
	safe: function(t, n, s) {
		if (t) {
			if (n) t = t.replace(/\$N/, pString.encodeHTML(n));
			if (s) t = t.replace(/\$S/, pString.encodeHTML(s));
		}
		return t;
	}
};

window.pROSE = new (function() {
	this.tracePrefix = 'ROSE';
	var that = this;
	
	this.query_param_autostart = "start";
	this.query_param_play = "play";
	
	var props = new pMap([
			'audio-cache-next',						{ type: "boolean", vdefault: true },

			'view-user-advanced-play-homevideos',	{ type: "boolean", vdefault: true },
			
			'view-user-advanced-play-radios',		{ type: "boolean", vdefault: true },

			'view-user-advanced-play-movies',		{ type: 'boolean', vdefault: false },

			'view-user-advanced-play-images',		{ type: "boolean", vdefault: false },
			'view-user-advanced-show-images', 		{ type: "boolean", vdefault: true },

			'view-user-advanced-play-books',		{ type: "boolean", vdefault: false },
			'view-user-advanced-show-book-images', 	{ type: "boolean", vdefault: true },

			'ui-dialog-for-select',					{ type: 'boolean', vdefault: pDevice.isIOS() }
		]);
	
	function transformProp(v, p) {
			if (p && p.type) {
				
				if (p.getValue) v = p.getValue(v);
								
				if (p.type == "boolean") {
					if (!v)
						return false;
					return (v == "true" || v == "1")? true : ((v == "false" || v == "0")? false : v);
				}
				if (p.type == "int") {
					v = parseInt(v);
					if (null!=p.min) v = Math.max(v, p.min);
					if (null!=p.max) v = Math.min(v, p.max);
				}
				if (p.type == "string") {
					if (p.vdefault.forArray && p.vdefault.length>0) {
						if (p.vdefault.find(function(a) { return v == a }))
							return v;
						return vdefault[0];
					}
				}
			}
			return v;
	}

	this.getProps = function() {
		return props.keys();
	};
	
	this.getProp = function(n, p) {
		if (n.name!=null && n.type!=null && n.vdefault!=null) {
			p = n;
			n = p.name;

			//*** STORE PROP SO IT CAN BE RETRIEVED BY ITS NAME LATER
			props.put(n, p);
		}
		p = p || props.getValue(n, null);
		
		var v = pCookieManager.getCookie(n);
		if (!v)
			v = p? p.vdefault : null;
		if (p && p.type!='array' && v && v.forEach && v.length>0)
			v = v[0];

		return transformProp(v, p);
	};
	
	this.setProp = function(n, v, p) {
		if (n.name!=null && n.type!=null && n.vdefault!=null) {
			p = n;
			n = p.name;

			//*** STORE PROP SO IT CAN BE RETRIEVED BY ITS NAME LATER
			props.put(n, p);
		}
		p = p || props.getValue(n, null);

		if (p && v === p.vdefault)
			pCookieManager.remove(n);
		else if (p && p.type == "boolean")
			pCookieManager.setGlobal(n, v? 1:0);
		else if (p && p.type == "string") {
			if (p.vdefault.forArray && p.vdefault.length>0) {
				if (p.vdefault[0] == v)
					pCookieManager.remove(n);
				else if (p.vdefault.find(function(v) { return v == v }))
					pCookieManager.setGlobal(n, v);
				else
					pCookieManager.remove(n); //set default value (as value does not match enum)
			}
			else
				pCookieManager.setGlobal(n, v);
		}
		else
			pCookieManager.setGlobal(n, v);
	};
	this.newBooleanProp = function(n, d) {
		return { name: n, type: 'boolean', vdefault: d || false };
	};
	this.newStringProp = function(n, d) {
		return { name: n, type: 'string', vdefault: d };
	};
	this.showErrorPage =  function(t) {
		//document.body.style.background = "initial";
		document.body.style.backgroundImage = null;
		document.body.className = "background-index";
		document.body.innerHTML = t;
	};
	this.isErrorPage = function(r) {
		return !r.responseText.startsWith('while') && r.responseText.indexOf('<html')>=0;
	};
	
	this.handleHTTPResponse = function(request, p_ignoreHtml) { // returns true if caller can continue, otherwise false...
		if (pConsole.debugging()) {
			try {
				alert("AJAX Response:\ncode: "+request.status+" "+request.statusText+"\nresponse: \n"+pJSON.pretty(request.responseText));
			}
			catch (exception) {
				pConsole.error(this, "AJAX Response (Incorrect JSON):\ncode: "+request.status+" "+request.statusText+"\nresponse: \n"+request.responseText);
				alert("AJAX Response (Incorrect JSON):\ncode: "+request.status+" "+request.statusText+"\nresponse: \n"+request.responseText);
			}
		}

		//server error
		if (request.status==500 && pROSE.isErrorPage(request)) {
			pDocument.stopwait();
			pROSE.showErrorPage(request.responseText);
			return false;
		}

		if (request.status!=200) {
			pDocument.stopwait();
			return false; //TODO: ...
		}

		//requires login
		if (request.status==200 && (request.responseURL.indexOf('/login')>0 || request.getResponseHeader("x-requires-login")=="true")) {
			//alert('here');
			window.location.replace("/login?url="+encodeURI(pLocation.getPathAndQuery())+"&expired=true");
			return false;
		}

		if (request.status==200 && !p_ignoreHtml && pROSE.isErrorPage(request)) {
			//alert('here2');
			window.location.replace("/login?url="+encodeURI(pLocation.getPathAndQuery())+"&expired=true");
			return false;
		}

		// other errors
		if (request.status!=200 || (!p_ignoreHtml && pROSE.isErrorPage(request))) {
			pDocument.stopwait();
			pROSE.showErrorPage(request.responseText);
			return false;
		}

		// tells the caller to continue
		return true;
	};
	
	this.canonicalizeAdvisory = function(a) {
		a = a.toLowerCase().split('|');
		if (a.includes('explicit')) return 'explicit';
		if (a.includes('clean')) return 'clean';
		/*if (a) {
	 		var i_adv = ('|'+a+'|').toLowerCase();
			if (a != 'explicit')
				if (i_adv.indexOf('|explicit|')>=0)
					return 'explicit';
	
			if (a != 'clean')
				if (i_adv.indexOf('|clean|')>=0)
					return 'clean';
		}*/
		return a;
	};

	//*** DEFAULT VALUES
	props.getValue('audio-cache-next', null).vdefault = pDevice.isIOS();

	this.getMainLang = function() {
		return pApplicationUI.OPTION_PREFERRED_LANGS[0];
	};

	this.getMainLongLang = function() {
		return pLang.getLongName(pApplicationUI.OPTION_PREFERRED_LANGS[0]);
	};

	this.debugging = function() {
		return pCookieManager.getCookie('view-user-advanced-debug')=='true';
	};
	
	this.initOptions = function(x) {
		if (x && x.options)
			for(var o in x.options) {
				var p = x.options[o];
				pROSE.getProp(p);
				p.get = (function() { return pROSE.getProp(this); }).bind(p);
				
				Object.defineProperty(x, o, { get: p.get });
			}
	};
});

/******************************************************************************/
/******************************************************************************/
/******************************************************************************/