Changeset - d66201a7ce6e
[Not reviewed]
default
0 2 0
Mads Kiilerich - 7 years ago 2018-12-25 20:23:42
mads@kiilerich.com
files: change "callbacks" function to the more descriptive name "post_load_state" and let it take an actual state data object

Makes the function more independent of native.history.js .
2 files changed with 11 insertions and 17 deletions:
0 comments (0 inline, 0 general)
kallithea/templates/files/files.html
Show inline comments
 
@@ -45,110 +45,109 @@ var url_base = ${h.js(h.url("files_home"
 
var node_list_url = ${h.js(h.url("files_nodelist_home",repo_name=c.repo_name,revision='__REV__',f_path='__FPATH__'))};
 

	
 
## new pyroutes URLs
 
pyroutes.register('files_nodelist_home', ${h.js(h.url('files_nodelist_home', repo_name=c.repo_name,revision='%(revision)s',f_path='%(f_path)s'))}, ['revision', 'f_path']);
 
pyroutes.register('files_history_home', ${h.js(h.url('files_history_home', repo_name=c.repo_name,revision='%(revision)s',f_path='%(f_path)s'))}, ['revision', 'f_path']);
 
pyroutes.register('files_authors_home', ${h.js(h.url('files_authors_home', repo_name=c.repo_name,revision='%(revision)s',f_path='%(f_path)s'))}, ['revision', 'f_path']);
 

	
 
var ypjax_links = function(){
 
    $('.ypjax-link').click(function(e){
 

	
 
        //don't do ypjax on middle click
 
        if(e.which == 2 || !History.enabled){
 
            return true;
 
        }
 

	
 
        var el = e.currentTarget;
 
        var url = el.href;
 

	
 
        var _base_url = ${h.jshtml(h.url("files_home",repo_name=c.repo_name,revision='',f_path=''))};
 
        _base_url = _base_url.replace('//','/');
 

	
 
        //extract rev and the f_path from url.
 
        parts = url.split(_base_url);
 
        if(parts.length != 2){
 
            return false;
 
        }
 

	
 
        var parts2 = parts[1].split('/');
 
        var rev = parts2.shift(); // pop the first element which is the revision
 
        var f_path = parts2.join('/');
 

	
 
        //page title - make this consistent with title mako block above
 
        var title = ${h.jshtml(_('%s Files') % c.repo_name)} + " \u00B7 " + (f_path || '/') + " \u00B7 " + ${h.jshtml(c.site_name)};
 

	
 
        var _node_list_url = node_list_url.replace('__REV__',rev).replace('__FPATH__', f_path);
 
        var _url_base = url_base.replace('__REV__',rev);
 

	
 
        // Change our States and save some data for handling events
 
        var data = {url:url,title:title, url_base:_url_base,
 
                    node_list_url:_node_list_url, rev:rev, f_path:f_path};
 
        History.pushState(data, title, url);
 

	
 
        //now we're sure that we can do ypjax things
 
        e.preventDefault();
 
        return false;
 
    });
 
}
 

	
 
// callbacks needed to process the ypjax filebrowser
 
var callbacks = function(State){
 
var post_load_state = function(state) {
 
    ypjax_links();
 
    tooltip_activate();
 

	
 
    if(State !== undefined){
 
    if(state !== undefined) {
 
        //initially loaded stuff
 
        var _f_path = State.data.f_path;
 
        var _rev = State.data.rev;
 
        var _f_path = state.f_path;
 
        var _rev = state.rev;
 

	
 
        fileBrowserListeners(State.data.node_list_url, State.data.url_base);
 
        fileBrowserListeners(state.node_list_url, state.url_base);
 
        // Inform Google Analytics of the change
 
        if ( typeof window.pageTracker !== 'undefined' ) {
 
            window.pageTracker._trackPageview(State.url);
 
            window.pageTracker._trackPageview(state.url);
 
        }
 
    }
 

	
 
    function highlight_lines(lines){
 
        for(pos in lines){
 
          $('#L'+lines[pos]).css('background-color','#FFFFBE');
 
        }
 
    }
 
    page_highlights = location.href.substring(location.href.indexOf('#')+1).split('L');
 
    if (page_highlights.length == 2){
 
       highlight_ranges  = page_highlights[1].split(",");
 

	
 
       var h_lines = [];
 
       for (pos in highlight_ranges){
 
            var _range = highlight_ranges[pos].split('-');
 
            if(_range.length == 2){
 
                var start = parseInt(_range[0]);
 
                var end = parseInt(_range[1]);
 
                if (start < end){
 
                    for(var i=start;i<=end;i++){
 
                        h_lines.push(i);
 
                    }
 
                }
 
            }
 
            else{
 
                h_lines.push(parseInt(highlight_ranges[pos]));
 
            }
 
      }
 
      highlight_lines(h_lines);
 
      $('#L'+h_lines[0]).each(function(){
 
          this.scrollIntoView();
 
      });
 
    }
 

	
 
    // select code link event
 
    $('#hlcode').mouseup(getSelectionLink);
 

	
 
    // history select field
 
    var cache = {};
 
    $("#diff1").select2({
 
        placeholder: _TM['Select changeset'],
 
        dropdownAutoWidth: true,
 
        maxResults: 50,
 
        query: function(query){
 
          var key = 'cache';
 
          var cached = cache[key] ;
 
          if(cached) {
 
            var data = {results: []};
 
@@ -166,94 +165,93 @@ var callbacks = function(State){
 
                });
 
                children = branchSort(children, undefined, query)
 
                data.results.push({'text': section, 'children': children});
 
            });
 
            //push the typed in changeset
 
            data.results.push({'text':_TM['Specify changeset'],
 
                               'children': [{'id': query.term, 'text': query.term, 'type': 'rev'}]});
 
            query.callback(data);
 
          }else{
 
              $.ajax({
 
                url: pyroutes.url('files_history_home', {'revision': _rev, 'f_path': _f_path}),
 
                data: {},
 
                dataType: 'json',
 
                type: 'GET',
 
                success: function(data) {
 
                  cache[key] = data;
 
                  query.callback(data);
 
                }
 
              });
 
          }
 
        }
 
    });
 
    $('#show_authors').on('click', function(){
 
        $.ajax({
 
            url: pyroutes.url('files_authors_home', {'revision': _rev, 'f_path': _f_path}),
 
            success: function(data) {
 
                $('#file_authors').html(data);
 
                $('#file_authors').show();
 
                tooltip_activate();
 
            }
 
        });
 
    });
 
}
 

	
 
$(document).ready(function(){
 
    ypjax_links();
 
    var $files_data = $('#files_data');
 
    //Bind to StateChange Event
 
    History.Adapter.bind(window,'statechange',function(){
 
        var State = History.getState();
 
        cache_key = State.url;
 
        //check if we have this request in cache maybe ?
 
        var _cache_obj = CACHE[cache_key];
 
        var _cur_time = new Date().getTime();
 
        // get from cache if it's there and not yet expired !
 
        if(_cache_obj !== undefined && _cache_obj[0] > _cur_time){
 
            $files_data.html(_cache_obj[1]);
 
            $files_data.css('opacity','1.0');
 
            //callbacks after ypjax call
 
            callbacks(State);
 
            post_load_state(State.data);
 
        }
 
        else{
 
            asynchtml(State.url, $files_data, function(){
 
                    callbacks(State);
 
                    post_load_state(State.data);
 
                    var expire_on = new Date().getTime() + CACHE_EXPIRE;
 
                    CACHE[cache_key] = [expire_on, $files_data.html()];
 
                });
 
        }
 
    });
 

	
 
    // init the search filter
 
    var _State = {
 
       url: ${h.js(h.url.current())},
 
       data: {
 
         node_list_url: node_list_url.replace('__REV__',${h.js(c.changeset.raw_id)}).replace('__FPATH__', ${h.js(h.safe_unicode(c.file.path))}),
 
         url_base: url_base.replace('__REV__',${h.js(c.changeset.raw_id)}),
 
         rev:${h.js(c.changeset.raw_id)},
 
         f_path: ${h.js(h.safe_unicode(c.file.path))}
 
       }
 
    }
 
    fileBrowserListeners(_State.data.node_list_url, _State.data.url_base);
 

	
 
    // change branch filter
 
    $("#branch_selector").select2({
 
        dropdownAutoWidth: true,
 
        maxResults: 50,
 
        sortResults: branchSort
 
        });
 

	
 
    $("#branch_selector").change(function(e){
 
        var selected = e.currentTarget.options[e.currentTarget.selectedIndex].value;
 
        if(selected && selected != ${h.js(c.changeset.raw_id)}){
 
            window.location = pyroutes.url('files_home', {'repo_name': ${h.js(h.safe_unicode(c.repo_name))}, 'revision': selected, 'f_path': ${h.js(h.safe_unicode(c.file.path))}});
 
            $("#body").hide();
 
        } else {
 
            $("#branch_selector").val(${h.js(c.changeset.raw_id)});
 
        }
 
    });
 

	
 
});
 

	
 
</script>
 

	
 
</%def>
kallithea/templates/files/files_source.html
Show inline comments
 
@@ -35,61 +35,57 @@
 
                ${h.link_to(_('Delete'), h.url('files_delete_home',repo_name=c.repo_name,revision=c.changeset.raw_id,f_path=c.f_path, anchor='edit'),class_="btn btn-danger btn-xs")}
 
               %elif c.on_branch_head and c.file.is_binary:
 
                ${h.link_to(_('Edit'), '#', class_="btn btn-default btn-xs disabled", title=_('Editing binary files not allowed'),**{'data-toggle':'tooltip'})}
 
                ${h.link_to(_('Delete'), h.url('files_delete_home',repo_name=c.repo_name,revision=c.changeset.raw_id,f_path=c.f_path, anchor='edit'),class_="btn btn-danger btn-xs")}
 
               %else:
 
                ${h.link_to(_('Edit'), '#', class_="btn btn-default btn-xs disabled", title=_('Editing files allowed only when on branch head revision'),**{'data-toggle':'tooltip'})}
 
                ${h.link_to(_('Delete'), '#', class_="btn btn-danger btn-xs disabled", title=_('Deleting files allowed only when on branch head revision'),**{'data-toggle':'tooltip'})}
 
               %endif
 
              %endif
 
        </span>
 
    </div>
 
    <div class="panel-body">
 
      <div class="author">
 
            ${h.gravatar_div(h.email_or_none(c.changeset.author), size=16)}
 
            <div title="${c.changeset.author}" class="user">${h.person(c.changeset.author)}</div>
 
      </div>
 
      <div class="formatted-fixed">${h.urlify_text(c.changeset.message,c.repo_name)}</div>
 
    </div>
 
    <div class="panel-body no-padding">
 
      %if c.file.is_browser_compatible_image():
 
        <img src="${h.url('files_raw_home',repo_name=c.repo_name,revision=c.changeset.raw_id,f_path=c.f_path)}" class="img-preview"/>
 
      %elif c.file.is_binary:
 
        <div>
 
          ${_('Binary file (%s)') % c.file.mimetype}
 
        </div>
 
      %else:
 
        %if c.file.size < c.cut_off_limit or c.fulldiff:
 
            %if c.annotate:
 
              ${h.pygmentize_annotation(c.repo_name,c.file,linenos=True,anchorlinenos=True,lineanchors='L',cssclass="code-highlight")}
 
            %else:
 
              ${h.pygmentize(c.file,linenos=True,anchorlinenos=True,lineanchors='L',cssclass="code-highlight")}
 
            %endif
 
        %else:
 
          <h4>
 
            ${_('File is too big to display.')}
 
            %if c.annotate:
 
              ${h.link_to(_('Show full annotation anyway.'), h.url.current(fulldiff=1, **request.GET.mixed()))}
 
            %else:
 
              ${h.link_to(_('Show as raw.'), h.url('files_raw_home',repo_name=c.repo_name,revision=c.changeset.raw_id,f_path=c.f_path))}
 
            %endif
 
          </h4>
 
        %endif
 
      %endif
 
    </div>
 
</div>
 

	
 
<script>
 
    $(document).ready(function(){
 
        // fake html5 history state
 
        var _State = {
 
           url: ${h.js(h.url.current())},
 
           data: {
 
        var state = {
 
             node_list_url: node_list_url.replace('__REV__',${h.js(c.changeset.raw_id)}).replace('__FPATH__', ${h.js(h.safe_unicode(c.file.path))}),
 
             url_base: url_base.replace('__REV__',${h.js(c.changeset.raw_id)}),
 
             rev:${h.js(c.changeset.raw_id)},
 
             rev: ${h.js(c.changeset.raw_id)},
 
             f_path: ${h.js(h.safe_unicode(c.file.path))}
 
           }
 
        }
 
        callbacks(_State); // defined in files.html, main callbacks. Triggered in pjax calls
 
        post_load_state(State.data); // defined in files.html
 
    });
 
</script>
0 comments (0 inline, 0 general)