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