1 // The Antville Project 2 // http://code.google.com/p/antville 3 // 4 // Copyright 2007-2011 by Tobi Schäfer. 5 // 6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer, 7 // Matthias & Michael Platzer, Christoph Lincke. 8 // 9 // Licensed under the Apache License, Version 2.0 (the ``License''); 10 // you may not use this file except in compliance with the License. 11 // You may obtain a copy of the License at 12 // 13 // http://www.apache.org/licenses/LICENSE-2.0 14 // 15 // Unless required by applicable law or agreed to in writing, software 16 // distributed under the License is distributed on an ``AS IS'' BASIS, 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 // See the License for the specific language governing permissions and 19 // limitations under the License. 20 // 21 // $Revision$ 22 // $LastChangedBy$ 23 // $LastChangedDate$ 24 // $URL$ 25 26 /** 27 * @fileoverview Defines the FlexiPath prototype. 28 */ 29 30 var FlexiPath = function(name, parent) { 31 var self = this; 32 33 this._id = name; 34 this._parent = parent; 35 this._patterns = {}; 36 37 this.__defineGetter__("patterns", function() { 38 var ref = this; 39 while (ref._parent.constructor === FlexiPath) { 40 ref = ref._parent; 41 } 42 return ref._patterns; 43 }); 44 45 this.addUrlPattern = function(pattern, callback) { 46 this._patterns[pattern] = callback; 47 return; 48 } 49 50 this.href = function(action) { 51 var href = []; 52 var ref = this; 53 while (ref._parent === this.constructor) { 54 href.unshift(ref._id); 55 ref = ref._parent; 56 } 57 //href.push("/"); 58 if (action) { 59 href.push(action); 60 } 61 return this._parent.href() + href.join("/"); 62 } 63 64 this.getChildElement = function(name) { 65 return new this.constructor(name, self); 66 } 67 68 return this; 69 }; 70 71 FlexiPath.prototype.main_action = function() { 72 for (let pattern in this.patterns) { 73 let match; 74 let re = new RegExp(pattern); 75 if (match = req.path.match(re)) { 76 return this.patterns[pattern].apply(this, match); 77 } 78 } 79 return; 80 } 81