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