All Downloads are FREE. Search and download functionalities are using the official Maven repository.

web.js.smart.ext.SmartContainer.js Maven / Gradle / Ivy

zk.$package('smart.ext');
/**
 * SMART Container
 */
var SmartContainer =
smart.ext.SmartContainer = zk.$extends(zul.utl.Iframe, {
	_manifest : null,
	
	_app_instance : null,
	
	_smart : null,
	
	_context : null,
	
	_active : false,
	
	_request : 0,
	
	_requests : {},

	$init : function() {
		var self = this;
		this.$supers('$init', arguments);
		this._smart = new SMART_CONNECT_HOST();
		this._smart.get_credentials = function(a, b) { self.get_credentials(a, b); };
		this._smart.get_iframe = function(a, b) { self.get_iframe(a, b); };
		this._smart.handle_api = function(a, b, c, d) { self.handle_api(a, b, c, d); };
	},

	$define : {
		manifest : function(v) {
			this._manifest = v;
		},
		
		context : function(ctx) {
			this._context = ctx;
			
			if (this._app_instance && this._app_instance.iframe) {
				this._app_instance.iframe.src = "about:blank";
				this._app_instance.iframe = null;
			}
			
			this._smart.record_context_changed();
			this.handle_context_changed();
		},
		
		active : function(v) {
			this._active = v;
			
			if (this._active && !this._app_instance)
				this.init_app();
			else 
				this.notify_app(v ? "foregrounded" : "backgrounded");
		},
		
		notify : function(v) {
			this.notify_app(v);
		},
		
		api_result : function(v) {
			var requestId = v.requestId;
			var request = this._requests[requestId];
			delete this._requests[requestId];
			
			if (!v.contentType) {
				v.contentType="application/json";
				v.data = JSON.stringify(v.data);
			}
			
			var error = v.error;
			
			if (v.contentType == "application/raw")
				v = v.data;
			
			if (error)
				request.error(error, v);
			else
				request.success(v);
		}
	},
	
	bind_ : function () {
		this.$supers(SmartContainer, 'bind_', arguments);
		this.init_app();
	},
	
	unbind_ : function () {
		this.notify_app("destroyed");
		this.$supers(SmartContainer, 'unbind_', arguments);
	},
	
	reset_app : function(restart) {
		this._app_instance = null;
		this._requests = {};
		
		if (restart)
			this.init_app();
	},
	
	init_app : function() {
		if (!this._app_instance && this._manifest && this._context && this.$n() && this._context[this._manifest.scope])
			this._smart.launch_app(this._manifest, this._context);
	},
	
	notify_app : function(v) {
		if (v && this._app_instance)
			this._smart.notify_app(this._app_instance, v);
	},
	
	handle_context_changed : function() {
		this.reset_app(this._active);
	},
	
	get_credentials : function (app_instance, callback) {
		/*
		 * Ask for tokens scoped for:
		 * app_instance.manifest.id app_instance.context.user.id
		 * app_instance.context.record.id
		 */
		var params = {};
		params.smart_record_id = app_instance.context.record.id;
		params.smart_app_id=app_instance.manifest.id;
		params.smart_app_uuid=app_instance.uuid;
		params.smart_user_id=app_instance.context.user.id;
		var api_call = {func: "/oauth", params: params};
		this.handle_api(app_instance, api_call, callback, function(a , b) {callback(null);});
	},

	get_iframe : function (app_instance, callback) {
		this._app_instance = app_instance;
	    var n = this.$n();
	    callback(n);
	},

	handle_api : function (app_instance, api_call, callback_success, callback_error) {
		var func = api_call.func;
		
		if (func.match("^/apps/manifests[/]?$")) {
			callback_success(this.package_result([this._manifest]));
			return;
		}
		
		if (func.match("^/apps/.+/manifest[/]?$")) {
			var pcs = func.split("/");
			
			if (this._manifest && this._manifest.id == pcs[2])
				callback_success(this.package_result(this._manifest));
			else
				callback_error(400, this.package_result("Manifest not found", "text/plain"));
			
			return;
		}
		
		var requestId = ++this._request;
		api_call.requestId = requestId;
		var request = {};
		request.success = callback_success;
		request.error = callback_error;
		this._requests[requestId] = request;
		var event = new zk.Event(this, "onHandleAPI", api_call);
		zAu.send(event);
	},
	
	package_result : function (result, contentType) {
		return {data: contentType ? result : JSON.stringify(result), contentType: contentType ? contentType : "application/json"};
	}

});




© 2015 - 2025 Weber Informatics LLC | Privacy Policy