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