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:3341 $
 20 // $LastChangedBy:piefke3000 $
 21 // $LastChangedDate:2007-09-27 00:22:37 +0200 (Thu, 27 Sep 2007) $
 22 // $URL$
 23 //
 24 
 25 /**
 26  * @fileOverview Defines the Stories prototype
 27  */
 28 
 29 /**
 30  * @name Stories
 31  * @constructor
 32  * @property {Story[]} _children
 33  * @property {Tag[]} alphabeticalTags
 34  * @property {Story[]} closed
 35  * @property {Comment[]} comments
 36  * @property {Story[]} featured
 37  * @property {Tag[]} otherTags
 38  * @property {Story[]} recent
 39  * @property {Tag[]} tags
 40  * @property {Story[]} top
 41  * @property {Story[]} union
 42  * @extends HopObject
 43  */
 44 
 45 /**
 46  * 
 47  * @param {String} action
 48  * @returns {Boolean}
 49  */
 50 Stories.prototype.getPermission = function(action) {
 51    if (!this._parent.getPermission("main")) {
 52       return false;
 53    }
 54    switch (action) {
 55       case ".":
 56       case "main":
 57       case "create":
 58       return Site.require(Site.OPEN) && session.user ||
 59             Membership.require(Membership.CONTRIBUTOR) || 
 60             User.require(User.PRIVILEGED); 
 61 
 62       case "all":
 63       case "top":
 64       case "closed":
 65       return Membership.require(Membership.MANAGER) ||
 66             User.require(User.PRIVILEGED);
 67    }
 68    return false;
 69 }
 70 
 71 Stories.prototype.main_action = function() {
 72    var stories = User.getMembership().stories;
 73    res.data.list = renderList(stories, "$Story#listItem", 
 74          10, req.queryParams.page);
 75    res.data.pager = renderPager(stories, 
 76          this.href(), 10, req.queryParams.page);
 77    res.data.title = gettext("Member Stories");
 78    res.data.body = this.renderSkinAsString("$Stories#main");
 79    this._parent.renderSkin("Site#page");
 80    return;
 81 }
 82 
 83 Stories.prototype.create_action = function() {
 84    var story = new Story;
 85    if (req.postParams.save) {
 86       try {
 87          story.update(req.postParams);
 88          this.add(story);
 89          story.notify(req.action);
 90          delete session.data.backup;
 91          res.message = gettext("The story was successfully created.");
 92          res.redirect(story.href());
 93       } catch (ex) {
 94          res.message = ex;
 95          app.log(ex);
 96       }
 97    }
 98    
 99    res.data.title = gettext("Add Story");
100    res.data.action = this.href(req.action);
101    res.data.body = story.renderSkinAsString("Story#edit");
102    this._parent.renderSkin("Site#page");
103    return;
104 }
105 
106 Stories.prototype.closed_action = function() {
107    res.data.list = renderList(this.closed, 
108          "$Story#listItem", 10, req.queryParams.page);
109    res.data.pager = renderPager(this.closed, 
110          this.href(req.action), 10, req.queryParams.page);
111    res.data.title = gettext("Closed Stories");
112    res.data.body = this.renderSkinAsString("$Stories#main");
113    this._parent.renderSkin("Site#page");
114    return;
115 }
116 
117 Stories.prototype.all_action = function() {
118    res.data.list = renderList(this, "$Story#listItem", 10, req.queryParams.page);
119    res.data.pager = renderPager(this, 
120          this.href(req.action), 10, req.queryParams.page);
121    res.data.title = gettext("All Stories");
122    res.data.body = this.renderSkinAsString("$Stories#main");
123    this._parent.renderSkin("Site#page");
124    return;
125 }
126 
127 Stories.prototype.top_action = function() {
128    res.data.title = gettext("Top Stories");
129    res.data.body = this.renderSkinAsString("$Stories#top");
130    this._parent.renderSkin("Site#page");
131    return;
132 }
133 
134 /**
135  * 
136  * @param {Object} param
137  * @param {String} type
138  */
139 Stories.prototype.list_macro = function(param, type) {
140    switch (type) {
141       case "top":
142       var counter = 1;
143       this.top.forEach(function() {
144          this.renderSkin("$Story#top", {
145             position: counter
146          });
147          counter += 1;
148       }); 
149       break;
150    }
151    return;
152 }
153 
154 /**
155  * 
156  * @param {String} group
157  * @returns {Tag[]}
158  * @see Site#getTags
159  */
160 Stories.prototype.getTags = function(group) {
161    return this._parent.getTags("tags", group);
162 }
163 
164 /**
165  * 
166  * @param {String} name
167  * @returns {String[]}
168  */
169 Stories.prototype.getAdminHeader = function(name) {
170    return ["#", "Tag", "Items"];
171 }
172