LinkData API

LinkData Global  1.0.0

LinkData API > linkdata > script.js (source view)
Search:
 
Filters
/**
 * The LinkData object is the single global object used by LinkData Library.
 * It contains utility function for read data from linkdata work.
 * @module linkdata
 * @title LinkData Global
 */
if(typeof LinkData === "undefined" || !LinkData) {
	/**
	 * The LinkData global namespace object.
	 * If LinkData is already defined, the existing LinkData object will
	 * not be overwritten so that defined namespaces are preserved.
	 * @class LinkData
	 * @static
	 */
	var LinkData = {};
}

/**
 * Returns work ids with databases related to the application.
 * @method getWorks
 * @return {Array} A array of work ids with related work files.
 * <dl>
 * 	<dd>Example: <code>[&#123;&lt;workId&gt;,[&lt;fileName&gt;,&lt;fileName&gt;...]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getWorks = function() {
	return LinkData.app.api.getWorks();
}

/**
 * Returns name by given rdf work id.
 * @method getWorkName
 * @param {String} workId Id of rdf work. (optional)
 * @return {Array} A array of work name values. 
 * <dl>
 * 	<dd>Example:</dd>
 * 	<dd>If work id exist, <code>[&lt;workName&gt;]</code></dd>
 * 	<dd>If not work id exist, <code>[&#123;&lt;workId&gt;,[&lt;workName&gt;]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getWorkName = function(workId) {
	return LinkData.app.api.getWorkName(workId);
}

/**
 * Returns databases related to the application by given work id.
 * If work id is undefined or null, returns all databases related to the application grouped by work id.
 * @method getFiles
 * @param {String} workId Id of rdf work. (optional)
 * @return {Array} A array of work files values. 
 * <dl>
 * 	<dd>Example:</dd>
 * 	<dd>If work id exist, <code>[&lt;fileName&gt;,&lt;fileName&gt;...]</code></dd>
 * 	<dd>If not work id exist, <code>[&#123;&lt;workId&gt;,[&lt;fileName&gt;,&ltfileName&gt;...]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getFiles = function(workId) {
	return LinkData.app.api.getFiles(workId);
}

/**
 * Returns databases related to the application by given work id and tag name.
 * If work id is undefined or null, returns all databases related to tag name grouped by work id.
 * @method getFilesByTag
 * @param {String} workId Id of rdf work. (optional)
 * @param {String} tagName name of the tag.
 * @return {Array} A array of work files values. 
 * <dl>
 * 	<dd>Example:</dd>
 * 	<dd>If work id exist, <code>[&lt;fileName&gt;,&lt;fileName&gt;...]</code></dd>
 * 	<dd>If not work id exist, <code>[&#123;&lt;workId&gt;,[&lt;fileName&gt;,&ltfileName&gt;...]&#125;,&#123;.....&#125;...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getFilesByTag = function(workId, tagName) {
	return LinkData.app.api.getFilesByTag(workId, tagName);
}

/**
 * Returns subject list of the database.
 * @method getSubjects
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @return {Array} A array of subject url values.
 * <dl>
 * 	<dd>Example: <code>[&lt;subject&gt;,&lt;subject&gt;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getSubjects = function(workId, filename) {
	return LinkData.app.api.getSubjects(workId, filename);
}

/**
 * Returns property list of the database.
 * @method getProperties
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @return {Array} The array of property url values.
 * <dl>
 * 	<dd>Example: <code>[&lt;property&gt;,&lt;property&gt;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getProperties = function(workId, filename) {
	return LinkData.app.api.getProperties(workId, filename);
}

/**
 * Returns object list of the database by subject and property.
 * @method getObjects
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {String} subject Url of subject
 * @param {String} property Url of property
 * @return {Array} The array of object values. 
 * <dl>
 * 	<dd>Example: <code>[&lt;obejctValue&gt;,&lt;obejctValue&gt;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getObjects = function(workId, filename, subject, property) {
	return LinkData.app.api.getObjects(workId, filename, subject, property);
}

/**
 * Returns all triple list of the database.
 * @method getTriples
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @return {Array} A array of all triples.
 * <dl>
 * 	<dd>Example: <code>[&#123;"subject":&lt;subject&gt;,"property":&lt;property&gt;, "object":&lt;object&gt;&#125;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getTriples = function(workId, filename) {
	return LinkData.app.api.getTriples(workId, filename);
}

/**
 * Returns triple list of the database by subject. If subject is undefined or null, then returns all triple list of the database.
 * @method getTriplesBySubject
 * @param {String} workId Id of rdf work
 * @param {String} filename Name of rdf work file
 * @param {String} subject Url of subject. (optional)
 * @return {Array} A array of triples with subject.
 * <dl>
 * 	<dd>Example: <code>[&#123;"subject":&lt;subject&gt;,"property":&lt;property&gt;, "object":&lt;object&gt;&#125;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getTriplesBySubject = function(workId, filename, subject) {
	return LinkData.app.api.getTriplesBySubject(workId, filename, subject);
}

/**
 * Returns triple list of the database by property. If property is undefined or null, then returns all triple list of the database.
 * @method getTriplesByProperty
 * @param {String} workId Id of rdf work.
 * @param {String} filename Name of rdf work file.
 * @param {String} property Url of property. (optional)
 * @return {Array} A array of triples by property.
 * <dl>
 * 	<dd>Example: <code>[&#123;"subject":&lt;subject&gt;,"property":&lt;property&gt;, "object":&lt;object&gt;&#125;,...]</code></dd>
 * </dl>
 * @static
 */
