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 Choice prototype.
 27  */
 28 
 29 /**
 30  * 
 31  */
 32 Choice.remove = function() {
 33    if (this.constructor === Choice) {
 34       HopObject.remove.call(this);
 35       this.remove();
 36    }
 37    return;
 38 }
 39 
 40 /**
 41  * @name Choice
 42  * @constructor
 43  * @param {Object} title
 44  * @property {Vote[]} _children
 45  * @property {Date} created
 46  * @property {Date} modified
 47  * @property {Poll} poll
 48  * @property {String} title
 49  * @extends HopObject
 50  */
 51 Choice.prototype.constructor = function(title) {
 52    this.title = title;
 53    this.created = this.modified = new Date;
 54    return this;
 55 }
 56 
 57 /**
 58  * 
 59  */
 60 Choice.prototype.selected_macro = function() {
 61    var votes;
 62    if (session.user && (votes = this._parent.votes.get(session.user.name))) {
 63       res.write(this === votes.choice);
 64    } else {
 65       res.write(false);
 66    }
 67    return;
 68 }
 69 
 70 /**
 71  * 
 72  * @param {Object} param
 73  * @param {String} variant
 74  */
 75 Choice.prototype.votes_macro = function(param, variant) {
 76    var votes = 0;
 77    if (variant) {
 78       if (variant.endsWith("%")) {
 79          variant = parseInt(variant) || 1;
 80          var max = this._parent.votes.size();
 81          votes = this.size() / max * variant;
 82       } else {
 83          var max = 1;
 84          this._parent.forEach(function() {
 85             var n = this.size();
 86             if (n > max) {
 87                max = n;
 88             }
 89             return;
 90          });
 91          votes = Math.round(this.size() / max * variant);
 92       }
 93    } else {
 94       votes = this.size();
 95    }
 96    if (!votes && param["default"]) {
 97       return param["default"];
 98    }
 99    return votes;
100 }
101