1 // The Antville Project
  2 // http://code.google.com/p/antville
  3 //
  4 // Copyright 2007-2011 by Tobi Schäfer.
  5 //
  6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
  7 // Matthias & Michael Platzer, Christoph Lincke.
  8 //
  9 // Licensed under the Apache License, Version 2.0 (the ``License'');
 10 // you may not use this file except in compliance with the License.
 11 // You may obtain a copy of the License at
 12 //
 13 //    http://www.apache.org/licenses/LICENSE-2.0
 14 //
 15 // Unless required by applicable law or agreed to in writing, software
 16 // distributed under the License is distributed on an ``AS IS'' BASIS,
 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18 // See the License for the specific language governing permissions and
 19 // limitations under the License.
 20 //
 21 // $Revision$
 22 // $LastChangedBy$
 23 // $LastChangedDate$
 24 // $URL$
 25 
 26 /**
 27  * @fileOverview Defines the Choice prototype.
 28  */
 29 
 30 markgettext("Choice");
 31 markgettext("choice");
 32 
 33 /**
 34  * 
 35  */
 36 Choice.remove = function() {
 37    if (this.constructor === Choice) {
 38       HopObject.remove.call(this);
 39       this.remove();
 40    }
 41    return;
 42 }
 43 
 44 /**
 45  * @name Choice
 46  * @constructor
 47  * @param {Object} title
 48  * @property {Vote[]} _children
 49  * @property {Date} created
 50  * @property {Date} modified
 51  * @property {Poll} poll
 52  * @property {String} title
 53  * @extends HopObject
 54  */
 55 Choice.prototype.constructor = function(title) {
 56    this.title = title;
 57    this.created = this.modified = new Date;
 58    return this;
 59 }
 60 
 61 /**
 62  * 
 63  */
 64 Choice.prototype.selected_macro = function() {
 65    var votes;
 66    if (session.user && (votes = this._parent.votes.get(session.user.name))) {
 67       res.write(this === votes.choice);
 68    } else {
 69       res.write(false);
 70    }
 71    return;
 72 }
 73 
 74 /**
 75  * 
 76  * @param {Object} param
 77  * @param {String} variant
 78  */
 79 Choice.prototype.votes_macro = function(param, variant) {
 80    var votes = 0;
 81    if (variant) {
 82       if (variant.endsWith("%")) {
 83          variant = parseInt(variant) || 1;
 84          var max = this._parent.votes.size();
 85          votes = this.size() / max * variant;
 86       } else {
 87          var max = 1;
 88          this._parent.forEach(function() {
 89             var n = this.size();
 90             if (n > max) {
 91                max = n;
 92             }
 93             return;
 94          });
 95          votes = Math.round(this.size() / max * variant);
 96       }
 97    } else {
 98       votes = this.size();
 99    }
100    if (!votes && param["default"]) {
101       return param["default"];
102    }
103    return votes;
104 }
105