2 $(document).ready(function(){
3 $('#add-url, #add-keyword').keypress(function(e){
4 if (e.which == 13) {add_link();}
7 $('#new_url_form').attr('action', 'javascript:add_link();');
9 $('input.text').focus(function(){
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') ) {
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();
27 // Create new link and add to table
29 if( $('#add-button').hasClass('disabled') ) {
32 var newurl = $("#add-url").val();
33 var nonce = $("#nonce-add").val();
34 if ( !newurl || newurl == 'http://' || newurl == 'https://' ) {
37 var keyword = $("#add-keyword").val();
38 add_loading("#add-button");
41 {action:'add', url: newurl, keyword: keyword, nonce: nonce},
43 if(data.status == 'success') {
44 $('#main_table tbody').prepend( data.html ).trigger("update");
45 $('#nourl_found').css('display', 'none');
48 toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );
52 end_loading("#add-button");
53 end_disable("#add-button");
55 feedback(data.message, data.status);
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();
71 // Display the edition interface
72 function edit_link_display(id) {
73 if( $('#edit-button-'+id).hasClass('disabled') ) {
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' );
81 { action: "edit_display", keyword: keyword, nonce: nonce, id: id },
83 $("#id-" + id).after( data.html );
84 $("#edit-url-"+ id).focus();
85 end_loading('#actions-'+id+' .button');
91 function remove_link(id) {
92 if( $('#delete-button-'+id).hasClass('disabled') ) {
95 if (!confirm('Really delete?')) {
98 var keyword = $('#keyword_'+id).val();
99 var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );
102 { action: "delete", keyword: keyword, nonce: nonce, id: id },
104 if (data.success == 1) {
105 $("#id-" + id).fadeOut(function(){
107 if( $('#main_table tbody tr').length == 1 ) {
108 $('#nourl_found').css('display', '');
114 decrease_total_clicks( id );
116 alert('something wrong happened while deleting :/');
122 // Redirect to stat page
123 function go_stats(link) {
124 window.location=link;
127 // Cancel edition of a link
128 function edit_link_hide(id) {
129 $("#edit-" + id).fadeOut(200, function(){
130 end_disable('#actions-'+id+' .button');
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();
145 {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },
147 if(data.status == 'success') {
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>';
152 var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';
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");
161 $('#keyword_'+id).val( newkeyword );
162 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );
164 feedback(data.message, data.status);
165 end_loading("#edit-close-" + id);
166 end_disable("#edit-close-" + id);
167 end_disable("#actions-" + id + ' .button');
172 // Prettify table with odd & even rows
173 function zebra_table() {
174 $("#main_table tbody tr:even").removeClass('odd').addClass('even');
175 $("#main_table tbody tr:odd").removeClass('even').addClass('odd');
176 $('#main_table tbody').trigger("update");
179 // Ready to add another URL
180 function add_link_reset() {
181 $('#add-url').val('').focus();
182 $('#add-keyword').val('');
185 // Increment URL counters
186 function increment_counter() {
187 $('.increment').each(function(){
188 $(this).html( parseInt($(this).html()) + 1);
192 // Decrement URL counters
193 function decrement_counter() {
194 $('.increment').each(function(){
195 $(this).html( parseInt($(this).html()) - 1 );
199 // Decrease number of total clicks
200 function decrease_total_clicks( id ) {
201 var total_clicks = $("#overall_tracking strong:nth-child(2)");
202 total_clicks.html( parseInt( total_clicks.html() ) - parseInt( $('#clicks-' + id).html() ) );
206 function toggle_share(id) {
207 if( $('#share-button-'+id).hasClass('disabled') ) {
210 var link = $('#url-'+id+' a:first');
211 var longurl = link.attr('href');
212 var title = link.attr('title');
213 var shorturl = $('#keyword-'+id+' a:first').attr('href');
215 toggle_share_fill_boxes( longurl, shorturl, title );
218 // When "Search" is clicked, split search text to beat servers which don't like query string with "http://"
219 // See https://github.com/YOURLS/YOURLS/issues/1576
220 function split_search_text_before_search() {
221 // Add 2 hidden fields and populate them with parts of search text
222 $("<input type='hidden' name='search_protocol' />").appendTo('#filter_form');
223 $("<input type='hidden' name='search_slashes' />").appendTo('#filter_form');
224 var search = get_protocol_slashes_and_rest( $('#filter_form input[name=search]').val() );
225 $('#filter_form input[name=search]').val( search.rest );
226 $('#filter_form input[name=search_protocol]').val( search.protocol );
227 $('#filter_form input[name=search_slashes]').val( search.slashes );