]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/insert.js
Don't prefill empty input field. Fixes #1716.
[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         // When Searching, explode search text in pieces -- see split_search_text_before_search()
21         $('#filter_form').submit( function(){
22                 split_search_text_before_search();
23                 return true;
24         });
25 });
26
27 // Create new link and add to table
28 function add_link() {
29         if( $('#add-button').hasClass('disabled') ) {
30                 return false;
31         }
32         var newurl = $("#add-url").val();
33         var nonce = $("#nonce-add").val();
34         if ( !newurl || newurl == 'http://' || newurl == 'https://' ) {
35                 return;
36         }
37         var keyword = $("#add-keyword").val();
38         add_loading("#add-button");
39         $.getJSON(
40                 ajaxurl,
41                 {action:'add', url: newurl, keyword: keyword, nonce: nonce},
42                 function(data){
43                         if(data.status == 'success') {
44                                 $('#main_table tbody').prepend( data.html ).trigger("update");
45                                 $('#nourl_found').css('display', 'none');
46                                 zebra_table();
47                                 increment_counter();
48                                 toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );
49                         }
50
51                         add_link_reset();
52                         end_loading("#add-button");
53                         end_disable("#add-button");
54
55                         feedback(data.message, data.status);
56                 }
57         );
58 }
59
60 function toggle_share_fill_boxes( url, shorturl, title ) {
61         $('#copylink').val( shorturl );
62         $('#titlelink').val( title );
63         $('#origlink').attr( 'href', url ).html( url );
64         $('#statlink').attr( 'href', shorturl+'+' ).html( shorturl+'+' );
65         var tweet = ( title ? title + ' ' + shorturl : shorturl );
66         $('#tweet_body').val( tweet ).keypress();
67         $('#shareboxes').slideDown( '300', function(){ init_clipboard(); } ); // clipboard re-initialized after slidedown to make sure the invisible Flash element is correctly positionned
68         $('#tweet_body').keypress();
69 }
70
71 // Display the edition interface
72 function edit_link_display(id) {
73         if( $('#edit-button-'+id).hasClass('disabled') ) {
74                 return false;
75         }
76         add_loading('#actions-'+id+' .button');
77         var keyword = $('#keyword_'+id).val();
78         var nonce = get_var_from_query( $('#edit-button-'+id).attr('href'), 'nonce' );
79         $.getJSON(
80                 ajaxurl,
81                 { action: "edit_display", keyword: keyword, nonce: nonce, id: id },
82                 function(data){
83                         $("#id-" + id).after( data.html );
84                         $("#edit-url-"+ id).focus();
85                         end_loading('#actions-'+id+' .button');
86                 }
87         );
88 }
89
90 // Delete a link
91 function remove_link(id) {
92         if( $('#delete-button-'+id).hasClass('disabled') ) {
93                 return false;
94         }
95         if (!confirm('Really delete?')) {
96                 return;
97         }
98         var keyword = $('#keyword_'+id).val();
99         var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );
100         $.getJSON(
101                 ajaxurl,
102                 { action: "delete", keyword: keyword, nonce: nonce, id: id },
103                 function(data){
104                         if (data.success == 1) {
105                                 $("#id-" + id).fadeOut(function(){
106                                         $(this).remove();
107                                         if( $('#main_table tbody tr').length  == 1 ) {
108                                                 $('#nourl_found').css('display', '');
109                                         }
110
111                                         zebra_table();
112                                 });
113                                 decrement_counter();
114                         } else {
115                                 alert('something wrong happened while deleting :/');
116                         }
117                 }
118         );
119 }
120
121 // Redirect to stat page
122 function go_stats(link) {
123         window.location=link;
124 }
125
126 // Cancel edition of a link
127 function edit_link_hide(id) {
128         $("#edit-" + id).fadeOut(200, function(){
129                 end_disable('#actions-'+id+' .button');
130         });
131 }
132
133 // Save edition of a link
134 function edit_link_save(id) {
135         add_loading("#edit-close-" + id);
136         var newurl = encodeURI( $("#edit-url-" + id).val() );
137         var newkeyword = $("#edit-keyword-" + id).val();
138         var title = $("#edit-title-" + id).val();
139         var keyword = $('#old_keyword_'+id).val();
140         var nonce = $('#nonce_'+id).val();
141         var www = $('#yourls-site').val();
142         $.getJSON(
143                 ajaxurl,
144                 {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },
145                 function(data){
146                         if(data.status == 'success') {
147                         
148                                 if( data.url.title != '' ) {
149                                         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>';
150                                 } else {
151                                         var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';
152                                 }
153
154                                 $("#url-" + id).html(display_link);
155                                 $("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');
156                                 $("#timestamp-" + id).html(data.url.date);
157                                 $("#edit-" + id).fadeOut(200, function(){
158                                         $('#main_table tbody').trigger("update");
159                                 });
160                                 $('#keyword_'+id).val( newkeyword );
161                                 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );
162                         }
163                         feedback(data.message, data.status);
164                         end_loading("#edit-close-" + id);
165                         end_disable("#actions-" + id + ' .button');
166                 }
167         );
168 }
169
170 // Prettify table with odd & even rows
171 function zebra_table() {
172         $("#main_table tbody tr:even").removeClass('odd').addClass('even');
173         $("#main_table tbody tr:odd").removeClass('even').addClass('odd');
174         $('#main_table tbody').trigger("update");
175 }
176
177 // Ready to add another URL
178 function add_link_reset() {
179         $('#add-url').val('').focus();
180         $('#add-keyword').val('');
181 }
182
183 // Increment URL counters
184 function increment_counter() {
185         $('.increment').each(function(){
186                 $(this).html( parseInt($(this).html()) + 1);
187         });
188 }
189
190 // Decrement URL counters
191 function decrement_counter() {
192         $('.increment').each(function(){
193                 $(this).html( parseInt($(this).html()) - 1 );
194         });
195 }
196
197 // Toggle Share box
198 function toggle_share(id) {
199         if( $('#share-button-'+id).hasClass('disabled') ) {
200                 return false;
201         }
202         var link = $('#url-'+id+' a:first');
203         var longurl = link.attr('href');
204         var title = link.attr('title');
205         var shorturl = $('#keyword-'+id+' a:first').attr('href');
206         
207         toggle_share_fill_boxes( longurl, shorturl, title );
208 }
209
210 // When "Search" is clicked, split search text to beat servers which don't like query string with "http://"
211 // See https://github.com/YOURLS/YOURLS/issues/1576
212 function split_search_text_before_search() {
213         // Add 2 hidden fields and populate them with parts of search text
214         $("<input type='hidden' name='search_protocol' />").appendTo('#filter_form');
215         $("<input type='hidden' name='search_slashes' />").appendTo('#filter_form');
216         var search = get_protocol_slashes_and_rest( $('#filter_form input[name=search]').val() );
217         $('#filter_form input[name=search]').val( search.rest );
218         $('#filter_form input[name=search_protocol]').val( search.protocol );
219         $('#filter_form input[name=search_slashes]').val( search.slashes );
220 }
221