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