OSDN Git Service

embrj
[embrj/master.git] / js / search.js
1 $(function(){
2         var theAC = null;
3         var searches = null;
4
5         $("#btn_savesearch").click(function(e){
6                 var nowsearch = $("#query").val().trim();
7                 if (theAC == null || searches == null || nowsearch == '') {
8                         updateSentTip("Error in saving search!", 3000, "failure");
9                         return;
10                 }
11
12                 var sameone = false;
13                 $.each(searches, function(){
14                         if (nowsearch == this[1]) {
15                                 updateSentTip("Duplicated search!", 3000, "failure");
16                                 sameone = true;
17                         }
18                 });
19                 if (sameone) return;
20
21                 $.ajax({
22                         url: "ajax/savedSearches.php",
23                         data: {method: "save", query: nowsearch},
24                         type: "GET",
25                         success: function(msg) {
26                                 if (msg.indexOf("[") >= 0) {
27                                         updateSentTip("Successfully saved search!", 3000, "success");
28                                         var theData = eval("("+msg+")");
29                                         searches.push(theData);
30                                         theAC.flushCache();
31                                         theAC.setOptions({data:searches});
32                                 }
33                                 else
34                                         updateSentTip("Error in saving search!", 3000, "failure");
35                         },
36                         error: function(msg) {
37                                 updateSentTip("Error in saving search!", 3000, "failure");
38                         }
39                 });
40         });
41
42         $.ajax({
43                 url: "ajax/savedSearches.php",
44                 data: {method: "list"},
45                 type: "GET",
46                 success: function(msg) {
47                         searches = eval("("+msg+")");
48                         $(".ss_delete_btn").live("click",function(e){
49                                 e.preventDefault();
50                                 $("#query").val("");
51                                 if (theAC != null && searches != null) {
52                                         var selectedId = $(this).attr("id").substr(3);
53                                         var selectedIndex = -1;
54                                         $.each(searches, function(ind, ele){
55                                                 if (ele[0] == selectedId)
56                                                         selectedIndex = ind;
57                                         });
58                                         if (selectedIndex != -1) {
59                                                 var cfm = window.confirm("Are you sure to delete the saved search \"" + searches[selectedIndex][1] + "\"?");
60                                                 if (!cfm) return;
61                                                 $.ajax({
62                                                         url: "ajax/savedSearches.php",
63                                                         data: {method: "delete", ssid: selectedId},
64                                                         type: "GET",
65                                                         success: function(m) {
66                                                                 if (m.indexOf("success") >= 0)
67                                                                         updateSentTip("Successfully deleted saved search!", 3000, "success");
68                                                                 else
69                                                                         updateSentTip("Error in deleting saved search!", 3000, "failure");
70                                                                 searches.splice(selectedIndex, 1);
71                                                                 theAC.flushCache();
72                                                                 theAC.setOptions({data:searches});
73                                                         },
74                                                         error: function(m) {
75                                                                 updateSentTip("Error in deleting saved search!", 3000, "failure");
76                                                         }
77                                                 });
78                                         }
79                                 }
80                         });
81                         theAC = $("#query").autocomplete(searches, {
82                                 minChars:0,
83                                 formatItem:function(data, i, total) {
84                                         return "<a class=\"ss_delete_btn\" href=\"#\" id=\"sgt" + data[0] + "\" >delete</a>" + data[1];
85                                 },
86                                 formatMatch:function(data, i, total) {
87                                         return data[1];
88                                 },
89                                 formatResult:function(data) {
90                                         return data[1];
91                                 }
92                         });
93                 },
94                 error: function(msg) {
95                         updateSentTip("Failed to fetch the saved searches!", 3000, "failure");
96                 }
97         });
98
99         formHTML = "<h2>What are you doing?</h2>" + formHTML + "<div class=\"clear\"></div>";
100         $("#allTimeline").click(function(e) {
101                 var $this = $(e.target);
102                 var type = $this.attr('class');
103                 switch(type) {
104                         case 'rt_btn':
105                                 e.preventDefault();
106                                 if ($("#textbox").length > 0) {
107                                         onRT($this);
108                                 } else {
109                                         $("#search_form").after(formHTML);
110                                         formFunc();
111                                         onRT($this);
112                                 }
113                                 break;
114                         case 'retw_btn':
115                                 e.preventDefault();
116                                 onNwRT($this);
117                                 break;
118                         case 'replie_btn':
119                                 e.preventDefault();
120                                 if ($("#textbox").length > 0) {
121                                         onReplie($this,e);
122                                 } else {
123                                         $("#search_form").after(formHTML);
124                                         formFunc();
125                                         onReplie($this,e);
126                                 }
127                                 break;
128                         case 'favor_btn':
129                                 e.preventDefault();
130                                 onFavor($this);
131                                 break;
132                         case 'unfav_btn':
133                                 e.preventDefault();
134                                 UnFavor($this);
135                                 break;
136                         case 'delete_btn':
137                                 e.preventDefault();
138                                 onDelete($this);
139                                 break;
140                         case 'rt_undo':
141                                 e.preventDefault();
142                                 onUndoRt($this);
143                                 break;
144                 }
145         });
146         $("#submit_btn").click(function(e){
147                 updateStatus();
148                 e.preventDefault();
149         });
150         updateTrends();
151 });