]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/insert.js
JS improvements: fix Firebug warning about keycodes.
[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').click(function(){\r
10                 $(this).select();\r
11         });     \r
12         \r
13 });\r
14 \r
15 // Create new link and add to table\r
16 function add() {\r
17         var newurl = $("#add-url").val();\r
18         if ( !newurl || newurl == 'http://' || newurl == 'https://' ) {\r
19                 return;\r
20         }\r
21         var keyword = $("#add-keyword").val();\r
22         add_loading("#add-button");\r
23         $.getJSON(\r
24                 "index_ajax.php",\r
25                 {mode:'add', url: newurl, keyword: keyword},\r
26                 function(data){\r
27                         if(data.status == 'success') {\r
28                                 $('#tblUrl tbody').prepend( data.html ).trigger("update");\r
29                                 $('.nourl_found').remove();\r
30                                 zebra_table();\r
31                                 reset_url();\r
32                                 increment();\r
33                         }\r
34                         feedback(data.message, data.status);\r
35                         \r
36                         $('#copylink').val( data.shorturl );\r
37                         $('#origlink').attr( 'href', data.url.url ).html( data.url.url );\r
38                         $('#statlink').attr( 'href', data.shorturl+'+' ).html( data.shorturl+'+' );\r
39                         $('#tweet_body').val( data.shorturl ).keypress();\r
40                         $('#shareboxes').slideDown();           \r
41                         \r
42                         end_loading("#add-button");\r
43                         end_disable("#add-button");\r
44                 }\r
45         );\r
46 }\r
47 \r
48 // Display the edition interface\r
49 function edit(id) {\r
50         add_loading('#actions-'+id+' .button');\r
51         var keyword = $('#keyword_'+id).val();\r
52         $.getJSON(\r
53                 "index_ajax.php",\r
54                 { mode: "edit_display", keyword: keyword },\r
55                 function(data){\r
56                         $("#id-" + id).after( data.html );\r
57                         $("#edit-url-"+ id).focus();\r
58                         end_loading('#actions-'+id+' .button');\r
59                 }\r
60         );\r
61 }\r
62 \r
63 // Delete a link\r
64 function remove(id) {\r
65         if (!confirm('Really delete?')) {\r
66                 return;\r
67         }\r
68         var keyword = $('#keyword_'+id).val();\r
69         $.getJSON(\r
70                 "index_ajax.php",\r
71                 { mode: "delete", keyword: keyword },\r
72                 function(data){\r
73                         if (data.success == 1) {\r
74                                 $("#id-" + id).fadeOut(function(){$(this).remove();zebra_table();});\r
75                                 decrement();\r
76                         } else {\r
77                                 alert('something wrong happened while deleting :/');\r
78                         }\r
79                 }\r
80         );\r
81 }\r
82 \r
83 // Redirect to stat page\r
84 function stats(link) {\r
85         window.location=link;\r
86 }\r
87 \r
88 // Cancel edition of a link\r
89 function hide_edit(id) {\r
90         $("#edit-" + id).fadeOut(200, function(){\r
91                 end_disable('#actions-'+id+' .button');\r
92         });\r
93 }\r
94 \r
95 // Save edition of a link\r
96 function edit_save(id) {\r
97         add_loading("#edit-close-" + id);\r
98         var newurl = $("#edit-url-" + id).val();\r
99         var newkeyword = $("#edit-keyword-" + id).val();\r
100         var keyword = $('#old_keyword_'+id).val();\r
101         var www = $('#yourls-site').val();\r
102         $.getJSON(\r
103                 "index_ajax.php",\r
104                 {mode:'edit_save', url: newurl, keyword: keyword, newkeyword: newkeyword },\r
105                 function(data){\r
106                         if(data.status == 'success') {\r
107                                 $("#url-" + id).html('<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>');\r
108                                 $("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');\r
109                                 $("#timestamp-" + id).html(data.url.date);\r
110                                 $("#edit-" + id).fadeOut(200, function(){\r
111                                         $('#tblUrl tbody').trigger("update");\r
112                                 });\r
113                                 $('#keyword_'+id).val( newkeyword );\r
114                                 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );\r
115                         }\r
116                         feedback(data.message, data.status);\r
117                         end_disable("#edit-close-" + id);\r
118                         end_loading("#edit-close-" + id);\r
119                         end_disable("#edit-button-" + id);\r
120                         end_disable("#delete-button-" + id);\r
121                 }\r
122         );\r
123 }\r
124 \r
125 // Unused for now since HTTP Auth sucks donkeys.\r
126 function logout() {\r
127         $.ajax({\r
128                 type: "POST",\r
129                 url: "index_ajax.php",\r
130                 data: {mode:'logout'},\r
131                 success: function() {\r
132                         window.parent.location.href = window.parent.location.href;\r
133                 }\r
134         });\r
135 }\r
136 \r
137 // Begin the spinning animation & disable a button\r
138 function add_loading(el) {\r
139         $(el).attr("disabled", "disabled").addClass('disabled').addClass('loading');\r
140 }\r
141 \r
142 // End spinning animation\r
143 function end_loading(el) {\r
144         $(el).removeClass('loading');\r
145 }\r
146 \r
147 // Un-disable an element\r
148 function end_disable(el) {\r
149         $(el).removeAttr("disabled").removeClass('disabled');\r
150 }\r
151 \r
152 // Prettify table with odd & even rows\r
153 function zebra_table() {\r
154         $("#tblUrl tbody tr:even").removeClass('odd').addClass('even');\r
155         $("#tblUrl tbody tr:odd").removeClass('even').addClass('odd');\r
156         $('#tblUrl tbody').trigger("update");\r
157 }\r
158 \r
159 // Update feedback message\r
160 function feedback(msg, type) {\r
161         var span = (type == 'fail') ? '<span class="fail">' : '<span>' ;\r
162         var delay = (type == 'fail') ? 2500 : 1000 ;\r
163         $('#feedback').html(span + msg + '</span>').fadeIn(200,function(){\r
164                 $(this).animate({'opacity':1}, delay, function() {\r
165                         $(this).fadeOut(800);\r
166                 })\r
167         });\r
168 }\r
169 \r
170 // Ready to add another URL\r
171 function reset_url() {\r
172         $('#add-url').val('http://').focus();\r
173         $('#add-keyword').val('');\r
174 }\r
175 \r
176 // Increment URL counters\r
177 function increment() {\r
178         $('.increment').each(function(){\r
179                 $(this).html( parseInt($(this).html()) + 1);\r
180         });\r
181 }\r
182 \r
183 // Decrement URL counters\r
184 function decrement() {\r
185         $('.increment').each(function(){\r
186                 $(this).html( parseInt($(this).html()) - 1 );\r
187         });\r
188 }\r
189 \r
190 // Change an element text an revert in a smooth pulse. el is an element id like '#copybox h2'\r
191 function html_pulse( el, newtext ){\r
192         var oldtext = $(el).html();\r
193         // Fast pulse to "Copied" and revert\r
194         $(el).fadeTo(\r
195                 "normal",\r
196                 0.01,\r
197                 function(){\r
198                         $(el)\r
199                         .html( newtext )\r
200                         .css('opacity', 1)\r
201                         .fadeTo(\r
202                                 "slow", 1, // this fades from 1 to 1: just a 'sleep(1)' actually\r
203                                 function(){\r
204                                         $(el).fadeTo("normal", 0.01, function(){$(el).html( oldtext ).css('opacity', 1)});\r
205                                 }\r
206                         );\r
207                 }\r
208         );\r
209 \r
210 \r
211 }\r
212 \r
213 \r