| 1 |
dojo.provide("dojox.widget.SortList"); |
| 2 |
dojo.experimental("dojox.widget.SortList"); |
| 3 |
|
| 4 |
dojo.require("dijit.layout._LayoutWidget"); |
| 5 |
dojo.require("dijit._Templated"); |
| 6 |
|
| 7 |
dojo.declare("dojox.widget.SortList", |
| 8 |
[dijit.layout._LayoutWidget, dijit._Templated], |
| 9 |
{ |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
title: "", |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
heading: "", |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
descending: true, |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
selected: null, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
sortable: true, |
| 36 |
|
| 37 |
|
| 38 |
store: "", |
| 39 |
key: "name", |
| 40 |
|
| 41 |
baseClass: "dojoxSortList", |
| 42 |
|
| 43 |
templateString: dojo.cache("dojox.widget","SortList/SortList.html"), |
| 44 |
|
| 45 |
_addItem: function(item){ |
| 46 |
dojo.create("li", { |
| 47 |
innerHTML: this.store.getValue(item, this.key).replace(/</g, "<") |
| 48 |
}, this.containerNode); |
| 49 |
}, |
| 50 |
|
| 51 |
postCreate: function(){ |
| 52 |
if(this.store){ |
| 53 |
this.store = dojo.getObject(this.store); |
| 54 |
var props = { |
| 55 |
onItem: dojo.hitch(this, "_addItem"), |
| 56 |
onComplete: dojo.hitch(this, "onSort") |
| 57 |
}; |
| 58 |
this.store.fetch(props); |
| 59 |
}else{ this.onSort(); } |
| 60 |
this.inherited(arguments); |
| 61 |
}, |
| 62 |
|
| 63 |
startup: function(){ |
| 64 |
this.inherited(arguments); |
| 65 |
if(this.heading){ |
| 66 |
this.setTitle(this.heading); |
| 67 |
this.title = this.heading; |
| 68 |
} |
| 69 |
|
| 70 |
setTimeout(dojo.hitch(this,"resize"), 5); |
| 71 |
if(this.sortable){ this.connect(this.titleNode,"onclick", "onSort"); } |
| 72 |
}, |
| 73 |
|
| 74 |
resize: function(){ |
| 75 |
|
| 76 |
|
| 77 |
this.inherited(arguments); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
var offset = ((this._contentBox.h) - (dojo.style(this.titleNode,"height")))-10; |
| 84 |
this.bodyWrapper.style.height = Math.abs(offset) + "px"; |
| 85 |
}, |
| 86 |
|
| 87 |
onSort: function(e){ |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
var arr = dojo.query("li",this.domNode); |
| 92 |
if (this.sortable){ |
| 93 |
this.descending = !this.descending; |
| 94 |
dojo.addClass(this.titleNode,((this.descending)?"sortListDesc":"sortListAsc")); |
| 95 |
dojo.removeClass(this.titleNode,((this.descending)?"sortListAsc":"sortListDesc")); |
| 96 |
arr.sort(this._sorter); |
| 97 |
if(this.descending){ arr.reverse(); } |
| 98 |
} |
| 99 |
var i=0; |
| 100 |
dojo.forEach(arr,function(item){ |
| 101 |
dojo[(i++) % 2 === 0 ? "addClass" : "removeClass"](item,"sortListItemOdd"); |
| 102 |
this.containerNode.appendChild(item); |
| 103 |
},this); |
| 104 |
}, |
| 105 |
|
| 106 |
_set: function(e){ |
| 107 |
|
| 108 |
|
| 109 |
if(e.target !== this.bodyWrapper){ |
| 110 |
dojo.addClass(e.target,"sortListItemHover"); |
| 111 |
} |
| 112 |
}, |
| 113 |
|
| 114 |
_unset: function( e){ |
| 115 |
|
| 116 |
|
| 117 |
dojo.removeClass(e.target,"sortListItemHover"); |
| 118 |
}, |
| 119 |
|
| 120 |
_handleClick: function( e){ |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
dojo.toggleClass(e.target,"sortListItemSelected"); |
| 125 |
e.target.focus(); |
| 126 |
this._updateValues(e.target.innerHTML); |
| 127 |
}, |
| 128 |
|
| 129 |
_updateValues: function(){ |
| 130 |
this._selected = dojo.query("li.sortListItemSelected", this.containerNode); |
| 131 |
this.selected = []; |
| 132 |
dojo.forEach(this._selected, function(node){ |
| 133 |
this.selected.push(node.innerHTML); |
| 134 |
}, this); |
| 135 |
this.onChanged(arguments); |
| 136 |
}, |
| 137 |
|
| 138 |
_sorter: function(a,b){ |
| 139 |
|
| 140 |
|
| 141 |
var aStr = a.innerHTML; |
| 142 |
var bStr = b.innerHTML; |
| 143 |
if(aStr>bStr){ return 1; } |
| 144 |
if(aStr<bStr){ return -1; } |
| 145 |
return 0; |
| 146 |
}, |
| 147 |
|
| 148 |
setTitle: function( title){ |
| 149 |
|
| 150 |
|
| 151 |
this.focusNode.innerHTML = this.title = title; |
| 152 |
}, |
| 153 |
|
| 154 |
onChanged: function(){ |
| 155 |
|
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
}); |