      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 Tags prototype.
     28  */
     29
     30 markgettext("Tags");
     31 markgettext("tags");
     32
     33 /** @constant */
     34 Tags.ALL = "all";
     35 /** @constant */
     36 Tags.OTHER = "other";
     37 /** @constant */
     38 Tags.ALPHABETICAL = "alphabetical";
     39
     40 /**
     41  * @name Tags
     42  * @constructor
     43  * @extends HopObject
     44  */
     45
     46 /**
     47  *
     48  * @param {String} action
     49  * @returns {Boolean}
     50  */
     51 Tags.prototype.getPermission = function(action) {
     52    return res.handlers.site.getPermission("main");
     53 }
     54
     55 Tags.prototype.main_action = function() {
     56    var action = this.getAction();
     57    if (req.data.group) {
     58       this.setGroup(req.data.group)
     59       res.redirect(this.href(action));
     60    }
     61    if (req.data.page) {
     62       this.setPage(req.data.page);
     63       res.redirect(this.href(action));
     64    }
     65    res.data.title = this.getTitle();
     66    res.data.body = this.renderSkinAsString("$Tags#" + req.action);
     67    res.handlers.site.renderSkin("Site#page");
     68    return;
     69 }
     70
     71 Tags.prototype.admin_action = function() {
     72    return this.main_action();
     73 }
     74
     75 /**
     76  *
     77  * @param {Number} id
     78  * @returns {HopObject}
     79  */
     80 Tags.prototype.getChildElement = function(id) {
     81    var child = this[id] || this.get(Tags.ALL).get(id);
     82    return child;
     83 }
     84
     85 /**
     86  *
     87  */
     88 Tags.prototype.alphabet_macro = function() {
     89    if (this.get(Tags.ALL).size() < 50) {
     90       return;
     91    }
     92
     93    var self = this;
     94    var collection = this.get(Tags.ALPHABETICAL);
     95    var prefix = "?group=";
     96    var group = this.getGroup();
     97
     98    var add = function(text, id) {
     99       if (group === id) {
    100          res.write(text);
    101       } else {
    102          html.link({href: self.href(self.getAction()) + prefix + id}, text);
    103       }
    104       res.write(" ");
    105       return;
    106    };
    107
    108    add("*", Tags.ALL);
    109    collection.forEach(function() {
    110       add(this._id, this._id);
    111    });
    112    if (this.get(Tags.OTHER).size() > 0) {
    113       add("?", Tags.OTHER);
    114    }
    115    return;
    116 }
    117
    118 /**
    119  *
    120  */
    121 Tags.prototype.pager_macro = function() {
    122    var page = this.getPage();
    123    var max = this.get(this.getGroup()).size();
    124    var size = this.getPageSize();
    125    var total = Math.ceil(max / size);
    126    if (total < 2) {
    127       return;
    128    }
    129    var prefix = "?page=";
    130    for (var i=1; i<=total; i+=1) {
    131       if (i == page) {
    132          res.write(i);
    133       } else {
    134          html.link({href: this.href(this.getAction()) + prefix + i}, i);
    135       }
    136       res.write(" ");
    137    }
    138    return;
    139 }
    140
    141 /**
    142  *
    143  * @param {Object} param
    144  */
    145 Tags.prototype.header_macro = function(param) {
    146    var header = this.getHeader();
    147    for each (var title in header) {
    148       this.renderSkin("Tags#header", {title: title});
    149    }
    150    return;
    151 }
    152
    153 /**
    154  *
    155  */
    156 Tags.prototype.list_macro = function(param, skin) {
    157    var page = this.getPage();
    158    var size = param.limit ? Math.min(param.limit, 100) : this.getPageSize();
    159    var start = (page - 1) * size;
    160    var collection = this.get(this.getGroup()).list(start, size);
    161    // FIXME: ListRenderer should do this
    162    //var list = new jala.ListRenderer(collection);
    163    //list.render(skin || mgrlistitem);
    164    var index = start + 1;
    165    for each (var item in collection) {
    166       // FIXME: Is there a more elegant solution?
    167       if (item.constructor !== Tag) {
    168          item = item.get(0);
    169       }
    170       item.renderSkin(skin || "$Tag#listItem", {index: index});
    171       index += 1;
    172    }
    173    return;
    174 }
    175
    176 /**
    177  *
    178  * @param {String} group
    179  * @returns {TagHub[]}
    180  */
    181 Tags.prototype.get = function(group) {
    182    return this._parent.getTags(this._id, group || this.getGroup());
    183 }
    184
    185 /**
    186  * @returns {String}
    187  */
    188 Tags.prototype.getGroup = function() {
    189    return decodeURIComponent(session.data[this.href("group")] || Tags.ALL);
    190 }
    191
    192 /**
    193  *
    194  * @param {String} group
    195  */
    196 Tags.prototype.setGroup = function(group) {
    197    session.data[this.href("group")] = encodeURIComponent(group);
    198    this.setPage(1);
    199    return;
    200 }
    201
    202 /**
    203  * @returns {Number}
    204  */
    205 Tags.prototype.getPage = function() {
    206    return session.data[this.href("page")] || 1;
    207 }
    208
    209 /**
    210  *
    211  * @param {Number} page
    212  */
    213 Tags.prototype.setPage = function(page) {
    214    session.data[this.href("page")] = page;
    215    return;
    216 }
    217
    218 /**
    219  * @returns {Number}
    220  */
    221 Tags.prototype.getPageSize = function() {
    222    return 25;
    223 }
    224
    225 /**
    226  * @returns {String}
    227  */
    228 Tags.prototype.getAction = function() {
    229    return (req.action === "main" ? String.EMPTY : req.action);
    230 }
    231
    232 /**
    233  * @returns {String[]}
    234  * @see Stories#getAdminHeader
    235  * @see Images#getAdminHeader
    236  */
    237 Tags.prototype.getHeader = function() {
    238    if (this._parent.getAdminHeader) {
    239       return this._parent.getAdminHeader(this._id) || [];
    240    }
    241    return [];
    242 }
    243