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