[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/js/ -> insert.js (source)

   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      var nextid = parseInt($('#main_table tbody tr[id^="id-"]').length) + 1;
  39      add_loading("#add-button");
  40      $.getJSON(
  41          ajaxurl,
  42          {action:'add', url: newurl, keyword: keyword, nonce: nonce, rowid: nextid},
  43          function(data){
  44              if(data.status == 'success') {
  45                  $('#main_table tbody').prepend( data.html ).trigger("update");
  46                  $('#nourl_found').css('display', 'none');
  47                  zebra_table();
  48                  increment_counter();
  49                  toggle_share_fill_boxes( data.url.url, data.shorturl, data.url.title );
  50              }
  51  
  52              add_link_reset();
  53              end_loading("#add-button");
  54              end_disable("#add-button");
  55  
  56              feedback(data.message, data.status);
  57          }
  58      );
  59  }
  60  
  61  function toggle_share_fill_boxes( url, shorturl, title ) {
  62      $('#copylink').val( shorturl );
  63      $('#titlelink').val( title );
  64      $('#origlink').attr( 'href', url ).html( url );
  65      $('#statlink').attr( 'href', shorturl+'+' ).html( shorturl+'+' );
  66      var tweet = ( title ? title + ' ' + shorturl : shorturl );
  67      $('#tweet_body').val( tweet ).keypress();
  68      $('#shareboxes').slideDown( '300', function(){ init_clipboard(); } ); // clipboard re-initialized after slidedown to make sure the invisible Flash element is correctly positionned
  69      $('#tweet_body').keypress();
  70  }
  71  
  72  // Display the edition interface
  73  function edit_link_display(id) {
  74      if( $('#edit-button-'+id).hasClass('disabled') ) {
  75          return false;
  76      }
  77      add_loading('#actions-'+id+' .button');
  78      var keyword = $('#keyword_'+id).val();
  79      var nonce = get_var_from_query( $('#edit-button-'+id).attr('href'), 'nonce' );
  80      $.getJSON(
  81          ajaxurl,
  82          { action: "edit_display", keyword: keyword, nonce: nonce, id: id },
  83          function(data){
  84              $("#id-" + id).after( data.html );
  85              $("#edit-url-"+ id).focus();
  86              end_loading('#actions-'+id+' .button');
  87          }
  88      );
  89  }
  90  
  91  // Delete a link
  92  function remove_link(id) {
  93      if( $('#delete-button-'+id).hasClass('disabled') ) {
  94          return false;
  95      }
  96      var dialog = $('#delete-confirm-dialog')[0];
  97      dialog.showModal();
  98  
  99      $('#delete-confirm-dialog input[name="keyword_id"]').val(id);
 100      var keyword = trim_long_string( $('#keyword-'+id+' > a').text() );
 101      $('#delete-confirm-dialog span[name="short_url"]').text(keyword);
 102      var title = trim_long_string( $('#url-'+id+' > a').attr('title') );
 103      $('#delete-confirm-dialog span[name="title"]').text(title);
 104      var url = trim_long_string( $('#url-'+id+' > a').attr('href') );
 105      $('#delete-confirm-dialog span[name="url"]').text(url);
 106  
 107      // Listen for dialog close event (handles Escape key)
 108      $(dialog).one('close', function() {
 109          if (document.activeElement) {
 110              document.activeElement.blur();
 111          }
 112      });
 113  
 114      // Show dialog, don't follow link
 115      return false;
 116  }
 117  
 118  function remove_link_confirmed(id) {
 119      var id = $('#delete-confirm-dialog input[name="keyword_id"]').val();
 120      var keyword = $('#keyword_'+id).val();
 121      var nonce = get_var_from_query( $('#delete-button-'+id).attr('href'), 'nonce' );
 122      $.getJSON(
 123          ajaxurl,
 124          { action: "delete", keyword: keyword, nonce: nonce, id: id },
 125          function(data){
 126              if (data.success == 1) {
 127                  $("#id-" + id).fadeOut(function(){
 128                      $(this).remove();
 129                      if( $('#main_table tbody tr').length  == 1 ) {
 130                          $('#nourl_found').css('display', '');
 131                      }
 132  
 133                      zebra_table();
 134                  });
 135                  decrement_counter();
 136                  decrease_total_clicks( id );
 137              } else {
 138                  feedback('something wrong happened while deleting!' , 'fail');
 139              }
 140              $('#delete-confirm-dialog')[0].close();
 141          }
 142      );
 143  }
 144  
 145  function remove_link_canceled() {
 146      $('#delete-confirm-dialog')[0].close();
 147  
 148      // Force blur on all interactive elements to restore action buttons opacity
 149      if (document.activeElement) {
 150          document.activeElement.blur();
 151      }
 152  }
 153  
 154  // Redirect to stat page
 155  function go_stats(link) {
 156      window.location=link;
 157  }
 158  
 159  // Cancel edition of a link
 160  function edit_link_hide(id) {
 161      $("#edit-" + id).fadeOut(200, function(){
 162          $("#edit-" + id).remove();
 163          end_disable('#actions-'+id+' .button');
 164      });
 165  }
 166  
 167  // Save edition of a link
 168  function edit_link_save(id) {
 169      add_loading("#edit-close-" + id);
 170      var newurl = $("#edit-url-" + id).val();
 171      var newkeyword = $("#edit-keyword-" + id).val();
 172      var title = $("#edit-title-" + id).val();
 173      var keyword = $('#old_keyword_'+id).val();
 174      var nonce = $('#nonce_'+id).val();
 175      var www = $('#yourls-site').val();
 176      $.getJSON(
 177          ajaxurl,
 178          {action:'edit_save', url: newurl, id: id, keyword: keyword, newkeyword: newkeyword, title: title, nonce: nonce },
 179          function(data){
 180              if(data.status == 'success') {
 181  
 182                  if( data.url.title != '' ) {
 183                      var display_link = '<a href="' + data.url.url + '" title="' + data.url.title + '">' + data.url.display_title + '</a><br/><small><a href="' + data.url.url + '">' + data.url.display_url + '</a></small>';
 184                  } else {
 185                      var display_link = '<a href="' + data.url.url + '" title="' + data.url.url + '">' + data.url.display_url + '</a>';
 186                  }
 187  
 188                  $("#url-" + id).html(display_link);
 189                  $("#keyword-" + id).html('<a href="' + data.url.shorturl + '" title="' + data.url.shorturl + '">' + data.url.keyword + '</a>');
 190                  $("#edit-" + id).fadeOut(200, function(){
 191                      $("#edit-" + id).remove();
 192                      $('#main_table tbody').trigger("update");
 193                  });
 194                  $('#keyword_'+id).val( newkeyword );
 195                  $('#statlink-'+id).attr( 'href', data.url.shorturl+'+' );
 196              }
 197              feedback(data.message, data.status);
 198              end_loading("#edit-close-" + id);
 199              end_disable("#edit-close-" + id);
 200              if(data.status == 'success') {
 201                  end_disable("#actions-" + id + ' .button');
 202              }
 203          }
 204      );
 205  }
 206  
 207  // Prettify table with odd & even rows
 208  function zebra_table() {
 209      $("#main_table tbody tr:even").removeClass('odd').addClass('even');
 210      $("#main_table tbody tr:odd").removeClass('even').addClass('odd');
 211      $('#main_table tbody').trigger("update");
 212  }
 213  
 214  // Ready to add another URL
 215  function add_link_reset() {
 216      $('#add-url').val('').focus();
 217      $('#add-keyword').val('');
 218  }
 219  
 220  // Increment URL counters
 221  function increment_counter() {
 222      $('.increment').each(function(){
 223          $(this).html( parseInt($(this).html()) + 1);
 224      });
 225  }
 226  
 227  // Decrement URL counters
 228  function decrement_counter() {
 229      $('.increment').each(function(){
 230          $(this).html( parseInt($(this).html()) - 1 );
 231      });
 232  }
 233  
 234  // Decrease number of total clicks
 235  function decrease_total_clicks( id ) {
 236      var total_clicks = $("#overall_tracking strong:nth-child(2)");
 237      total_clicks.html( parseInt( total_clicks.html() ) - parseInt( $('#clicks-' + id).html() ) );
 238  }
 239  
 240  // Toggle Share box
 241  function toggle_share(id) {
 242      if( $('#share-button-'+id).hasClass('disabled') ) {
 243          return false;
 244      }
 245      var link = $('#url-'+id+' a:first');
 246      var longurl = link.attr('href');
 247      var title = link.attr('title');
 248      var shorturl = $('#keyword-'+id+' a:first').attr('href');
 249  
 250      toggle_share_fill_boxes( longurl, shorturl, title );
 251  }
 252  
 253  // When "Search" is clicked, split search text to beat servers which don't like query string with "http://"
 254  // See https://github.com/YOURLS/YOURLS/issues/1576
 255  function split_search_text_before_search() {
 256      // Add 2 hidden fields and populate them with parts of search text
 257      $("<input type='hidden' name='search_protocol' />").appendTo('#filter_form');
 258      $("<input type='hidden' name='search_slashes' />").appendTo('#filter_form');
 259      var search = get_protocol_slashes_and_rest( $('#filter_form input[name=search]').val() );
 260      $('#filter_form input[name=search]').val( search.rest );
 261      $('#filter_form input[name=search_protocol]').val( search.protocol );
 262      $('#filter_form input[name=search_slashes]').val( search.slashes );
 263  }


Generated: Wed Jan 7 05:11:05 2026 Cross-referenced by PHPXref 0.7.1