]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - js/insert.js
Re-enable actions only if edit-save succeeded
[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                                 decrease_total_clicks( id );
115                         } else {
116                                 alert('something wrong happened while deleting :/');
117                         }
118                 }
119         );
120 }
121
122 // Redirect to stat page
123 function go_stats(link) {
124         window.location=link;
125 }
126
127 // Cancel edition of a link
128 function edit_link_hide(id) {
129         $("#edit-" + id).fadeOut(200, function(){
130                 end_disable('#actions-'+id+' .button');
131         });
132 }
133
134 // Save edition of a link
135 function edit_link_save(id) {
136         add_loading("#edit-close-" + id);
137         var newurl = encodeURI( $("#edit-url-" + id).val() );
138         var newkeyword = $("#edit-keyword-" + id).val();
139         var title = $("#edit-title-" + id).val();
140         var keyword = $('#old_keyword_'+id).val();
141         var nonce = $('#nonce_'+id).val();
142         var www = $('#yourls-site').val();
143         $.getJSON(
144                 ajaxurl,
145                 {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },
146                 function(data){
147                         if(data.status == 'success') {
148                         
149                                 if( data.url.title != '' ) {
150                                         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>';
151                                 } else {
152                                         var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';
153                                 }
154
155                                 $("#url-" + id).html(display_link);
156                                 $("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');
157                                 $("#timestamp-" + id).html(data.url.date);
158                                 $("#edit-" + id).fadeOut(200, function(){
159                                         $('#main_table tbody').trigger("update");
160                                 });
161                                 $('#keyword_'+id).val( newkeyword );
162                                 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );
163                         }
164                         feedback(data.message, data.status);
165                         end_loading("#edit-close-" + id);
166                         end_disable("#edit-close-" + id);
167                         if(data.status == 'success') {
168                                 end_disable("#actions-" + id + ' .button');
169                         }
170                 }
171         );
172 }
173
174 // Prettify table with odd & even rows
175 function zebra_table() {
176         $("#main_table tbody tr:even").removeClass('odd').addClass('even');
177         $("#main_table tbody tr:odd").removeClass('even').addClass('odd');
178         $('#main_table tbody').trigger("update");
179 }
180
181 // Ready to add another URL
182 function add_link_reset() {
183         $('#add-url').val('').focus();
184         $('#add-keyword').val('');
185 }
186
187 // Increment URL counters
188 function increment_counter() {
189         $('.increment').each(function(){
190                 $(this).html( parseInt($(this).html()) + 1);
191         });
192 }
193
194 // Decrement URL counters
195 function decrement_counter() {
196         $('.increment').each(function(){
197                 $(this).html( parseInt($(this).html()) - 1 );
198         });
199 }
200
201 // Decrease number of total clicks
202 function decrease_total_clicks( id ) {
203         var total_clicks = $("#overall_tracking strong:nth-child(2)");
204         total_clicks.html( parseInt( total_clicks.html() ) - parseInt( $('#clicks-' + id).html() ) );
205 }
206
207 // Toggle Share box
208 function toggle_share(id) {
209         if( $('#share-button-'+id).hasClass('disabled') ) {
210                 return false;
211         }
212         var link = $('#url-'+id+' a:first');
213         var longurl = link.attr('href');
214         var title = link.attr('title');
215         var shorturl = $('#keyword-'+id+' a:first').attr('href');
216         
217         toggle_share_fill_boxes( longurl, shorturl, title );
218 }
219
220 // When "Search" is clicked, split search text to beat servers which don't like query string with "http://"
221 // See https://github.com/YOURLS/YOURLS/issues/1576
222 function split_search_text_before_search() {
223         // Add 2 hidden fields and populate them with parts of search text
224         $("<input type='hidden' name='search_protocol' />").appendTo('#filter_form');
225         $("<input type='hidden' name='search_slashes' />").appendTo('#filter_form');
226         var search = get_protocol_slashes_and_rest( $('#filter_form input[name=search]').val() );
227         $('#filter_form input[name=search]').val( search.rest );
228         $('#filter_form input[name=search_protocol]').val( search.protocol );
229         $('#filter_form input[name=search_slashes]').val( search.slashes );
230 }
231