Skip to content
Snippets Groups Projects
scp.js 38.6 KiB
Newer Older
  • Learn to ignore specific revisions
  •     $(window).unbind('beforeunload');
    
        // Close popups
        $('.dialog .body').empty().parent().hide();
    
        $.toggleOverlay(false);
    
        $('.tip_box').remove();
    
    
    $(document).on('pjax:send', function(event) {
    
        if ($('#loadingbar').length !== 0) {
            $('#loadingbar').remove();
        }
    
        $("body").append("<div id='loadingbar'></div>");
        $("#loadingbar").addClass("waiting").append($("<dt/><dd/>"));
    
        // right
        $('#loadingbar').stop(false, true).width((50 + Math.random() * 30) + "%");
    
        $('#overlay').css('background-color','white');
        $.toggleOverlay(true);
    
    });
    
    $(document).on('pjax:complete', function() {
    
        // right
        $("#loadingbar").width("101%").delay(200).fadeOut(400, function() {
            $(this).remove();
        });
    
        $.toggleOverlay(false);
        $('#overlay').removeAttr('style');
    
    // Enable PJAX for the staff interface
    if ($.support.pjax) {
      $(document).on('click', 'a', function(event) {
        var $this = $(this);
        if (!$this.hasClass('no-pjax')
            && !$this.closest('.no-pjax').length
            && $this.attr('href')[0] != '#')
          $.pjax.click(event, {container: $this.data('pjaxContainer') || $('#pjax-container'), timeout: 2000});
      })
    }
    
    
    // Quick-Add dialogs
    $(document).on('change', 'select[data-quick-add]', function() {
        var $select = $(this),
    
    Jared Hancock's avatar
    Jared Hancock committed
            selected = $select.find('option:selected'),
            type = selected.parent().closest('[data-quick-add]').data('quickAdd');
        if (!type || (selected.data('quickAdd') === undefined && selected.val() !== ':new:'))
    
            return;
    
    Jared Hancock's avatar
    Jared Hancock committed
        $.dialog('ajax.php/admin/quick-add/' + type, 201,
    
        function(xhr, data) {
            data = JSON.parse(data);
            if (data && data.id && data.name) {
    
    Jared Hancock's avatar
    Jared Hancock committed
              var id = data.id;
              if (selected.data('idPrefix'))
                id = selected.data('idPrefix') + id;
    
              $('<option>')
    
    Jared Hancock's avatar
    Jared Hancock committed
                .attr('value', id)
    
                .text(data.name)
    
                .insertBefore(selected)
    
    Jared Hancock's avatar
    Jared Hancock committed
              $select.val(id);
    
    // Quick note interface
    
    $(document).on('click.note', '.quicknote .action.edit-note', function() {
    
        var note = $(this).closest('.quicknote'),
            body = note.find('.body'),
            T = $('<textarea>').text(body.html());
    
    Peter Rotich's avatar
    Peter Rotich committed
        if (note.closest('.dialog, .tip_box').length)
    
            T.addClass('no-bar small');
        body.replaceWith(T);
        $.redact(T);
    
    Jared Hancock's avatar
    Jared Hancock committed
        $(T).redactor('focus.setStart');
    
        note.find('.action.edit-note').hide();
        note.find('.action.save-note').show();
        note.find('.action.cancel-edit').show();
    
        $('#new-note-box').hide();
    
    $(document).on('click.note', '.quicknote .action.cancel-edit', function() {
    
        var note = $(this).closest('.quicknote'),
            T = note.find('textarea'),
            body = $('<div class="body">');
        body.load('ajax.php/note/' + note.data('id'), function() {
    
    Jared Hancock's avatar
    Jared Hancock committed
          try { T.redactor('core.destroy'); } catch (e) {}
    
          T.replaceWith(body);
          note.find('.action.save-note').hide();
          note.find('.action.cancel-edit').hide();
          note.find('.action.edit-note').show();
    
          $('#new-note-box').show();
    
    $(document).on('click.note', '.quicknote .action.save-note', function() {
    
        var note = $(this).closest('.quicknote'),
            T = note.find('textarea');
        $.post('ajax.php/note/' + note.data('id'),
    
    Jared Hancock's avatar
    Jared Hancock committed
          { note: T.redactor('code.get') },
    
          function(html) {
            var body = $('<div class="body">').html(html);
    
    Jared Hancock's avatar
    Jared Hancock committed
            try { T.redactor('core.destroy'); } catch (e) {}
    
            T.replaceWith(body);
            note.find('.action.save-note').hide();
            note.find('.action.cancel-edit').hide();
            note.find('.action.edit-note').show();
    
            $('#new-note-box').show();
    
    $(document).on('click.note', '.quicknote .delete', function() {
    
      var that = $(this),
          id = $(this).closest('.quicknote').data('id');
      $.ajax('ajax.php/note/' + id, {
        type: 'delete',
        success: function() {
          that.closest('.quicknote').animate(
            {height: 0, opacity: 0}, 'slow', function() {
              $(this).remove();
          });
        }
      });
      return false;
    });
    
    $(document).on('click', '#new-note', function() {
    
      var note = $(this).closest('.quicknote'),
        T = $('<textarea>'),
    
        button = $('<input type="button">').val(__('Create'));
    
        button.click(function() {
    
          $.post('ajax.php/' + note.data('url'),
    
    Jared Hancock's avatar
    Jared Hancock committed
            { note: T.redactor('code.get'), no_options: note.hasClass('no-options') },
    
    Jared Hancock's avatar
    Jared Hancock committed
              $(T).redactor('core.destroy').replaceWith(note);
    
              $(response).show('highlight').insertBefore(note.parent());
    
              $('.submit', note.parent()).remove();
            },
            'html'
          );
        });
    
    Peter Rotich's avatar
    Peter Rotich committed
        if (note.closest('.dialog, .tip_box').length)
    
            T.addClass('no-bar small');
        note.replaceWith(T);
        $('<p>').addClass('submit').css('text-align', 'center')
            .append(button).appendTo(T.parent());
        $.redact(T);
    
    Jared Hancock's avatar
    Jared Hancock committed
        $(T).redactor('focus.setStart');
    
    
    function __(s) {
    
      if ($.oststrings && $.oststrings[s])
        return $.oststrings[s];
    
    
    // Thanks, http://stackoverflow.com/a/487049
    function addSearchParam(key, value) {
        key = encodeURI(key); value = encodeURI(value);
    
        var kvp = document.location.search.substr(1).split('&');
        var i=kvp.length; var x;
        while (i--) {
            x = kvp[i].split('=');
            if (x[0]==key) {
                x[1] = value;
                kvp[i] = x.join('=');
                break;
            }
        }
        if(i<0) {kvp[kvp.length] = [key,value].join('=');}
    
        //this will reload the page, it's likely better to store this until finished
    
        return kvp.join('&');
    
    
    // Periodically adjust relative times
    window.relativeAdjust = setInterval(function() {
    
      // Thanks, http://stackoverflow.com/a/7641822/1025836
    
      var prettyDate = function(time) {
        var date = new Date((time || "").replace(/-/g, "/").replace(/[TZ]/g, " ")),
            diff = (((new Date()).getTime() - date.getTime()) / 1000),
            day_diff = Math.floor(diff / 86400);
    
        if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31) return;
    
        return day_diff == 0 && (
             diff < 60 && __("just now")
          || diff < 120 && __("about a minute ago")
          || diff < 3600 && __("%d minutes ago").replace('%d', Math.floor(diff/60))
          || diff < 7200 && __("about an hour ago")
    
          || diff < 86400 &&  __("%d hours ago").replace('%d', Math.floor(diff/3600))
    
        )
        || day_diff == 1 && __("yesterday")
        || day_diff < 7 && __("%d days ago").replace('%d', day_diff);
        // Longer dates don't need to change dynamically
      };
      $('time.relative[datetime]').each(function() {
        var rel = prettyDate($(this).attr('datetime'));
        if (rel) $(this).text(rel);
      });
    }, 20000);