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