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 // $Author$
 23 // $Date$
 24 // $URL$
 25 
 26 /**
 27  * @fileoverview Defines the Feature prototype.
 28  */
 29 
 30 /**
 31  * 
 32  * @param {Object} param
 33  * @param {String} id
 34  */
 35 global.feature_macro = function(param, id) {
 36    var func, feature = Feature.get(id);
 37    if (feature && (func = feature.main)) {
 38       func.constructor === Function && func(param);
 39    }
 40    return;
 41 }
 42 
 43 /**
 44  *
 45  * @param {String} id
 46  * @param {String} url
 47  * @param {Object} feature
 48  */
 49 var Feature = function(id, url, feature) {
 50    var self = this;
 51 
 52    this.__defineGetter__("id", function() {return id});
 53 
 54    for (let i in feature) {
 55       this[i] = feature[i];
 56    }
 57 
 58    this.toString = function() {
 59       return "[Feature: " + html.linkAsString({href: url}, id) + "]";
 60    }
 61 
 62    return this;
 63 }
 64 
 65 /**
 66  *
 67  * @param {String} id
 68  * @param {String} url
 69  * @param {Object} feature
 70  * @returns {Feature}
 71  */
 72 Feature.add = function(id, url, feature) {
 73    if (!id || !url) {
 74       throw Error("Insufficient arguments");
 75    }
 76 
 77    var existingFeature = Feature.get(id);
 78    if (existingFeature) {
 79       app.log("Warning! Overwriting already present feature with ID " + id);
 80       Feature.remove(existingFeature);
 81    }
 82 
 83    Feature.list().push(new Feature(id, url, feature));
 84    return this;
 85 }
 86 
 87 /**
 88  *
 89  * @param {Feature} feature
 90  * @returns {Number}
 91  */
 92 Feature.remove = function(feature) {
 93    var features = Feature.list();
 94    if (feature === "*") {
 95       features.length = 0;
 96    } else if (feature) {
 97       var index = features.indexOf(feature);
 98       (index > -1) && features.splice(index, 1);
 99    }
100    return features.length;
101 }
102 
103 /**
104  *
105  */
106 Feature.list = function() {
107    return app.data.features;
108 }
109 
110 /**
111  *
112  * @param {String} id
113  * @returns {Feature}
114  */
115 Feature.get = function(id) {
116    for each (let feature in Feature.list()) {
117       if (feature.id === id) {
118          return feature;
119       }
120    }
121    return;
122 }
123 
124 /**
125  * 
126  * @param {String} id
127  * @param {Function} callback
128  * @returns {Object}
129  */
130 Feature.invoke = function(id, callback) {
131    id || (id = "*");
132    if (callback) {
133       var feature, method, result;
134       var args = Array.prototype.slice.call(arguments, 2);
135       if (id === "*") {
136          for each (feature in Feature.list()) {
137             method = feature[String(callback)];
138             if (method && method.constructor === Function) {
139                result = method.apply(feature, args);
140             }
141          }
142       } else {
143          feature = Feature.get(id);
144          if (feature) {
145             if (callback.constructor === Function) {
146                result = callback.apply(feature, args);
147             } else {
148                method = feature[callback];
149                if (method && method.constructor === Function) {
150                   result = method.apply(feature, args);
151                }
152             }
153          }
154       }
155    }
156    return result;
157 }
158 
159 /**
160  *
161  * @param {String} action
162  * @returns {Boolean}
163  */
164 Feature.getPermission = function(action) {
165    for each (let feature in Feature.list()) {
166       let method = feature._getPermission;
167       if (method && method.constructor === Function && method.call(this, action)) {
168          return true;
169       }
170    }
171    return false;
172 }