LinkData.getTriplesByProperty = function(workId, filename, property) {
	return LinkData.app.api.getTriplesByProperty(workId, filename, property);
}

/**
 * Returns the subject name from subject url which passed by browser URL.
 * <dl>
 * 	<dd>Example:</dd>
 *  <dd>if Subject URI: <code>http://linkdata.org/resource/rdf1s1243i#ABC</code></dd>
 *  <dd>then Browser URL: <code>http://...?id=ABC&subject=http://linkdata.org/resource/rdf1s1243i#ABC  </code></dd>
 *  <dd>returns Subject Name: <code>ABC</code></dd>
 * </dl>
 * @method getSubjectName
 * @return {String} Subject Name
 * @static
 */
LinkData.getSubjectName = function() {
	return LinkDataUtil.app.api.getParameterByName("id");
}

/**
 * Returns the subject uri which passed by browser URL.
 * <dl>
 * 	<dd>Example:</dd>
 *  <dd>if Subject URI: <code>http://linkdata.org/resource/rdf1s1243i#ABC</code></dd>
 *  <dd>then Browser URL: <code>http://...?id=ABC&subject=http://linkdata.org/resource/rdf1s1243i#ABC  </code></dd>
 *  <dd>returns Subject URI: <code>http://linkdata.org/resource/rdf1s1243i#ABC</code></dd>
 * </dl>
 * @method getSubjectUri
 * @return {String} Subject URI
 * @static
 */
LinkData.getSubjectUri = function() {
	return LinkDataUtil.app.api.getParameterByName("subject");
}


/**
 * 
 * This class contains LinkData application internal API functions
 * Note: {{},{}...} case for (var obj in data) and [{},{}...] for (var i = 0, i < data.length, i++) 
 * @private
 * 
 */

if(typeof LinkData.app === "undefined" || !LinkData.app) {
	LinkData.app = {};
} 

