OSDN Git Service

embrj
[embrj/master.git] / js / lists.js
1 $(function(){
2         $(".rt_btn").live("click", function(e){
3                 e.preventDefault();
4                 onRT($(this));
5         });
6         
7         $(".replie_btn").live("click", function(e){
8                 e.preventDefault();
9                 onReplie($(this),e);
10         });
11         $(".favor_btn").live("click", function(e){
12                 e.preventDefault();
13                 onFavor($(this));
14         });
15         $(".unfollow_list").live("click",function(e){
16                 e.preventDefault();
17                 var $this = $(this);
18                 var id = $this.parent().parent().find(".rank_name").text().substr(1);
19                 updateSentTip("Unfollowing lists...", 5000, "ing");
20                 $.ajax({
21                         url: "ajax/list.php",
22                         type: "POST",
23                         data: "action=destory&id=" + id,
24                         success: function(msg) {
25                                 if (msg.indexOf("success") >= 0) {
26                                         updateSentTip("Successfully unfollowing list " + id, 3000, "success");
27                                         $this.parent().parent().parent().fadeOut("fast");
28                                 } else {
29                                         updateSentTip("Unfollow failed. Please try again.", 3000, "failure");
30                                 }
31                         },
32                         error: function(msg) {
33                                 updateSentTip("Unfollow failed. Please try again.", 3000, "failure");
34                         }
35                 });
36                 
37         });
38
39         $(".delete_list").live("click", function(e){
40                 e.preventDefault();
41                 var $this = $(this);  
42                 var list_slug = $this.parent().parent().find(".rank_name").text().split("/")[1];
43                 var confirm = window.confirm("Do you really want to delete " + list_slug + "?");
44                 if (confirm) {
45                         updateSentTip("deleting list " + list_slug + "...", 5000, "ing");
46                         $.ajax({
47                                 url: "ajax/delete.php",
48                                 type: "POST",
49                                 data: "list_slug=" + list_slug,
50                                 success: function(msg) {
51                                         if (msg.indexOf("success") >= 0) {
52                                                 updateSentTip("Successfully deleting " + list_slug, 3000, "success");
53                                                 $this.parent().parent().parent().fadeOut("fast");
54                                         } else {
55                                                 updateSentTip("Delete failed. Please try again.", 3000, "failure");
56                                         }
57                                 },
58                                 error: function(msg) {
59                                         updateSentTip("Delete failed. Please try again.", 3000, "failure");
60                                 }
61                         });
62                 }
63         });
64         
65         $("#list_create_btn").live("click", function(e){
66                 e.preventDefault();
67                 $("#list_form").toggle("fast");
68                 $("#list_name").focus().val("");
69                 $("#list_description").val("");
70                 $("#list_protect").removeAttr("checked");
71                 $("#pre_list_name").val("");
72                 $("#list_spanid").val("");
73                 $("#is_edit").val(0);
74                 $("#list_submit").val("Create");
75         });
76         
77         $(".edit_list").live("click", function(e){
78                 e.preventDefault();
79                 var parent = $(this).parent().parent();
80                 var list_name = parent.find(".rank_name").text().split("/")[1];
81                 var list_description = parent.find(".rank_description").text().slice(5);
82                 var list_protect = parent.find(".rank_count").text().indexOf("Private") > 0;
83
84                 $("#list_form").show("fast");
85                 $("#list_name").focus().val(list_name);
86                 $("#list_description").val(list_description);
87                 if (list_protect) { 
88                         $("#list_protect").attr("checked", "checked");
89                 } else {
90                         $("#list_protect").removeAttr("checked");
91                 }
92                 $("#list_spanid").val(parent.attr("id"));
93                 $("#is_edit").val(1);
94                 $("#list_submit").val("Edit");
95                 $("#pre_list_name").val(list_name);
96         })
97         
98         $("#list_submit").live("click", function(e){
99                 e.preventDefault();
100                 var list_name = $("#list_name").val();
101                 if (list_name.length == 0) {
102                         window.alert("List name cannot be empty!");
103                         return;
104                 }
105                 $('#list_form').slideToggle(300);
106                 var list_description = $("#list_description").val();
107                 var list_protect = $("#list_protect").attr("checked") == "checked" ? "private" : "public";
108                 var pre_list_name = $("#pre_list_name").val();
109                 var is_edit = $("#is_edit").val();
110                 var postdata = {"name" : list_name,
111                                 "description" : list_description,
112                                 "mode" : list_protect};
113                 if (is_edit == 1) {
114                         postdata["slug"] = pre_list_name;
115                         updateSentTip("editing list " + pre_list_name + "...", 5000, "ing");
116                         var spanid = $("#list_spanid").val();
117                 } else
118                         updateSentTip("creating list " + list_name + "...", 5000, "ing");
119                         
120                 $.ajax({
121                         url: "ajax/modifyList.php",
122                         type: "POST",
123                         dataType: "json",
124                         data: postdata,
125                         success: function(msg) {
126                                 if (msg.result == 'success') {
127                                         if (is_edit == 1) {
128                                                 updateSentTip("Successfully modifying list " + pre_list_name, 3000, "success");
129                                                 var rank_content = $("#"+spanid);
130                                                 rank_content.find(".rank_name").html('<a href="list.php?id='+msg.listuri+'"><em>'+msg.username+'/</em>'+list_name+'</a>');
131                                                 var rank_count = rank_content.find(".rank_count");
132                                                 rank_count.html(rank_count.html().replace(/Public|Private/, list_protect == "public" ? "Public" : "Private"));
133                                                 rank_content.find(".rank_description").html("Bio: "+list_description);
134                                         }
135                                         else {
136                                                 updateSentTip("Successfully creating list " + list_name, 3000, "success");
137                                                 var html = '<li><span class=\"rank_img\"><img src="'+msg.imgurl+'" /></span>';
138                                                 html += '<div class="rank_content" id="'+msg.contentid+'"><span class="rank_num"><span class="rank_name">';
139                                                 html +='<a href="list.php?id='+msg.listuri+'"><em>'+msg.username+'/</em>'+list_name+'</a>';
140                                                 html += '</span></span><span class="rank_count">Followers: 0&nbsp;&nbsp;Members: 0&nbsp;&nbsp;'+(list_protect == "public" ? "Public" : "Private")+'</span>';
141                                                 html += '<span class="rank_description">Bio: '+list_description+'</span>';
142                                                 html += '<span id="list_action"><a id="btn" href="#" class="edit_list">Edit</a> <a id="btn" href="#" class="delete_list">Delete</a> <a id="btn" href="#" class="add_member">Add Members</a></span>';
143                                                 html += "</div></li>";
144                                                 $(html).prependTo($(".rank_list")).fadeIn('fast');
145                                         }
146                                 } else {
147                                         if (is_edit == 1)
148                                                 updateSentTip("Editing failed. Please try again.", 3000, "failure");
149                                         else
150                                                 updateSentTip("Creating failed. Please try again.", 3000, "failure");
151                                 }
152                         }
153                 });
154         });
155         
156         $(".add_member").live("click", function(e){
157                 e.preventDefault();
158                 $("#member_form").remove();
159                 var position = $(this).position();
160                 var liPosition = $(this).parent().parent().parent().position();
161                 var list_name = $(this).parent().parent().find(".rank_name").text().split("/")[1];
162                 var owner_name = $(this).parent().parent().find(".rank_name").text().split("/")[0];
163                 var rank_count = $(this).parent().parent().find(".rank_count");
164                 owner_name = owner_name.split("@")[1];
165                 $('<form method="POST" action="./lists.php?t=1" id="member_form">' +
166                 '<span>User ID:(Saperated with comma, e.g. JLHwung,twitter)</span>' +
167                 '<span><textarea type="text" name="list_members" id="list_members"></textarea></span>' +
168                 '<span><input type="submit" class="btn" id="member_submit" value="Submit" /> <input type="button" class="btn" id="member_cancel" value="Cancel" /></span>' +
169             '</form>').appendTo("#statuses").css("left", liPosition.left + position.left).css("top", liPosition.top + position.top + 30);
170                 
171                 $("#member_cancel").click(function(){
172                         $("#member_form").remove();
173                 });
174                 
175                 $("#member_submit").click(function(e){
176                         e.preventDefault();
177                         var list_members = $("#list_members").val();
178                         if (list_members.length <= 0) {
179                                 window.alert("User IDs cannot be empty!");
180                                 return;
181                         }
182                         $("#member_form").remove();
183                         updateSentTip("adding members to list " + list_name + "...", 5000, "ing");
184                         $.ajax({
185                                 url: "ajax/addMembersToList.php",
186                                 type: "POST",
187                                 data: { "slug" : list_name,
188                                         "owner" : owner_name,
189                                         "add_members" : list_members},
190                                 success: function(msg) {
191                                         if (msg.indexOf("error") >= 0) {
192                                                 updateSentTip("Adding failed. Please try again.", 3000, "failure");
193                                         } else {
194                                                 updateSentTip("Successfully adding members to list " + list_name, 3000, "success");
195                                                 rank_count.html(msg);
196                                         }
197                                 },
198                                 error: function(msg) {
199                                         updateSentTip("Adding failed. Please try again.", 3000, "failure");
200                                 }
201                         });
202                 })
203         })
204         
205 });