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