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