]> Joshua Wise's Git repositories - patchfork.git/blob - std/quickadd.js
Add Jon Fink's iPhone theme/frontend for Pitchfork. Access using player/mini.php.
[patchfork.git] / std / quickadd.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 var quickadd_last_term = "";
20
21 function quickadd_focus(e) {
22         var qa = document.getElementById('quickadd');
23         var qa_disp = document.getElementById('qa_suggestions');
24         if(e&&e.type&&e.type!="focus");
25                 qa.focus();
26         if(qa.value==qa.defaultValue) {
27                 qa.value = "";
28         }
29         qa_disp.style.display = "block";
30 }
31 function quickadd_blur(e) {
32         var qa = document.getElementById('quickadd');
33         var qa_disp = document.getElementById('qa_suggestions');
34
35         if(qa.value.trim().length==0) {
36                 qa.value=qa.defaultValue;
37                 quickadd_hide();
38         }
39 }
40 function quickadd_keydown_handler(e) { 
41         e.stopPropagation(); 
42         var qa = document.getElementById('quickadd');
43         var key = e.keyCode;
44
45         /* return key, send request to add if something to add */
46         if(key==RETURN_KEY_CODE) {
47                 stop_event(e);
48                 var add = qa.value;
49                 add = add.trim();
50                 if(add.length>0) {
51                         var txt_node = document.getElementById('qa_suggestions_txt');
52                         var elems = txt_node.getElementsByTagName("span");
53                         var i = qa_get_selected_id(elems);
54                         if(i>=0) {
55                                 add = elems[i].name;
56                         }
57                         quickadd_clean();
58                         send_command("add=" + encodeURIComponent(add), function(response) 
59                                         { if(response=="failed") show_status_bar(LANG.E_FAILED_ADD); }, 
60                                         LANG.WAIT_ADDING);
61                 }
62                 else {
63                         quickadd_clean();
64                 }
65         }
66         else if(key==27) { // esc :(
67                 stop_event(e);
68                 qa.value = "";
69                 qa.blur();
70                 quickadd_hide();
71         }
72         else if(key>=37&&key<=40) { /* left up right down */
73                 if(key==40) { // down
74                         quickadd_move_selection(1);
75                         stop_event(e);
76                 }
77                 else if(key==38) {  // up
78                         quickadd_move_selection(-1);
79                         stop_event(e);
80                 }
81                 else if(key==39) { // right
82                         var txt_node = document.getElementById('qa_suggestions_txt');
83                         var elems = txt_node.getElementsByTagName("span");
84                         var sel = qa_get_selected_id(elems);
85                         if(sel>=0) {
86                                 //stop_event(e);
87                                 qa.value = elems[sel].name + "/";
88                                 quickadd_keyup_handler();
89                                 qa.focus();
90                                 setCaretToEnd(qa);
91                         }
92                 }
93                 else if(key==37) { // left
94                 }
95         }
96 }
97
98 function quickadd_clean() {
99         var qa = document.getElementById('quickadd');
100         qa.value = "";
101         qa.blur();
102         quickadd_hide();
103 }
104
105 function qa_get_selected_id(elems) {
106         for(var i=0; i<elems.length; i++) {
107                 if(elems[i].hasAttribute("qa_selected")) {
108                         return i;
109                 }
110         }
111         return -1;
112 }
113
114 function quickadd_move_selection(num) {
115         var txt_node = document.getElementById('qa_suggestions_txt');
116         var elems = txt_node.getElementsByTagName("span");
117         var sel_node = qa_get_selected_id(elems);
118
119         if(sel_node>=0) {
120                 elems[sel_node].removeAttribute("qa_selected");
121         }
122
123         num = num+sel_node;
124
125         if(num>=elems.length ||num==-1) {
126                 return;
127         }
128         else if(num<0) // flip it around
129                 num=elems.length-1;
130         elems[num].setAttribute("qa_selected", "omg");
131         /* safari workaround */
132         elems[num].className = elems[num].className;
133 }
134
135 function quickadd_hide() {
136         var txt_node = document.getElementById('qa_suggestions_txt');
137         var qa_disp = document.getElementById('qa_suggestions');
138         qa_disp.style.display = "none";
139         remove_children(txt_node);
140 }
141
142 function quickadd_keyup_handler(e) {
143         var qa = document.getElementById('quickadd');
144         var search_str = qa.value;
145         search_str = search_str.trim();
146
147         /* unwanted event */
148         if(e) {
149                 e.stopPropagation(); 
150                 if(e.altKey||e.metaKey||e.ctrlKey) {
151                         return;
152                 }
153         }
154
155         if(search_str.length>0) {
156                 if(search_str!=quickadd_last_term) {
157                         quickadd_last_term = search_str;
158                         send_command("quick_search=" + encodeURIComponent(search_str), quickadd_result_handler);
159                 }
160         }
161         else {
162                 var txt_node = document.getElementById('qa_suggestions_txt');
163                 remove_children(txt_node);
164                 quickadd_last_term = "";
165         }
166 }
167
168 function quickadd_result_handler(res) {
169         var txt_node = document.getElementById('qa_suggestions_txt');
170         if(!res||res=="failed") {
171                 remove_children(txt_node);
172                 txt_node.appendChild(create_txt(LANG.E_NOTHING_FOUND));
173         }
174         else {
175                 remove_children(txt_node);
176                 for(var ix in res) {
177                         var name = res[ix];
178                         var node = create_node("span");
179                         node.className = "qa_element";
180                         node.name = name;
181                         var idx = name.lastIndexOf(DIR_SEPARATOR);
182                         node.appendChild(create_txt((idx>0?"..":"") + name.substring(idx)));
183                         txt_node.appendChild(node);
184                 }
185         }
186 }
This page took 0.032781 seconds and 4 git commands to generate.