1 //
  2 // The Antville Project
  3 // http://code.google.com/p/antville
  4 //
  5 // Copyright 2001-2007 by The Antville People
  6 //
  7 // Licensed under the Apache License, Version 2.0 (the ``License'');
  8 // you may not use this file except in compliance with the License.
  9 // You may obtain a copy of the License at
 10 //
 11 //    http://www.apache.org/licenses/LICENSE-2.0
 12 //
 13 // Unless required by applicable law or agreed to in writing, software
 14 // distributed under the License is distributed on an ``AS IS'' BASIS,
 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16 // See the License for the specific language governing permissions and
 17 // limitations under the License.
 18 //
 19 // $Revision:3337 $
 20 // $LastChangedBy:piefke3000 $
 21 // $LastChangedDate:2007-09-21 15:54:30 +0200 (Fri, 21 Sep 2007) $
 22 // $URL$
 23 //
 24 
 25 /**
 26  * @fileOverview Defines the Skins prototype.
 27  */
 28 
 29 markgettext("Skins");
 30 markgettext("skins");
 31 
 32 /**
 33  * @name Skins
 34  * @constructor
 35  * @param {String} name
 36  * @param {Skins} parent
 37  * @property {Skin[]} _children
 38  * @property {Skin[]} custom
 39  * @property {Skin[]} modified
 40  * @property {String} name
 41  * @property {Skins} parent
 42  * @extends HopObject
 43  */
 44 Skins.prototype.constructor = function(name, parent) {
 45   this.name = name; 
 46   this.parent = parent;
 47   return this;
 48 }
 49 
 50 /**
 51  * 
 52  * @param {String} action
 53  * @returns {Boolean}
 54  */
 55 Skins.prototype.getPermission = function(action) {
 56    return res.handlers.layout.getPermission("main");
 57 }
 58 
 59 /**
 60  * 
 61  * @param {String} name
 62  * @returns {Skins|Skin}
 63  */
 64 Skins.prototype.getChildElement = function(name) {
 65    if (this.parent) {
 66       var group = path[path.length - 1].name;
 67       var skin = this.getSkin(group, name);
 68       if (skin) {
 69          return skin;
 70       }
 71       if (global[group] || group === "Global") {
 72          return this.getSkin(group, name);         
 73       }
 74    }
 75    return new Skins(name, this);
 76 }
 77 
 78 Skins.prototype.onRequest = function() {
 79    if (this.parent) {
 80       res.redirect(res.handlers.layout.skins.href(req.action));
 81    }
 82    return HopObject.prototype.onRequest.call(this);
 83 }
 84 
 85 Skins.prototype.main_action = function() {
 86    res.data.title = gettext("Basic Skins");
 87    res.data.body = this.renderSkinAsString("$Skins#main");
 88    res.handlers.site.renderSkin("Site#page");
 89    return;
 90 }
 91 
 92 Skins.prototype.create_action = function() {
 93    var skin = new Skin; 
 94    if (req.postParams.save) {
 95       try {
 96          var prototype = req.postParams.prototype;
 97          var name = stripTags(req.postParams.name);
 98          if (!prototype || !req.postParams.name) {
 99             throw Error(gettext("Please choose a prototype and enter a skin name"));
100          } else if (name !== req.postParams.name || /\s/.test(name) || NAMEPATTERN.test(name)) {
101             throw Error(gettext("Please avoid special characters or HTML code in the name field."));
102          } else if (Skin.getByName(prototype, name)) {
103             throw Error("Sorry, there is already a skin with that name. Please enter a different one.");
104          }
105          skin = this.getSkin(prototype, name);
106          skin.update(req.postParams);
107          res.message = gettext("The changes were saved successfully.");
108          if (req.postParams.save == 1) {
109             res.redirect(skin.href("edit"));
110          } else {
111             res.redirect(res.handlers.layout.skins.href("modified"));
112          }
113       } catch (ex) {
114          res.message = ex;
115          app.log(ex);
116       }
117    }
118    res.data.title = gettext('Add Skin');
119    res.data.action = this.href(req.action);
120    res.data.body = skin.renderSkinAsString("$Skin#edit");
121    this.renderSkin("$Skins#page");
122    return;
123 }
124 
125 Skins.prototype.modified_action = function() {
126    res.data.title = gettext("Modified Skins");
127    res.push();
128    this.renderSkin("$Skins#header");
129    this.modified.forEach(function() {
130       this.renderSkin("$Skin#listItem");
131    });
132    res.data.body = res.pop();
133    res.handlers.site.renderSkin("Site#page");
134    return;
135 }
136 
137 Skins.prototype.all_action = function() {
138    if (this.parent) {
139       res.redirect(res.handlers.layout.skins.href(req.action));
140    }
141    res.data.list = this.getOutline();
142    res.data.title = gettext("All Skins");
143    res.data.body = this.renderSkinAsString("$Skins#all");
144    res.handlers.site.renderSkin("Site#page");
145    return;
146 }
147 
148 Skins.prototype.safe_action = function() {
149    res.data.title = gettext("Modified Skins");
150    res.push();
151    this.modified.forEach(function() {
152       this.renderSkin("$Skin#listItem");
153    });
154    res.data.title = "Modified Skins";
155    res.data.body = res.pop();
156    this.renderSkin("$Skins#page");
157    return;
158 }
159 
160 /**
161  * 
162  * @param {String} group
163  * @param {String} name
164  * @returns {Skin}
165  */
166 Skins.prototype.getSkin = function(group, name) {
167    return Skin.getByName(group, name) || new Skin(group, name);
168 }
169 
170 /**
171  * 
172  * @param {String} type
173  * @returns {String}
174  */
175 Skins.prototype.getOutline = function(type) {
176    var key = "outline:" + type;
177    var outline = this.cache[key];
178    if (outline) {
179       return outline;
180    }
181 
182    var prototype, skin, subskins, names, skins = [];
183    var options = Skin.getPrototypeOptions();
184 
185    for each (var option in options) {
186       prototype = option.value;
187       names = [];
188       for (var name in app.skinfiles[prototype]) {
189          if (name === prototype && type !== "custom") {
190             skin = createSkin(app.skinfiles[prototype][name]);
191             subskins = skin.getSubskinNames();
192             for each (var subskin in subskins) {
193                names.push(subskin);
194             }
195          } else if (name !== prototype && type === "custom") {
196             names.push(name);
197          }
198       }
199       names.sort();
200       skins.push([prototype, names]);
201    }
202 
203    res.push();
204    for each (var item in skins) {
205       prototype = item[0];
206       skin = item[1];
207       if (skin && skin.length > 0) {
208          html.openTag("li");
209          html.openTag("a", {href: "#", name: prototype, id: prototype});
210          res.write(prototype);
211          html.closeTag("a");
212          html.openTag("ul");
213          for each (var name in skin) {
214             subskin = this.getSkin(prototype, name);
215             html.openTag("li");
216             html.link({href: subskin.href("edit")}, 
217                   subskin.prototype + "." + subskin.name);
218             html.closeTag("li");
219          }
220          html.closeTag("ul");
221          html.closeTag("li");
222       }
223    }
224    return this.cache[key] = res.pop();
225 }
226