]> Joshua Wise's Git repositories - patchfork.git/blob - std/plsearch.js
74094ca04576c465e29a30b3ccce9621f48979a6
[patchfork.git] / std / plsearch.js
1 /* 
2     Pitchfork Music Player Daemon Client
3     Copyright (C) 2007  Roger Bystrøm
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; version 2 of the License.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19
20 var sidebar = null;
21
22 function Sidebar() {
23         this.is_open = false;
24         this.view = new HashMap();
25         this.header_height = new HashMap();
26         this.open_view = "metadata";
27         this.header_height.put("metadata", 20);
28         /* TODO: find another way to determine the height of this */
29         this.display_base_top = 107;
30         this.header = document.getElementById("sidebar_header");
31         this.display = document.getElementById("sidebar_display");
32         this.display_txt = document.getElementById("sidebar_display_txt");
33         this.last_metadata_request = null;
34         this.PLSEARCH_OPTS = new Array("Any", "Artist", "Title", "Album", "Genre", "Filename", "Composer", "Performer", "Date");
35         this.plsearch_choice = null; // select box
36         this.plsearch_input = null; // search input box
37 }
38
39 Sidebar.prototype.say_loading = function(txt) {
40         if(typeof(txt)=='undefined')
41                 txt = LANG.WAIT_LOADING;
42         this.set_content(create_txt(txt));
43 }
44
45 Sidebar.prototype.adjust_height = function(name) {
46         var h = this.header_height.get(name);
47         this.display.style.top = (this.display_base_top + h) + "px";
48         this.header.style.height = h + "px";
49 }
50
51 Sidebar.prototype.set_content = function(fragment) {
52         remove_children(this.display_txt);
53         this.display_txt.appendChild(fragment);
54 }
55
56 /* Name of view, buffer/element that is this view, and height of view */
57 Sidebar.prototype.add_view = function(name, buffer, height) {
58         this.view.put(name,buffer);
59         if(!height)
60                 height = 20;
61         this.header_height.put(name, height);
62 }
63
64 /* will switch to the specified view if it isn't already open */
65 Sidebar.prototype.switch_view = function(name) {
66         if(this.open_view==name) 
67                 return;
68         var n = this.view.remove(name); // make sure we can get it first
69         if(!n) {
70                 debug("can't get new sidebar view: " + name);
71                 return;
72         }
73         var buf = remove_children(this.header);
74         this.view.put(this.open_view, buf);
75         this.header.appendChild(n);
76         this.open_view = name;
77 }
78
79
80 function sidebar_close() {
81         if(browser_is_opera()) {
82                 sidebar.header.style.display = "none";
83                 opera_quirk_set_display_none(sidebar.display);
84         }
85         else {
86                 sidebar.header.style.display = "none";
87                 sidebar.display.style.display = "none";
88         }
89         remove_children(sidebar.display_txt);
90 }
91
92 function sidebar_open(view) {
93         if(view&&view!=sidebar.open_view) { // we have to change view
94                 sidebar.switch_view(view);
95                 sidebar.adjust_height(view);
96         }
97         sidebar.header.style.display = "block";
98         sidebar.display.style.display = "block";
99 }
100
101
102 function sidebar_init() {
103         sidebar = new Sidebar();
104 }
105
106 function plsearch_init() {
107         /* you can either use a buffer like in create_fragment()
108          * or add it to a top-container like I've done here*/
109         var t = create_node("p");
110         t.className = "nomargin";
111         t.appendChild(create_txt("Playlistsearch:  "));
112         var close = create_node("span", null, " ["+LANG.CLOSE+"]");
113         add_listener(close, "click", sidebar_close);
114         close.className = "fakelink";
115         t.appendChild(close);
116         t.appendChild(create_node("br"));
117
118         var search = create_search_choices(sidebar.PLSEARCH_OPTS, plsearch_choice_change)
119
120         t.appendChild(search);
121
122         sidebar.plsearch_choice = search;
123         search = create_node("input");
124         search.type = "text";
125         sidebar.plsearch_input = search;
126         add_listener(search, "keyup", plsearch_term_change);
127         add_listener(search, "keydown", stop_propagation);
128         search.className = "browse_type";
129         t.appendChild(search);
130         
131         sidebar.add_view("plsearch", t, 45);
132 }
133
134 function plsearch_choice_change(e) {
135         stop_propagation(e);
136         sidebar.plsearch_input.focus();
137 }
138
139 function plsearch_open() {
140         sidebar_open("plsearch");
141         sidebar.set_content(create_txt(""));
142         sidebar.plsearch_input.focus();
143 }
144
145 function plsearch_set_content(content) {
146         if(sidebar.open_view=="plsearch") 
147                 sidebar.set_content(content);
148 }
149
150 function plsearch_term_change(e) {
151         stop_propagation(e); // we'll keep it
152         if(e.keyCode == RETURN_KEY_CODE) { // send search
153                 var s = sidebar.plsearch_input.value.trim();
154                 if(s.length>0) {
155                         send_command("plsearch=" + sidebar.plsearch_choice.selectedIndex + 
156                                 "&s=" + s, plsearch_search_cb, LANG.WAIT_SEARCHING);
157                 }
158                 else { // clear results
159                         // fixme, possible leak
160                         remove_listener(sidebar.display.firstChild , "click", plsearch_click);
161                         remove_listener(sidebar.display.firstChild , "mousedown", stop_event);
162                         plsearch_set_content(create_txt(""));
163                 }
164         }
165 }
166
167 function plsearch_search_cb(resp) {
168         if(typeof(resp)!='undefined'&&resp!="failed") {
169                 var dst= create_node("p");
170                 dst.style.padding = "0px";
171                 for(var i=0; i<resp.length; i++) {
172                         var file = resp[i]["file"];
173                         var artist = resp[i]["Artist"];
174                         var title = resp[i]["Title"];
175                         var pos = resp[i]["Pos"];
176                         var name = "";
177                         if(title==null||!title.length) {
178                                 name = file.substring(file.lastIndexOf(DIR_SEPARATOR)+1);
179                         }
180                         else {
181                                 name = artist + " - " + title;
182                         }
183
184                         var e = create_node("span", null, name);
185                         e.className = "plse";
186                         e.setAttribute("diritem", file);
187                         e.setAttribute("dirtype", "file");
188                         e.setAttribute("plpos", pos);
189                         dst.appendChild(e);
190                 }
191                 plsearch_set_content(dst);
192                 add_listener(dst, "click", plsearch_click);
193                 add_listener(dst, "mousedown", stop_event);
194         }
195         else {
196                 plsearch_set_content(create_txt(LANG.E_INVALID_RESULT));
197         }
198 }
199
200 function plsearch_click(e) {
201         stop_event(e);
202         var target = e.target;
203         if(target&&target.hasAttribute("plpos")) {
204                 if(e.detail==1) {
205                         playlist_scroll_to_pos(parseInt(target.getAttribute("plpos")), true);   
206                 }
207                 else if(e.detail==2) {
208                         send_play_pos(target.getAttribute("plpos"));
209                 }
210         }
211 }
212
This page took 0.034473 seconds and 4 git commands to generate.