[903] | 1 | /* -------------------------------------------------------------------------- * |
---|
| 2 | * Experience Javascript Library (version 1.0b) |
---|
| 3 | * (c) 2007 Ahmed Saad <ahmeds@users.sourceforge.net> |
---|
| 4 | * |
---|
| 5 | * Experience is freely distributable under the terms of an MIT-style license. |
---|
| 6 | * For details, see project website at http://experience.sf.net |
---|
| 7 | * -------------------------------------------------------------------------- */ |
---|
| 8 | |
---|
| 9 | // Cloudy component |
---|
| 10 | |
---|
| 11 | experience.cloudy = { |
---|
| 12 | |
---|
| 13 | Version : '0.1', |
---|
| 14 | |
---|
| 15 | WeightedList : function (userParams){ |
---|
| 16 | |
---|
| 17 | this.Params = $H({ |
---|
| 18 | MinFontSize : 8, |
---|
| 19 | MaxFontSize : 40, |
---|
| 20 | Unit : "px", |
---|
| 21 | Items : null, // an array of items |
---|
| 22 | RenderIn: null, // an element or id |
---|
| 23 | ItemCSSClass: "cloudyItem", |
---|
| 24 | TipCSSClass: "cloudyTip" |
---|
| 25 | }); |
---|
| 26 | |
---|
| 27 | //// PUBLIC METHODS //////////////////////////////////////////////// |
---|
| 28 | |
---|
| 29 | this.render = experience.cloudy.render; |
---|
| 30 | |
---|
| 31 | //// INITIALIZATION /////////////////////////////////////////////// |
---|
| 32 | |
---|
| 33 | this.Params.merge(userParams); |
---|
| 34 | }, |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * Renders the weighted list inside an element (ie a <div>) |
---|
| 38 | * Parameters: |
---|
| 39 | * arr: A numerically indexed array of hash-like objects |
---|
| 40 | * representing items that has the following properties: |
---|
| 41 | * label: the label of the item (ie tag name) |
---|
| 42 | * weight: a number indicating item weight |
---|
| 43 | * href: where to link the item |
---|
| 44 | * html: (optional) a piece of html code (or just text) to |
---|
| 45 | * display when hovering over the item |
---|
| 46 | * handlers: (optional) an object-like hash of event names and |
---|
| 47 | * their handlers |
---|
| 48 | **/ |
---|
| 49 | |
---|
| 50 | render : function(){ |
---|
| 51 | |
---|
| 52 | if (this.Params.Items == null){ |
---|
| 53 | throw new Error ("No Items array was specified in the Items parameter"); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | if (this.Params.RenderIn == null){ |
---|
| 57 | throw new Error ("No element (or id) was specified in the RenderIn parameter"); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | var arr = this.Params.Items; |
---|
| 61 | var min, max, fix; |
---|
| 62 | |
---|
| 63 | for(var i = 0; i < arr.length; i++){ |
---|
| 64 | max = (arr[i]["weight"] > max || max == null)? arr[i]["weight"] : max; |
---|
| 65 | min = (arr[i]["weight"] < min || min == null)? arr[i]["weight"] : min; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | var fix = max - min == 0? 1 : max - min; |
---|
| 69 | |
---|
| 70 | for(var i = 0; i < arr.length; i++){ |
---|
| 71 | var ratio = (1 - (max - arr[i]["weight"]) / fix); |
---|
| 72 | var fontSize = this.Params.MinFontSize + (ratio * (this.Params.MaxFontSize - this.Params.MinFontSize)) ; |
---|
| 73 | |
---|
| 74 | var a = document.createElement('a'); |
---|
| 75 | a.href = arr[i]['href']? arr[i]['href'] : '#' ; |
---|
| 76 | a.className = this.Params.ItemCSSClass; |
---|
| 77 | a.style.fontSize = fontSize + this.Params.Unit; |
---|
| 78 | a.innerHTML = arr[i]["label"]; |
---|
| 79 | |
---|
| 80 | if(typeof(arr[i]["html"]) != 'undefined'){ |
---|
| 81 | experience.Core.bindTip(a, arr[i]["html"], this.Params.TipCSSClass ); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | if (arr[i]["handlers"]){ |
---|
| 85 | for(eventName in arr[i]["handlers"]){ |
---|
| 86 | Event.observe(a, eventName, arr[i]["handlers"][eventName]); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | $(this.Params.RenderIn).appendChild(a); |
---|
| 91 | $(this.Params.RenderIn).appendChild(document.createTextNode(' ')); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|