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') ) {
21 // Create new link and add to table
23 if( $('#add-button').hasClass('disabled') ) {
26 var newurl = $("#add-url").val();
27 var nonce = $("#nonce-add").val();
28 if ( !newurl || newurl == 'http://' || newurl == 'https://' ) {
31 var keyword = $("#add-keyword").val();
32 add_loading("#add-button");
35 {action:'add', url: newurl, keyword: keyword, nonce: nonce},
37 if(data.status == 'success') {
38 $('#main_table tbody').prepend( data.html ).trigger("update");
39 $('#nourl_found').css('display', 'none');
42 toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );
46 end_loading("#add-button");
47 end_disable("#add-button");
49 feedback(data.message, data.status);
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();
65 // Display the edition interface
66 function edit_link_display(id) {
67 if( $('#edit-button-'+id).hasClass('disabled') ) {
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' );
75 { action: "edit_display", keyword: keyword, nonce: nonce, id: id },
77 $("#id-" + id).after( data.html );
78 $("#edit-url-"+ id).focus();
79 end_loading('#actions-'+id+' .button');
85 function remove_link(id) {
86 if( $('#delete-button-'+id).hasClass('disabled') ) {
89 if (!confirm('Really delete?')) {
92 var keyword = $('#keyword_'+id).val();
93 var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );
96 { action: "delete", keyword: keyword, nonce: nonce, id: id },
98 if (data.success == 1) {
99 $("#id-" + id).fadeOut(function(){
101 if( $('#main_table tbody tr').length == 1 ) {
102 $('#nourl_found').css('display', '');
109 alert('something wrong happened while deleting :/');
115 // Redirect to stat page
116 function go_stats(link) {
117 window.location=link;
120 // Cancel edition of a link
121 function edit_link_hide(id) {
122 $("#edit-" + id).fadeOut(200, function(){
123 end_disable('#actions-'+id+' .button');
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();
138 {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },
140 if(data.status == 'success') {
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>';
145 var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';
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");
154 $('#keyword_'+id).val( newkeyword );
155 $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );
157 feedback(data.message, data.status);
158 end_loading("#edit-close-" + id);
159 end_disable("#actions-" + id + ' .button');
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");
171 // Ready to add another URL
172 function add_link_reset() {
173 $('#add-url').val('http://').focus();
174 $('#add-keyword').val('');
177 // Increment URL counters
178 function increment_counter() {
179 $('.increment').each(function(){
180 $(this).html( parseInt($(this).html()) + 1);
184 // Decrement URL counters
185 function decrement_counter() {
186 $('.increment').each(function(){
187 $(this).html( parseInt($(this).html()) - 1 );
192 function toggle_share(id) {
193 if( $('#share-button-'+id).hasClass('disabled') ) {
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');
201 toggle_share_fill_boxes( longurl, shorturl, title );