Debian Codegraph @

dojox/widget/SortList.js

raw symbols SCIP
1 dojo.provide("dojox.widget.SortList");
2 dojo.experimental("dojox.widget.SortList"); // level: prototype, designed for dijit.chat.demo
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 // summary:
11 // A sortable unordered-list with a fixed header for use in dijit.demos.chat
12 // for demonstration purposes only for now. feel free to make API suggestions
13 // or fixes.
14 //
15 // title: String
16 // The title in the header
17 title: "",
18
19 // heading: String
20 // In the event a parent container is expecting a title="" attribute, set it for the parent
21 // via title, and the title of this widget via heading="" ... assuming you want different
22 // titles for each. eg: TabContainer, AccordionContainer, etc.
23 heading: "",
24
25 // descending: Boolean
26 // Toggle sort order based on this value.
27 descending: true,
28
29 // selected: Array
30 // A list of the selected `<li>` nodes at any given time.
31 selected: null,
32
33 // sortable: Boolean
34 // toggle to enable/disable sorting
35 sortable: true,
36
37 // FIXME: this is really simple store support
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, "&lt;")
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 // we cheat, and give the browser just enough time so we know our height
70 setTimeout(dojo.hitch(this,"resize"), 5);
71 if(this.sortable){ this.connect(this.titleNode,"onclick", "onSort"); }
72 },
73
74 resize: function(){
75 // summary:
76 // do our additional calculations when resize() is called by or in a parent
77 this.inherited(arguments);
78 // FIXME:
79 // the 10 comes from the difference between the contentBox and calculated height
80 // because of badding and border extents. this shouldn't be done this way, a theme change will
81 // break it: but we also don't want to run getComputedStyle or dojo.coords() every time resize()
82 // is fired.
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(/* Event */e){
88 // summary:
89 // sort the data, and style the nodes.
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(/* Event */e){
107 // summary:
108 // set hover state
109 if(e.target !== this.bodyWrapper){
110 dojo.addClass(e.target,"sortListItemHover");
111 }
112 },
113
114 _unset: function(/* Event */ e){
115 // summary:
116 // remove hover state (FIXME: combine with _set?)
117 dojo.removeClass(e.target,"sortListItemHover");
118 },
119
120 _handleClick: function(/* Event */ e){
121 // summary:
122 // click listener for data portion of widget. toggle selected state
123 // of node, and update this.selected array accordingly
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 // summary:
140 // a basic sort function, use query sort, or keep this?
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(/* String */ title){
149 // summary:
150 // Sets the widget title to a String
151 this.focusNode.innerHTML = this.title = title;
152 },
153
154 onChanged: function(){
155 // summary:
156 // stub function, passes the last changed item, and is fired after current state
157 }
158
159 });