]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/insert.js
Implementation of the zclip jQuery plugin (a nice ZeroClipboard wrapper)
[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                         }\r
43 \r
44                         reset_url();\r
45                         toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );\r
46 \r
47                         end_loading("#add-button");\r
48                         end_disable("#add-button");\r
49 \r
50                         feedback(data.message, data.status);\r
51                 }\r
52         );\r
53 }\r
54 \r
55 function toggle_share_fill_boxes( url, shorturl, title ) {\r
56         $('#copylink').val( shorturl );\r
57         $('#titlelink').val( title );\r
58         $('#origlink').attr( 'href', url ).html( url );\r
59         $('#statlink').attr( 'href', shorturl+'+' ).html( shorturl+'+' );\r
60         var tweet = ( title ? title + ' ' + shorturl : shorturl );\r
61         $('#tweet_body').val( tweet ).keypress();\r
62         $('#shareboxes').slideDown( '300', function(){ init_clipboard(); } ); // clipboard re-initialized after slidedown to make sure the invisible Flash element is correctly positionned\r
63         $('#tweet_body').keypress();\r
64 }\r
65 \r
66 // Display the edition interface\r
67 function edit(id) {\r
68         if( $('#edit-button-'+id).hasClass('disabled') ) {\r
69                 return false;\r
70         }\r
71         add_loading('#actions-'+id+' .button');\r
72         var keyword = $('#keyword_'+id).val();\r
73         var nonce = get_var_from_query( $('#edit-button-'+id).attr('href'), 'nonce' );\r
74         $.getJSON(\r
75                 ajaxurl,\r
76                 { action: "edit_display", keyword: keyword, nonce: nonce, id: id },\r
77                 function(data){\r
78                         $("#id-" + id).after( data.html );\r
79                         $("#edit-url-"+ id).focus();\r
80                         end_loading('#actions-'+id+' .button');\r
81                 }\r
82         );\r
83 }\r
84 \r
85 // Delete a link\r
86 function remove(id) {\r
87         if( $('#delete-button-'+id).hasClass('disabled') ) {\r
88                 return false;\r
89         }\r
90         if (!confirm('Really delete?')) {\r
91                 return;\r
92         }\r
93         var keyword = $('#keyword_'+id).val();\r
94         var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );\r
95         $.getJSON(\r
96                 ajaxurl,\r
97                 { action: "delete", keyword: keyword, nonce: nonce, id: id },\r
98                 function(data){\r
99                         if (data.success == 1) {\r
100                                 $("#id-" + id).fadeOut(function(){\r
101                                         $(this).remove();\r
102                                         if( $('#main_table tbody tr').length  == 1 ) {\r
103                                                 $('#nourl_found').css('display', '');\r
104                                         }\r
105 \r
106                                         zebra_table();\r
107                                 });\r
108                                 decrement();\r
109                         } else {\r
110                                 alert('something wrong happened while deleting :/');\r
111                         }\r
112                 }\r
113         );\r
114 }\r
115 \r
116 // Redirect to stat page\r
117 function stats(link) {\r
118         window.location=link;\r
119 }\r
120 \r
121 // Cancel edition of a link\r
122 function hide_edit(id) {\r
123         $("#edit-" + id).fadeOut(200, function(){\r
124                 end_disable('#actions-'+id+' .button');\r
125         });\r
126 }\r
127 \r
128 // Save edition of a link\r
129 function edit_save(id) {\r
130         add_loading("#edit-close-" + id);\r
131         var newurl = $("#edit-url-" + id).val();\r
132         var newkeyword = $("#edit-keyword-" + id).val();\r
133         var title = $("#edit-title-" + id).val();\r
134         var keyword = $('#old_keyword_'+id).val();\r
135         var nonce = $('#nonce_'+id).val();\r
136         var www = $('#yourls-site').val();\r
137         $.getJSON(\r
138                 ajaxurl,\r
139                 {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },\r
140                 function(data){\r
141                         if(data.status == 'success') {\r
142                         \r
143                                 if( data.url.title != '' ) {\r
144                                         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
145                                 } else {\r
146                                         var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';\r
147                                 }\r
148 \r
149                                 $("#url-" + id).html(display_link);\r
150                                 $("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');\r
151                                 $("#timestamp-" + id).html(data.url.date);\r
152                                 $("#edit-" + id).fadeOut(200, function(){\r
153                                         $('#main_table tbody').trigger("update");\r
154                                 });\r
155                                 $('#keyword_'+id).val( newkeyword );\r
156                                 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );\r
157                         }\r
158                         feedback(data.message, data.status);\r
159                         end_loading("#edit-close-" + id);\r
160                         end_disable("#actions-" + id + ' .button');\r
161                 }\r
162         );\r
163 }\r
164 \r
165 // Prettify table with odd & even rows\r
166 function zebra_table() {\r
167         $("#main_table tbody tr:even").removeClass('odd').addClass('even');\r
168         $("#main_table tbody tr:odd").removeClass('even').addClass('odd');\r
169         $('#main_table tbody').trigger("update");\r
170 }\r
171 \r
172 // Ready to add another URL\r
173 function reset_url() {\r
174         $('#add-url').val('http://').focus();\r
175         $('#add-keyword').val('');\r
176 }\r
177 \r
178 // Increment URL counters\r
179 function increment() {\r
180         $('.increment').each(function(){\r
181                 $(this).html( parseInt($(this).html()) + 1);\r
182         });\r
183 }\r
184 \r
185 // Decrement URL counters\r
186 function decrement() {\r
187         $('.increment').each(function(){\r
188                 $(this).html( parseInt($(this).html()) - 1 );\r
189         });\r
190 }\r
191 \r
192 // Toggle Share box\r
193 function toggle_share(id) {\r
194         if( $('#share-button-'+id).hasClass('disabled') ) {\r
195                 return false;\r
196         }\r
197         var link = $('#url-'+id+' a:first');\r
198         var longurl = link.attr('href');\r
199         var title = link.attr('title');\r
200         var shorturl = $('#keyword-'+id+' a:first').attr('href');\r
201         \r
202         toggle_share_fill_boxes( longurl, shorturl, title );\r
203 }\r