| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | /********************************************************************** | 
					
						
							|  |  |  | *  | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | * | 
					
						
							|  |  |  | **********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //var DEBUG = DEBUG != null ? DEBUG : true
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*********************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // data store...
 | 
					
						
							|  |  |  | // Format:
 | 
					
						
							|  |  |  | // 	{
 | 
					
						
							|  |  |  | // 		<path>: {
 | 
					
						
							|  |  |  | // 			text: <text>,
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // 			links: [
 | 
					
						
							|  |  |  | // 				<offset>: <link>,
 | 
					
						
							|  |  |  | // 			],
 | 
					
						
							|  |  |  | // 		}
 | 
					
						
							|  |  |  | // 	}
 | 
					
						
							|  |  |  | var data = { | 
					
						
							| 
									
										
										
										
											2016-07-06 17:30:51 +03:00
										 |  |  | 	'Templates/DefaultPage': { | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 		text: 'This page is empty.<br><br>WikiHome', | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | var path2lst = path =>  | 
					
						
							|  |  |  | 	(path instanceof Array ?  | 
					
						
							|  |  |  | 	 		path  | 
					
						
							|  |  |  | 			: path.split(/[\\\/]+/g)) | 
					
						
							|  |  |  | 		.map(p => p.trim()) | 
					
						
							|  |  |  | 		.filter(p =>  | 
					
						
							|  |  |  | 			p != '' && p != '.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var normalizePath = path =>  | 
					
						
							|  |  |  | 	path2lst(path).join('/') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | var Wiki = { | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	__wiki_data: data, | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 	__home_page__: 'WikiHome', | 
					
						
							|  |  |  | 	__default_page__: 'DefaultPage', | 
					
						
							| 
									
										
										
										
											2016-07-06 17:30:51 +03:00
										 |  |  | 	__templates__: 'Templates', | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	// current location...
 | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	get location(){ | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 		return this.__location || this.__home_page__ }, | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	set location(value){ | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 		this.__location = normalizePath(value) }, | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	// page path...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// Format:
 | 
					
						
							|  |  |  | 	// 	<dir>/<title>
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// NOTE: changing this will move the page to the new path and change
 | 
					
						
							|  |  |  | 	// 		.location acordingly...
 | 
					
						
							|  |  |  | 	// NOTE: same applies to path parts below...
 | 
					
						
							|  |  |  | 	get path(){  | 
					
						
							|  |  |  | 		return this.location }, | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	set path(value){ | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 		var l = this.location | 
					
						
							|  |  |  | 		value = normalizePath(value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this.__wiki_data[value] = this.__wiki_data[l] || {} | 
					
						
							|  |  |  | 		this.location = value | 
					
						
							|  |  |  | 		delete this.__wiki_data[l] | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	// path parts: directory...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// NOTE: see .path for details...
 | 
					
						
							|  |  |  | 	get dir(){ | 
					
						
							|  |  |  | 		return path2lst(this.location).slice(0, -1).join('/') }, | 
					
						
							|  |  |  | 	set dir(value){ | 
					
						
							|  |  |  | 		this.path = value +'/'+ this.title }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// path parts: title...
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// NOTE: see .path for details...
 | 
					
						
							|  |  |  | 	get title(){  | 
					
						
							|  |  |  | 		return path2lst(this.location).pop() }, | 
					
						
							|  |  |  | 	set title(value){ | 
					
						
							|  |  |  | 		this.path = this.dir +'/'+ value }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// page content...
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-07-07 05:05:35 +03:00
										 |  |  | 	// Test acquesition order:
 | 
					
						
							|  |  |  | 	// 	- explicit path
 | 
					
						
							|  |  |  | 	// 	- .title in path
 | 
					
						
							|  |  |  | 	// 	- .title in templates
 | 
					
						
							|  |  |  | 	// 	- aquire default page (same order as above)
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	get text(){ | 
					
						
							| 
									
										
										
										
											2016-07-07 05:05:35 +03:00
										 |  |  | 		// get the page directly...
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 		return (this.__wiki_data[this.location] || {}).text | 
					
						
							| 
									
										
										
										
											2016-07-07 05:05:35 +03:00
										 |  |  | 			// acquire the page from path...
 | 
					
						
							|  |  |  | 			|| (this.acquire(this.title) || {}).text | 
					
						
							|  |  |  | 			// acquire the default page...
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 			|| (this.acquire(this.__default_page__) || {}).text | 
					
						
							| 
									
										
										
										
											2016-07-07 05:05:35 +03:00
										 |  |  | 			// nothing found...
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 			|| ''  | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	set text(value){ | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 		var l = this.location | 
					
						
							|  |  |  | 		this.__wiki_data[l] = this.__wiki_data[l] || {} | 
					
						
							|  |  |  | 		this.__wiki_data[l].text = value | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	// XXX
 | 
					
						
							|  |  |  | 	get links(){ | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 	set links(value){ | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// XXX
 | 
					
						
							|  |  |  | 	get list(){ | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	set list(value){ | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 	exists: function(path){ | 
					
						
							|  |  |  | 		return normalizePath(path) in this.__wiki_data }, | 
					
						
							|  |  |  | 	// get title from dir and then go up the tree...
 | 
					
						
							|  |  |  | 	acquire: function(title){ | 
					
						
							|  |  |  | 		title = title || this.__default_page__ | 
					
						
							|  |  |  | 		var templates = this.__templates__ | 
					
						
							|  |  |  | 		var data = this.__wiki_data | 
					
						
							|  |  |  | 		var that = this | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 		var path = path2lst(this.dir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var _res = function(p){ | 
					
						
							|  |  |  | 			return { | 
					
						
							|  |  |  | 				dir: normalizePath(p.slice(0, -1)), | 
					
						
							|  |  |  | 				title: title, | 
					
						
							|  |  |  | 				text: that.__wiki_data[normalizePath(p)].text | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		while(true){ | 
					
						
							|  |  |  | 			// get title from path...
 | 
					
						
							|  |  |  | 			var p = path.concat([title]) | 
					
						
							|  |  |  | 			if(this.exists(p)){ | 
					
						
							|  |  |  | 				return _res(p) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// get title from templates in path...
 | 
					
						
							|  |  |  | 			var p = path.concat([templates, title]) | 
					
						
							|  |  |  | 			if(this.exists(p)){ | 
					
						
							|  |  |  | 				return _res(p) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if(path.length == 0){ | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			path.pop() | 
					
						
							| 
									
										
										
										
											2016-07-04 20:42:46 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2016-07-05 16:58:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// serialization...
 | 
					
						
							|  |  |  | 	json: function(path){ | 
					
						
							|  |  |  | 		return path == null ? JSON.parse(JSON.stringify(this.__wiki_data)) | 
					
						
							|  |  |  | 			: path == '.' ? { | 
					
						
							|  |  |  | 					path: this.location, | 
					
						
							|  |  |  | 					text: this.text, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			: { | 
					
						
							|  |  |  | 				path: path, | 
					
						
							|  |  |  | 				text: (this.__wiki_data[path] || {}).text, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	// XXX should we inherit from the default???
 | 
					
						
							|  |  |  | 	load: function(json){ | 
					
						
							|  |  |  | 		this.__wiki_data = json | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2016-07-04 19:51:37 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /********************************************************************** | 
					
						
							|  |  |  | * vim:set ts=4 sw=4 :                                                */ |