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 /**
 30  * @name Skins
 31  * @constructor
 32  * @param {String} name
 33  * @param {Skins} parent
 34  * @property {Skin[]} _children
 35  * @property {Skin[]} custom
 36  * @property {Skin[]} modified
 37  * @property {String} name
 38  * @property {Skins} parent
 39  * @extends HopObject
 40  */
 41 Skins.prototype.constructor = function(name, parent) {
 42   this.name = name; 
 43   this.parent = parent;
 44   return this;
 45 }
 46 
 47 /**
 48  * 
 49  * @param {String} action
 50  * @returns {Boolean}
 51  */
 52 Skins.prototype.getPermission = function(action) {
 53    return res.handlers.layout.getPermission("main");
 54 }
 55 
 56 /**
 57  * 
 58  * @param {String} name
 59  * @returns {Skins|Skin}
 60  */
 61 Skins.prototype.getChildElement = function(name) {
 62    if (this.parent) {
 63       var group = path[path.length - 1].name;
 64       var skin = this.getSkin(group, name);
 65       if (skin) {
 66          return skin;
 67       }
 68       if (global[group] || group === "Global") {
 69          return this.getSkin(group, name);         
 70       }
 71    }
 72    return new Skins(name, this);
 73 }
 74 
 75 Skins.prototype.onRequest = function() {
 76    if (this.parent) {
 77       res.redirect(res.handlers.layout.skins.href(req.action));
 78    }
 79    return HopObject.prototype.onRequest.call(this);
 80 }
 81 
 82 Skins.prototype.main_action = function() {
 83    res.data.title = gettext("Basic Skins");
 84    res.data.body = this.renderSkinAsString("$Skins#main");
 85    res.handlers.site.renderSkin("Site#page");
 86    return;
 87 }
 88 
 89 Skins.prototype.create_action = function() {
 90    var skin = this.getSkin(req.postParams.prototype, req.postParams.name);
 91    if (req.postParams.save) {
 92       try {
 93          if (!req.postParams.prototype || !req.postParams.name) {
 94             throw Error(gettext("Please choose a prototype and enter a skin name"));
 95          }
 96          skin.update(req.postParams);
 97          res.message = gettext("The changes were saved successfully.");
 98          if (req.postParams.save == 1) {
 99             res.redirect(skin.href("edit"));
100          } else {
101             res.redirect(res.handlers.layout.skins.href("modified"));
102          }
103       } catch (ex) {
104          res.message = ex;
105          app.log(ex);
106       }
107    }
108    if (skin.getSource()) {
109       res.redirect(skin.href("edit")); // FIXME: Needs testing
110       //res.data.title = gettext("Edit skin: {0}.{1}", skin.prototype, skin.name);
111    } else {
112       res.data.title = gettext('Add Skin');
113    }
114    res.data.action = this.href(req.action);
115    res.data.body = skin.renderSkinAsString("$Skin#edit");
116    this.renderSkin("$Skins#page");
117    return;
118 }
119 
120 Skins.prototype.modified_action = function() {
121    res.data.title = gettext("Modified Skins");
122    res.push();
123    this.renderSkin("$Skins#header");
124    this.modified.forEach(function() {
125       this.renderSkin("$Skin#listItem");
126    });
127    res.data.body = res.pop();
128    res.handlers.site.renderSkin("Site#page");
129    return;
130 }
131 
132 Skins.prototype.all_action = function() {
133    if (this.parent) {
134       res.redirect(res.handlers.layout.skins.href(req.action));
135    }
136    res.data.list = this.getOutline();
137    res.data.title = gettext("All Skins");
138    res.data.body = this.renderSkinAsString("$Skins#all");
139    res.handlers.site.renderSkin("Site#page");
140    return;
141 }
142 
143 Skins.prototype.safe_action = function() {
144    res.data.title = gettext("Modified Skins");
145    res.push();
146    this.modified.forEach(function() {
147       this.renderSkin("$Skin#listItem");
148    });
149    res.data.title = "Modified Skins";
150    res.data.body = res.pop();
151    this.renderSkin("$Skins#page");
152    return;
153 }
154 
155 /**
156  * 
157  * @param {String} group
158  * @param {String} name
159  * @returns {Skin}
160  */
161 Skins.prototype.getSkin = function(group, name) {
162    return Skin.getByName(group, name) || new Skin(group, name);
163 }
164 
165 /**
166  * 
167  * @param {String} type
168  * @returns {String}
169  */
170 Skins.prototype.getOutline = function(type) {
171    var key = "outline:" + type;
172    var outline = this.cache[key];
173    if (outline) {
174       return outline;
175    }
176 
177    var prototype, skin, subskins, names, skins = [];
178    var options = Skin.getPrototypeOptions();
179 
180    for each (var option in options) {
181       prototype = option.value;
182       names = [];
183       for (var name in app.skinfiles[prototype]) {
184          if (name === prototype && type !== "custom") {
185             skin = createSkin(app.skinfiles[prototype][name]);
186             subskins = skin.getSubskinNames();
187             for each (var subskin in subskins) {
188                names.push(subskin);
189             }
190          } else if (name !== prototype && type === "custom") {
191             names.push(name);
192          }
193       }
194       names.sort();
195       skins.push([prototype, names]);
196    }
197 
198    res.push();
199    for each (var item in skins) {
200       prototype = item[0];
201       skin = item[1];
202       if (skin && skin.length > 0) {
203          html.openTag("li");
204          html.openTag("a", {href: "#", name: prototype, id: prototype});
205          res.write(prototype);
206          html.closeTag("a");
207          html.openTag("ul");
208          for each (var name in skin) {
209             subskin = this.getSkin(prototype, name);
210             html.openTag("li");
211             html.link({href: subskin.href("edit")}, 
212                   subskin.prototype + "." + subskin.name);
213             html.closeTag("li");
214          }
215          html.closeTag("ul");
216          html.closeTag("li");
217       }
218    }
219    return this.cache[key] = res.pop();
220 }
221