LinkData.app.api = {

	getWorks : function() {
		var result = [];
		var files = this.getFiles();
		for(var key in files) {
			result.push(key);
		}
		return result;
	},
	
	getWorkName : function(workId) {
		var result = [];
		
		var files = this.getFiles();
		for (var key in files) {
			if ( !result[key] ) {
				result[key] = [];
			}
			var rootEl = document.getElementById("applicationWorkFileListContainer");
			var workFileElList = LinkData.getElementsByClassName("applicationWorkFile", "div", rootEl);
			for (var i = 0; i < workFileElList.length; i++) {
				var workFileEl = workFileElList[i];
				var id = LinkData.getElementsByClassName("applicationWorkFileWorkId", "input", workFileEl)[0].value;
				var name = LinkData.getElementsByClassName("applicationWorkFileWorkName", "input", workFileEl)[0].value;
				
				if ( !result[id] || result[id].length == 0) {
					result[id] = [];
					result[id].push(name);
				}
				
			}
		}
		
		if (this._exist(workId)) {
			return result[workId];
		}
		return result;
	},
	
	getFiles : function(workId) {
		var result = new Object();
		
		var rootEl = document.getElementById("applicationWorkFileListContainer");
		var workFileElList = LinkData.getElementsByClassName("applicationWorkFile", "div", rootEl);
		for (var i = 0; i < workFileElList.length; i++) {
			var workFileEl = workFileElList[i];
			var id = LinkData.getElementsByClassName("applicationWorkFileWorkId", "input", workFileEl)[0].value;
			var filename = LinkData.getElementsByClassName("applicationWorkFileName", "input", workFileEl)[0].value;
			
			if ( !result[id] ) {
				result[id] = [];
			}
			
			result[id].push(filename);
		}
	
		if (this._exist(workId)) {
			return result[workId];
		}
		return result;
	},
	
	getFilesByTag : function(workId, tag) {
		var result = new Object();
		var rootEl = document.getElementById("applicationWorkFileListContainer");
		var workFileElList = LinkData.getElementsByClassName("applicationWorkFile", "div", rootEl);
		for (var i = 0; i < workFileElList.length; i++) {
			var workFileEl = workFileElList[i];
			var id = LinkData.getElementsByClassName("applicationWorkFileWorkId", "input", workFileEl)[0].value;
			var filename = LinkData.getElementsByClassName("applicationWorkFileName", "input", workFileEl)[0].value;
			var tagName = LinkData.getElementsByClassName("applicationWorkTag", "input", workFileEl)[0].value;
			var tmpTag = tagName.trim();
			var array = tmpTag.split(","); // for comma separated tag
			for (var j = 0; j < array.length; j++) {
				var t = array[j];
				if (t.trim() == tag.trim()) {
					if ( !result[id] ) {
						result[id] = [];
					}
					result[id].push(filename);					
				}
			}
		}
		if (this._exist(workId)) {
			return result[workId];
		}
		return result;
	},
	
	getSubjects : function(workId, filename) {
		var result = [];
		var json = this._getJson(workId, filename);
		
		for (var subject in json) {
			if (this._exist(subject)) {
				result.push(subject);
			}
		}
		
		return result;
	},
	
	getProperties : function(workId, filename) {
		var result = [];
		var json = this._getJson(workId, filename);

		var properties = [];
		for (var subject in json) {
			if (this._exist(subject)) {
				var propertyList = json[subject];
				for (var property in propertyList) {
					if (this._exist(property) && this._arrContains(properties, property) < 0) {
						properties.push(property);
						result.push(property);
					}
				}
			}
		}
		
		return result;
	},
	
	getObjects : function(workId, filename, subject, property) {
		var result = [];
		var json = this._getJson(workId, filename);
		
		for (var sub in json) {
			if (this._exist(sub) && sub == subject) {
				var propertyList = json[sub];
				for (var prop in propertyList) {
					if (this._exist(prop) && prop == property) {
						var objectList = propertyList[prop];
						for (var i = 0; i < objectList.length; i++) {
							result.push(objectList[i].value);
						}
					}
				}
			}
		}
		
		return result;
	},
	
	getTriples : function(workId, filename) {
		return this._getTriples(workId, filename, null, null);
	},
	
	getTriplesBySubject : function(workId, filename, subject) {
		return this._getTriples(workId, filename, subject, null);
	},
	
	getTriplesByProperty : function(workId, filename, property) {
		return this._getTriples(workId, filename, null, property);
	},
	
	_getTriples : function(workId, filename, subject, property) {
		var result = [];
		var json = this._getJson(workId, filename);
		
		for (var sub in json) {
			if (this._exist(sub) && this._valid(sub, subject)) {
				var propertyList = json[sub];
				for (var prop in propertyList) {
					if (this._exist(prop) && this._valid(prop, property)) {
						var objectList = propertyList[prop];
						for (var i = 0; i < objectList.length; i++) {
							var triple = new Object();
							triple["subject"] = sub;
							triple["property"] = prop;
							triple["object"] = objectList[i].value;
							result.push(triple);
						}
					}
				}
			}
		}
		
		return result;
	},
	
	_arrContains : function(arr, data) {
		for(var i = 0; i < arr.length; i++) {
            if(arr[i] === data) {
                return i;
            }
        }
        return -1;
	},
	
	_exist : function(data) {
		return (data && data.length > 0);
	},
	
	_valid : function(value, param) {
		if (param) {
			return (param == value);
		}
		return true;
	},
	
	_getJson : function(workId, filename) {
		return LinkData.userdata[workId][filename];
	}
};

/**
 * 
 * @private
 * linkdata traversing method by class name
 * 
 */
LinkData.getElementsByClassName = function(classname, tagname, root, fn) {
	if (!root) { 
		root = document.getElementsByTagName('body')[0]; 
	}
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b'); 
	var els = root.getElementsByTagName(tagname);
	if (fn) {
		for (var i = 0, j = els.length; i < j; i++) { 
			if ( re.test(els[i].className) ) { 
				fn(els[i]);
			} 
		}
	} else {
		for (var i = 0, j = els.length; i < j; i++) { 
			if ( re.test(els[i].className) ) { 
				a.push(els[i]); 
			} 
		}
	}
	return a;
};

Copyright © 2014 app.linkdata.org, All rights reserved.