]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/insert.js
Toggle share boxes only on successful requests
[Github/YOURLS.git] / js / insert.js
1 // Init some stuff\r
2 $(document).ready(function(){\r
3         $('#add-url, #add-keyword').keypress(function(e){\r
4                 if (e.which == 13) {add();}\r
5         });\r
6         reset_url();\r
7         $('#new_url_form').attr('action', 'javascript:add();');\r
8         \r
9         $('input.text').focus(function(){\r
10                 $(this).select();\r
11         });\r
12         \r
13         // this one actually has little impact, the .hasClass('disabled') in each edit(), remove() etc... fires faster\r
14         $('a.button').live('click', function() {\r
15                 if( $(this).hasClass('disabled') ) {\r
16                         return false;\r
17                 }\r
18         });\r
19 });\r
20 \r
21 // Create new link and add to table\r
22 function add() {\r
23         if( $('#add-button').hasClass('disabled') ) {\r
24                 return false;\r
25         }\r
26         var newurl = $("#add-url").val();\r
27         var nonce = $("#nonce-add").val();\r
28         if ( !newurl || newurl == 'http://' || newurl == 'https://' ) {\r
29                 return;\r
30         }\r
31         var keyword = $("#add-keyword").val();\r
32         add_loading("#add-button");\r
33         $.getJSON(\r
34                 ajaxurl,\r
35                 {action:'add', url: newurl, keyword: keyword, nonce: nonce},\r
36                 function(data){\r
37                         if(data.status == 'success') {\r
38                                 $('#main_table tbody').prepend( data.html ).trigger("update");\r
39                                 $('#nourl_found').css('display', 'none');\r
40                                 zebra_table();\r
41                                 increment();\r
42                                 toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );\r
43                         }\r
44 \r
45                         reset_url();\r
46                         end_loading("#add-button");\r
47                         end_disable("#add-button");\r
48 \r
49                         feedback(data.message, data.status);\r
50                 }\r
51         );\r
52 }\r
53 \r
54 function toggle_share_fill_boxes( url, shorturl, title ) {\r
55         $('#copylink').val( shorturl );\r
56         $('#titlelink').val( title );\r
57         $('#origlink').attr( 'href', url ).html( url );\r
58         $('#statlink').attr( 'href', shorturl+'+' ).html( shorturl+'+' );\r
59         var tweet = ( title ? title + ' ' + shorturl : shorturl );\r
60         $('#tweet_body').val( tweet ).keypress();\r
61         $('#shareboxes').slideDown( '300', function(){ init_clipboard(); } ); // clipboard re-initialized after slidedown to make sure the invisible Flash element is correctly positionned\r
62         $('#tweet_body').keypress();\r
63 }\r
64 \r
65 // Display the edition interface\r
66 function edit(id) {\r
67         if( $('#edit-button-'+id).hasClass('disabled') ) {\r
68                 return false;\r
69         }\r
70         add_loading('#actions-'+id+' .button');\r
71         var keyword = $('#keyword_'+id).val();\r
72         var nonce = get_var_from_query( $('#edit-button-'+id).attr('href'), 'nonce' );\r
73         $.getJSON(\r
74                 ajaxurl,\r
75                 { action: "edit_display", keyword: keyword, nonce: nonce, id: id },\r
76                 function(data){\r
77                         $("#id-" + id).after( data.html );\r
78                         $("#edit-url-"+ id).focus();\r
79                         end_loading('#actions-'+id+' .button');\r
80                 }\r
81         );\r
82 }\r
83 \r
84 // Delete a link\r
85 function remove(id) {\r
86         if( $('#delete-button-'+id).hasClass('disabled') ) {\r
87                 return false;\r
88         }\r
89         if (!confirm('Really delete?')) {\r
90                 return;\r
91         }\r
92         var keyword = $('#keyword_'+id).val();\r
93         var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );\r
94         $.getJSON(\r
95                 ajaxurl,\r
96                 { action: "delete", keyword: keyword, nonce: nonce, id: id },\r
97                 function(data){\r
98                         if (data.success == 1) {\r
99                                 $("#id-" + id).fadeOut(function(){\r
100                                         $(this).remove();\r
101                                         if( $('#main_table tbody tr').length  == 1 ) {\r
102                                                 $('#nourl_found').css('display', '');\r
103                                         }\r
104 \r
105                                         zebra_table();\r
106                                 });\r
107                                 decrement();\r
108                         } else {\r
109                                 alert('something wrong happened while deleting :/');\r
110                         }\r
111                 }\r
112         );\r
113 }\r
114 \r
115 // Redirect to stat page\r
116 function stats(link) {\r
117         window.location=link;\r
118 }\r
119 \r
120 // Cancel edition of a link\r
121 function hide_edit(id) {\r
122         $("#edit-" + id).fadeOut(200, function(){\r
123                 end_disable('#actions-'+id+' .button');\r
124         });\r
125 }\r
126 \r
127 // Save edition of a link\r
128 function edit_save(id) {\r
129         add_loading("#edit-close-" + id);\r
130         var newurl = $("#edit-url-" + id).val();\r
131         var newkeyword = $("#edit-keyword-" + id).val();\r
132         var title = $("#edit-title-" + id).val();\r
133         var keyword = $('#old_keyword_'+id).val();\r
134         var nonce = $('#nonce_'+id).val();\r
135         var www = $('#yourls-site').val();\r
136         $.getJSON(\r
137                 ajaxurl,\r
138                 {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },\r
139                 function(data){\r
140                         if(data.status == 'success') {\r
141                         \r
142                                 if( data.url.title != '' ) {\r
143                                         var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_title + '</a><br/><small><a href="' + data.url.url + '">' + data.url.display_url + '</a></small>';\r
144                                 } else {\r
145                                         var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';\r
146                                 }\r
147 \r
148                                 $("#url-" + id).html(display_link);\r
149                                 $("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');\r
150                                 $("#timestamp-" + id).html(data.url.date);\r
151                                 $("#edit-" + id).fadeOut(200, function(){\r
152                                         $('#main_table tbody').trigger("update");\r
153                                 });\r
154                                 $('#keyword_'+id).val( newkeyword );\r
155                                 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );\r
156                         }\r
157                         feedback(data.message, data.status);\r
158                         end_loading("#edit-close-" + id);\r
159                         end_disable("#actions-" + id + ' .button');\r
160                 }\r
161         );\r
162 }\r
163 \r
164 // Prettify table with odd & even rows\r
165 function zebra_table() {\r
166         $("#main_table tbody tr:even").removeClass('odd').addClass('even');\r
167         $("#main_table tbody tr:odd").removeClass('even').addClass('odd');\r
168         $('#main_table tbody').trigger("update");\r
169 }\r
170 \r
171 // Ready to add another URL\r
172 function reset_url() {\r
173         $('#add-url').val('http://').focus();\r
174         $('#add-keyword').val('');\r
175 }\r
176 \r
177 // Increment URL counters\r
178 function increment() {\r
179         $('.increment').each(function(){\r
180                 $(this).html( parseInt($(this).html()) + 1);\r
181         });\r
182 }\r
183 \r
184 // Decrement URL counters\r
185 function decrement() {\r
186         $('.increment').each(function(){\r
187                 $(this).html( parseInt($(this).html()) - 1 );\r
188         });\r
189 }\r
190 \r
191 // Toggle Share box\r
192 function toggle_share(id) {\r
193         if( $('#share-button-'+id).hasClass('disabled') ) {\r
194                 return false;\r
195         }\r
196         var link = $('#url-'+id+' a:first');\r
197         var longurl = link.attr('href');\r
198         var title = link.attr('title');\r
199         var shorturl = $('#keyword-'+id+' a:first').attr('href');\r
200         \r
201         toggle_share_fill_boxes( longurl, shorturl, title );\r
202 }\r