Changeset - a991e5c57a8d
[Not reviewed]
Andrew Shadura - 11 years ago 2014-05-24 00:03:16
andrew@shadura.me
rhodecode.js: Get rid of some more YUI
1 file changed with 35 insertions and 40 deletions:
0 comments (0 inline, 0 general)
rhodecode/public/js/rhodecode.js
Show inline comments
 
@@ -271,1065 +271,1060 @@ var pyroutes = (function() {
 
var YUC = YAHOO.util.Connect;
 
var YUD = YAHOO.util.Dom;
 
var YUE = YAHOO.util.Event;
 
var YUQ = YAHOO.util.Selector.query;
 

	
 
var _run_callbacks = function(callbacks){
 
    if (callbacks !== undefined){
 
        var _l = callbacks.length;
 
        for (var i=0;i<_l;i++){
 
            var func = callbacks[i];
 
            if(typeof(func)=='function'){
 
                try{
 
                    func();
 
                }catch (err){};
 
            }
 
        }
 
    }
 
}
 

	
 
/**
 
 * turns objects into GET query string
 
 */
 
var toQueryString = function(o) {
 
    if(typeof o !== 'object') {
 
        return false;
 
    }
 
    var _p, _qs = [];
 
    for(_p in o) {
 
        _qs.push(encodeURIComponent(_p) + '=' + encodeURIComponent(o[_p]));
 
    }
 
    return _qs.join('&');
 
};
 

	
 
/**
 
 * Partial Ajax Implementation
 
 *
 
 * @param url: defines url to make partial request
 
 * @param container: defines id of container to input partial result
 
 * @param s_call: success callback function that takes o as arg
 
 *  o.tId
 
 *  o.status
 
 *  o.statusText
 
 *  o.getResponseHeader[ ]
 
 *  o.getAllResponseHeaders
 
 *  o.responseText
 
 *  o.responseXML
 
 *  o.argument
 
 * @param f_call: failure callback
 
 * @param args arguments
 
 */
 
function ypjax(url,container,s_call,f_call,args){
 
    var method='GET';
 
    if(args===undefined){
 
        args=null;
 
    }
 

	
 
    // Set special header for partial ajax == HTTP_X_PARTIAL_XHR
 
    YUC.initHeader('X-PARTIAL-XHR',true);
 

	
 
    // wrapper of passed callback
 
    var s_wrapper = (function(o){
 
        return function(o){
 
            YUD.get(container).innerHTML=o.responseText;
 
            YUD.setStyle(container,'opacity','1.0');
 
            //execute the given original callback
 
            if (s_call !== undefined){
 
                s_call(o);
 
            }
 
        }
 
    })()
 
    YUD.setStyle(container,'opacity','0.3');
 
    YUC.asyncRequest(method,url,{
 
        success:s_wrapper,
 
        failure:function(o){
 
            console.log(o);
 
            YUD.get(container).innerHTML='<span class="error_red">ERROR: {0}</span>'.format(o.status);
 
            YUD.setStyle(container,'opacity','1.0');
 
        },
 
        cache:false
 
    },args);
 

	
 
};
 

	
 
var ajaxGET = function(url,success) {
 
    // Set special header for ajax == HTTP_X_PARTIAL_XHR
 
    YUC.initHeader('X-PARTIAL-XHR',true);
 

	
 
    var sUrl = url;
 
    var callback = {
 
        success: success,
 
        failure: function (o) {
 
            if (o.status != 0) {
 
                alert("error: " + o.statusText);
 
            };
 
        },
 
    };
 

	
 
    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
 
    return request;
 
};
 

	
 
var ajaxPOST = function(url,postData,success) {
 
    // Set special header for ajax == HTTP_X_PARTIAL_XHR
 
    YUC.initHeader('X-PARTIAL-XHR',true);
 

	
 
    var sUrl = url;
 
    var callback = {
 
        success: success,
 
        failure: function (o) {
 
            alert("error");
 
        },
 
    };
 
    var postData = toQueryString(postData);
 
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
 
    return request;
 
};
 

	
 

	
 
/**
 
 * tooltip activate
 
 */
 
var tooltip_activate = function(){
 
    yt = YAHOO.yuitip.main;
 
    YUE.onDOMReady(yt.init);
 
};
 

	
 
/**
 
 * show more
 
 */
 
var show_more_event = function(){
 
    YUE.on(YUD.getElementsByClassName('show_more'),'click',function(e){
 
        var el = e.target;
 
        YUD.setStyle(YUD.get(el.id.substring(1)),'display','');
 
        YUD.setStyle(el.parentNode,'display','none');
 
    });
 
};
 

	
 
/**
 
 * show changeset tooltip
 
 */
 
var show_changeset_tooltip = function(){
 
    YUE.on(YUD.getElementsByClassName('lazy-cs'), 'mouseover', function(e){
 
        var target = e.currentTarget;
 
        var rid = YUD.getAttribute(target,'raw_id');
 
        var repo_name = YUD.getAttribute(target,'repo_name');
 
        var ttid = 'tt-'+rid;
 
        var success = function(o){
 
            var json = JSON.parse(o.responseText);
 
            YUD.addClass(target,'tooltip')
 
            YUD.setAttribute(target, 'title',json['message']);
 
            YAHOO.yuitip.main.show_yuitip(e, target);
 
        }
 
        if(rid && !YUD.hasClass(target, 'tooltip')){
 
            YUD.setAttribute(target,'id',ttid);
 
            YUD.setAttribute(target, 'title',_TM['loading ...']);
 
            YAHOO.yuitip.main.set_listeners(target);
 
            YAHOO.yuitip.main.show_yuitip(e, target);
 
            var url = pyroutes.url('changeset_info', {"repo_name":repo_name, "revision": rid});
 
            ajaxGET(url, success)
 
        }
 
    });
 
};
 

	
 
var onSuccessFollow = function(target){
 
    var f = YUD.get(target);
 
    var f_cnt = YUD.get('current_followers_count');
 

	
 
    if(YUD.hasClass(f, 'follow')){
 
        f.setAttribute('class','following');
 
        f.setAttribute('title',_TM['Stop following this repository']);
 

	
 
        if(f_cnt){
 
            var cnt = Number(f_cnt.innerHTML)+1;
 
            f_cnt.innerHTML = cnt;
 
        }
 
    }
 
    else{
 
        f.setAttribute('class','follow');
 
        f.setAttribute('title',_TM['Start following this repository']);
 
        if(f_cnt){
 
            var cnt = Number(f_cnt.innerHTML)-1;
 
            f_cnt.innerHTML = cnt;
 
        }
 
    }
 
}
 

	
 
var toggleFollowingRepo = function(target,fallows_repo_id,token,user_id){
 
    args = 'follows_repo_id='+fallows_repo_id;
 
    args+= '&amp;auth_token='+token;
 
    if(user_id != undefined){
 
        args+="&amp;user_id="+user_id;
 
    }
 
    YUC.asyncRequest('POST',TOGGLE_FOLLOW_URL,{
 
        success:function(o){
 
    $.post(TOGGLE_FOLLOW_URL, args, function(data){
 
            onSuccessFollow(target);
 
        }
 
    },args);
 
        });
 
    return false;
 
}
 

	
 
var showRepoSize = function(target, repo_name, token){
 
    var args= 'auth_token='+token;
 

	
 
    if(!YUD.hasClass(target, 'loaded')){
 
        YUD.get(target).innerHTML = _TM['Loading ...'];
 
    if(!$("#" + target).hasClass('loaded')){
 
        $("#" + target).html(_TM['Loading ...']);
 
        var url = pyroutes.url('repo_size', {"repo_name":repo_name});
 
        YUC.asyncRequest('POST',url,{
 
            success:function(o){
 
                YUD.get(target).innerHTML = JSON.parse(o.responseText);
 
                YUD.addClass(target, 'loaded');
 
            }
 
        },args);
 
        $.post(url, args, function(data) {
 
            $("#" + target).html(data);
 
            $("#" + target).addClass('loaded');
 
        });
 
    }
 
    return false;
 
}
 

	
 
/**
 
 * TOOLTIP IMPL.
 
 */
 
YAHOO.namespace('yuitip');
 
YAHOO.yuitip.main = {
 

	
 
    $:          YAHOO.util.Dom.get,
 

	
 
    bgColor:    '#000',
 
    speed:      0.3,
 
    opacity:    0.9,
 
    offset:     [15,15],
 
    useAnim:    false,
 
    maxWidth:   600,
 
    add_links:  false,
 
    yuitips:    [],
 

	
 
    set_listeners: function(tt){
 
        YUE.on(tt, 'mouseover', yt.show_yuitip,  tt);
 
        YUE.on(tt, 'mousemove', yt.move_yuitip,  tt);
 
        YUE.on(tt, 'mouseout',  yt.close_yuitip, tt);
 
    },
 

	
 
    init: function(){
 
        yt.tipBox = yt.$('tip-box');
 
        if(!yt.tipBox){
 
            yt.tipBox = document.createElement('div');
 
            document.body.appendChild(yt.tipBox);
 
            yt.tipBox.id = 'tip-box';
 
        }
 

	
 
        YUD.setStyle(yt.tipBox, 'display', 'none');
 
        YUD.setStyle(yt.tipBox, 'position', 'absolute');
 
        if(yt.maxWidth !== null){
 
            YUD.setStyle(yt.tipBox, 'max-width', yt.maxWidth+'px');
 
        }
 

	
 
        var yuitips = YUD.getElementsByClassName('tooltip');
 

	
 
        if(yt.add_links === true){
 
            var links = document.getElementsByTagName('a');
 
            var linkLen = links.length;
 
            for(i=0;i<linkLen;i++){
 
                yuitips.push(links[i]);
 
            }
 
        }
 

	
 
        var yuiLen = yuitips.length;
 

	
 
        for(i=0;i<yuiLen;i++){
 
            yt.set_listeners(yuitips[i]);
 
        }
 
    },
 

	
 
    show_yuitip: function(e, el){
 
        YUE.stopEvent(e);
 
        if(el.tagName.toLowerCase() === 'img'){
 
            yt.tipText = el.alt ? el.alt : '';
 
        } else {
 
            yt.tipText = el.title ? el.title : '';
 
        }
 

	
 
        if(yt.tipText !== ''){
 
            // save org title
 
            YUD.setAttribute(el, 'tt_title', yt.tipText);
 
            // reset title to not show org tooltips
 
            YUD.setAttribute(el, 'title', '');
 

	
 
            yt.tipBox.innerHTML = yt.tipText;
 
            YUD.setStyle(yt.tipBox, 'display', 'block');
 
            if(yt.useAnim === true){
 
                YUD.setStyle(yt.tipBox, 'opacity', '0');
 
                var newAnim = new YAHOO.util.Anim(yt.tipBox,
 
                    {
 
                        opacity: { to: yt.opacity }
 
                    }, yt.speed, YAHOO.util.Easing.easeOut
 
                );
 
                newAnim.animate();
 
            }
 
        }
 
    },
 

	
 
    move_yuitip: function(e, el){
 
        YUE.stopEvent(e);
 
        var movePos = YUE.getXY(e);
 
        YUD.setStyle(yt.tipBox, 'top', (movePos[1] + yt.offset[1]) + 'px');
 
        YUD.setStyle(yt.tipBox, 'left', (movePos[0] + yt.offset[0]) + 'px');
 
    },
 

	
 
    close_yuitip: function(e, el){
 
        YUE.stopEvent(e);
 

	
 
        if(yt.useAnim === true){
 
            var newAnim = new YAHOO.util.Anim(yt.tipBox,
 
                {
 
                    opacity: { to: 0 }
 
                }, yt.speed, YAHOO.util.Easing.easeOut
 
            );
 
            newAnim.animate();
 
        } else {
 
            YUD.setStyle(yt.tipBox, 'display', 'none');
 
        }
 
        YUD.setAttribute(el,'title', YUD.getAttribute(el, 'tt_title'));
 
    }
 
}
 

	
 
/**
 
 * Quick filter widget
 
 *
 
 * @param target: filter input target
 
 * @param nodes: list of nodes in html we want to filter.
 
 * @param display_element function that takes current node from nodes and
 
 *    does hide or show based on the node
 
 *
 
 */
 
var q_filter = function(target,nodes,display_element){
 

	
 
    var nodes = nodes;
 
    var q_filter_field = YUD.get(target);
 
    var F = YAHOO.namespace(target);
 

	
 
    YUE.on(q_filter_field,'keyup',function(e){
 
        clearTimeout(F.filterTimeout);
 
        F.filterTimeout = setTimeout(F.updateFilter,600);
 
    });
 

	
 
    F.filterTimeout = null;
 

	
 
    var show_node = function(node){
 
        YUD.setStyle(node,'display','')
 
    }
 
    var hide_node = function(node){
 
        YUD.setStyle(node,'display','none');
 
    }
 

	
 
    F.updateFilter  = function() {
 
        // Reset timeout
 
        F.filterTimeout = null;
 

	
 
        var obsolete = [];
 

	
 
        var req = q_filter_field.value.toLowerCase();
 

	
 
        var l = nodes.length;
 
        var i;
 
        var showing = 0;
 

	
 
        for (i=0;i<l;i++ ){
 
            var n = nodes[i];
 
            var target_element = display_element(n)
 
            if(req && n.innerHTML.toLowerCase().indexOf(req) == -1){
 
                hide_node(target_element);
 
            }
 
            else{
 
                show_node(target_element);
 
                showing+=1;
 
            }
 
        }
 

	
 
        // if repo_count is set update the number
 
        var cnt = YUD.get('repo_count');
 
        if(cnt){
 
            YUD.get('repo_count').innerHTML = showing;
 
        }
 
    }
 
};
 

	
 
var tableTr = function(cls, body){
 
    var _el = document.createElement('div');
 
    var cont = new YAHOO.util.Element(body);
 
    var comment_id = fromHTML(body).children[0].id.split('comment-')[1];
 
    var id = 'comment-tr-{0}'.format(comment_id);
 
    var _html = ('<table><tbody><tr id="{0}" class="{1}">'+
 
                  '<td class="lineno-inline new-inline"></td>'+
 
                  '<td class="lineno-inline old-inline"></td>'+
 
                  '<td>{2}</td>'+
 
                 '</tr></tbody></table>').format(id, cls, body);
 
    _el.innerHTML = _html;
 
    return _el.children[0].children[0].children[0];
 
};
 

	
 
var createInlineForm = function(parent_tr, f_path, line) {
 
    var tmpl = YUD.get('comment-inline-form-template').innerHTML;
 
    var tmpl = $('#comment-inline-form-template').html();
 
    tmpl = tmpl.format(f_path, line);
 
    var form = tableTr('comment-form-inline',tmpl)
 

	
 
    // create event for hide button
 
    form = new YAHOO.util.Element(form);
 
    var form_hide_button = new YAHOO.util.Element(YUD.getElementsByClassName('hide-inline-form',null,form)[0]);
 
    form_hide_button.on('click', function(e) {
 
        var newtr = e.currentTarget.parentNode.parentNode.parentNode.parentNode.parentNode;
 
        if(YUD.hasClass(newtr.nextElementSibling,'inline-comments-button')){
 
            YUD.setStyle(newtr.nextElementSibling,'display','');
 
        }
 
        removeInlineForm(newtr);
 
        YUD.removeClass(parent_tr, 'form-open');
 
        YUD.removeClass(parent_tr, 'hl-comment');
 

	
 
    });
 

	
 
    return form
 
};
 

	
 
/**
 
 * Inject inline comment for on given TR this tr should be always an .line
 
 * tr containing the line. Code will detect comment, and always put the comment
 
 * block at the very bottom
 
 */
 
var injectInlineForm = function(tr){
 
    if(!YUD.hasClass(tr, 'line')){
 
    if(!$(tr).hasClass('line')){
 
        return
 
    }
 
    var submit_url = AJAX_COMMENT_URL;
 
    var _td = YUD.getElementsByClassName('code',null,tr)[0];
 
    if(YUD.hasClass(tr,'form-open') || YUD.hasClass(tr,'context') || YUD.hasClass(_td,'no-comment')){
 
    if($(tr).hasClass('form-open') || $(tr).hasClass('context') || $(_td).hasClass('no-comment')){
 
        return
 
    }
 
    YUD.addClass(tr,'form-open');
 
    YUD.addClass(tr,'hl-comment');
 
    $(tr).addClass('form-open hl-comment');
 
    var node = YUD.getElementsByClassName('full_f_path',null,tr.parentNode.parentNode.parentNode)[0];
 
    var f_path = YUD.getAttribute(node,'path');
 
    var lineno = getLineNo(tr);
 
    var form = createInlineForm(tr, f_path, lineno, submit_url);
 

	
 
    var parent = tr;
 
    while (1){
 
        var n = parent.nextElementSibling;
 
        // next element are comments !
 
        if(YUD.hasClass(n,'inline-comments')){
 
        if($(n).hasClass('inline-comments')){
 
            parent = n;
 
        }
 
        else{
 
            break;
 
        }
 
    }
 
    YUD.insertAfter(form,parent);
 
    var f = YUD.get(form);
 
    var overlay = YUD.getElementsByClassName('overlay',null,f)[0];
 
    var _form = YUD.getElementsByClassName('inline-form',null,f)[0];
 

	
 
    YUE.on(YUD.get(_form), 'submit',function(e){
 
        YUE.preventDefault(e);
 

	
 
        //ajax submit
 
        var text = YUD.get('text_'+lineno).value;
 
        var postData = {
 
                'text':text,
 
                'f_path':f_path,
 
                'line':lineno
 
        };
 

	
 
        if(lineno === undefined){
 
            alert('missing line !');
 
            return
 
        }
 
        if(f_path === undefined){
 
            alert('missing file path !');
 
            return
 
        }
 

	
 
        if(text == ""){
 
            return
 
        }
 

	
 
        var success = function(o){
 
            YUD.removeClass(tr, 'form-open');
 
            removeInlineForm(f);
 
            var json_data = JSON.parse(o.responseText);
 
            renderInlineComment(json_data);
 
        };
 

	
 
        if (YUD.hasClass(overlay,'overlay')){
 
            var w = _form.offsetWidth;
 
            var h = _form.offsetHeight;
 
            YUD.setStyle(overlay,'width',w+'px');
 
            YUD.setStyle(overlay,'height',h+'px');
 
        }
 
        YUD.addClass(overlay, 'submitting');
 

	
 
        ajaxPOST(submit_url, postData, success);
 
    });
 

	
 
    YUE.on('preview-btn_'+lineno, 'click', function(e){
 
        var _text = YUD.get('text_'+lineno).value;
 
        if(!_text){
 
            return
 
        }
 
        var post_data = {'text': _text};
 
        YUD.addClass('preview-box_'+lineno, 'unloaded');
 
        YUD.get('preview-box_'+lineno).innerHTML = _TM['Loading ...'];
 
        YUD.setStyle('edit-container_'+lineno, 'display', 'none');
 
        YUD.setStyle('preview-container_'+lineno, 'display', '');
 

	
 
        var url = pyroutes.url('changeset_comment_preview', {'repo_name': REPO_NAME});
 
        ajaxPOST(url,post_data,function(o){
 
            YUD.get('preview-box_'+lineno).innerHTML = o.responseText;
 
            YUD.removeClass('preview-box_'+lineno, 'unloaded');
 
        })
 
    })
 
    YUE.on('edit-btn_'+lineno, 'click', function(e){
 
        YUD.setStyle('edit-container_'+lineno, 'display', '');
 
        YUD.setStyle('preview-container_'+lineno, 'display', 'none');
 
    })
 

	
 
    setTimeout(function(){
 
        // callbacks
 
        tooltip_activate();
 
        MentionsAutoComplete('text_'+lineno, 'mentions_container_'+lineno,
 
                             _USERS_AC_DATA, _GROUPS_AC_DATA);
 
        var _e = YUD.get('text_'+lineno);
 
        if(_e){
 
            _e.focus();
 
        }
 
    },10)
 
};
 

	
 
var deleteComment = function(comment_id){
 
    var url = AJAX_COMMENT_DELETE_URL.replace('__COMMENT_ID__',comment_id);
 
    var postData = {'_method':'delete'};
 
    var success = function(o){
 
        var n = YUD.get('comment-tr-'+comment_id);
 
        var root = prevElementSibling(prevElementSibling(n));
 
        n.parentNode.removeChild(n);
 

	
 
        // scann nodes, and attach add button to last one only for TR
 
        // which are the inline comments
 
        if(root && root.tagName == 'TR'){
 
            placeAddButton(root);
 
        }
 
    }
 
    ajaxPOST(url,postData,success);
 
}
 

	
 
var createInlineAddButton = function(tr){
 

	
 
    var label = TRANSLATION_MAP['Add another comment'];
 

	
 
    var html_el = document.createElement('div');
 
    YUD.addClass(html_el, 'add-comment');
 
    html_el.innerHTML = '<span class="ui-btn">{0}</span>'.format(label);
 

	
 
    var add = new YAHOO.util.Element(html_el);
 
    add.on('click', function(e) {
 
        injectInlineForm(tr);
 
    });
 
    return add;
 
};
 

	
 
var getLineNo = function(tr) {
 
    var line;
 
    var o = tr.children[0].id.split('_');
 
    var n = tr.children[1].id.split('_');
 

	
 
    if (n.length >= 2) {
 
        line = n[n.length-1];
 
    } else if (o.length >= 2) {
 
        line = o[o.length-1];
 
    }
 

	
 
    return line
 
};
 

	
 
var placeAddButton = function(target_tr){
 
    if(!target_tr){
 
        return
 
    }
 
    var last_node = target_tr;
 
    //scann
 
    while (1){
 
        var n = last_node.nextElementSibling;
 
        // next element are comments !
 
        if(YUD.hasClass(n,'inline-comments')){
 
        if($(n).hasClass('inline-comments')){
 
            last_node = n;
 
            //also remove the comment button from previous
 
            var comment_add_buttons = YUD.getElementsByClassName('add-comment',null,last_node);
 
            for(var i=0;i<comment_add_buttons.length;i++){
 
                var b = comment_add_buttons[i];
 
                b.parentNode.removeChild(b);
 
            }
 
        }
 
        else{
 
            break;
 
        }
 
    }
 

	
 
    var add = createInlineAddButton(target_tr);
 
    // get the comment div
 
    var comment_block = YUD.getElementsByClassName('comment',null,last_node)[0];
 
    // attach add button
 
    YUD.insertAfter(add,comment_block);
 
}
 

	
 
/**
 
 * Places the inline comment into the changeset block in proper line position
 
 */
 
var placeInline = function(target_container,lineno,html){
 
    var lineid = "{0}_{1}".format(target_container,lineno);
 
    var target_line = YUD.get(lineid);
 
    var comment = new YAHOO.util.Element(tableTr('inline-comments',html))
 

	
 
    // check if there are comments already !
 
    var parent = target_line.parentNode;
 
    var root_parent = parent;
 
    while (1){
 
        var n = parent.nextElementSibling;
 
        // next element are comments !
 
        if(YUD.hasClass(n,'inline-comments')){
 
        if($(n).hasClass('inline-comments')){
 
            parent = n;
 
        }
 
        else{
 
            break;
 
        }
 
    }
 
    // put in the comment at the bottom
 
    YUD.insertAfter(comment,parent);
 

	
 
    // scann nodes, and attach add button to last one
 
    placeAddButton(root_parent);
 

	
 
    return target_line;
 
}
 

	
 
/**
 
 * make a single inline comment and place it inside
 
 */
 
var renderInlineComment = function(json_data){
 
    try{
 
        var html =  json_data['rendered_text'];
 
        var lineno = json_data['line_no'];
 
        var target_id = json_data['target_id'];
 
        placeInline(target_id, lineno, html);
 
    }catch(e){
 
        console.log(e);
 
    }
 
}
 

	
 
/**
 
 * Iterates over all the inlines, and places them inside proper blocks of data
 
 */
 
var renderInlineComments = function(file_comments){
 
    for (f in file_comments){
 
        // holding all comments for a FILE
 
        var box = file_comments[f];
 

	
 
        var target_id = YUD.getAttribute(box,'target_id');
 
        // actually comments with line numbers
 
        var comments = box.children;
 
        for(var i=0; i<comments.length; i++){
 
            var data = {
 
                'rendered_text': comments[i].outerHTML,
 
                'line_no': YUD.getAttribute(comments[i],'line'),
 
                'target_id': target_id
 
            }
 
            renderInlineComment(data);
 
        }
 
    }
 
}
 

	
 
var fileBrowserListeners = function(current_url, node_list_url, url_base){
 
    var current_url_branch = +"?branch=__BRANCH__";
 

	
 
    YUE.on('stay_at_branch','click',function(e){
 
    $('#stay_at_branch').on('click',function(e){
 
        if(e.target.checked){
 
            var uri = current_url_branch;
 
            uri = uri.replace('__BRANCH__',e.target.value);
 
            window.location = uri;
 
        }
 
        else{
 
            window.location = current_url;
 
        }
 
    })
 

	
 
    var n_filter = YUD.get('node_filter');
 
    var F = YAHOO.namespace('node_filter');
 

	
 
    F.filterTimeout = null;
 
    var nodes = null;
 

	
 
    F.initFilter = function(){
 
        YUD.setStyle('node_filter_box_loading','display','');
 
        YUD.setStyle('search_activate_id','display','none');
 
        YUD.setStyle('add_node_id','display','none');
 
        $('#node_filter_box_loading').css('display','');
 
        $('#search_activate_id').css('display','none');
 
        $('#add_node_id').css('display','none');
 
        YUC.initHeader('X-PARTIAL-XHR',true);
 
        YUC.asyncRequest('GET', node_list_url, {
 
            success:function(o){
 
                nodes = JSON.parse(o.responseText).nodes;
 
                YUD.setStyle('node_filter_box_loading','display','none');
 
                YUD.setStyle('node_filter_box','display','');
 
                $('#node_filter_box_loading').css('display','none');
 
                $('#node_filter_box').css('display','');
 
                n_filter.focus();
 
                if(YUD.hasClass(n_filter,'init')){
 
                    n_filter.value = '';
 
                    YUD.removeClass(n_filter,'init');
 
                }
 
            },
 
            failure:function(o){
 
                console.log('failed to load');
 
            }
 
        },null);
 
    }
 

	
 
    F.updateFilter = function(e) {
 
        return function(){
 
            // Reset timeout
 
            F.filterTimeout = null;
 
            var query = e.target.value.toLowerCase();
 
            var match = [];
 
            var matches = 0;
 
            var matches_max = 20;
 
            if (query != ""){
 
                for(var i=0;i<nodes.length;i++){
 
                    var pos = nodes[i].name.toLowerCase().indexOf(query)
 
                    if(query && pos != -1){
 
                        matches++
 
                        //show only certain amount to not kill browser
 
                        if (matches > matches_max){
 
                            break;
 
                        }
 

	
 
                        var n = nodes[i].name;
 
                        var t = nodes[i].type;
 
                        var n_hl = n.substring(0,pos)
 
                          +"<b>{0}</b>".format(n.substring(pos,pos+query.length))
 
                          +n.substring(pos+query.length)
 
                        var new_url = url_base.replace('__FPATH__',n);
 
                        match.push('<tr><td><a class="browser-{0}" href="{1}">{2}</a></td><td colspan="5"></td></tr>'.format(t,new_url,n_hl));
 
                    }
 
                    if(match.length >= matches_max){
 
                        match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_TM['Search truncated']));
 
                    }
 
                }
 
            }
 
            if(query != ""){
 
                YUD.setStyle('tbody','display','none');
 
                YUD.setStyle('tbody_filtered','display','');
 
                $('#tbody').css('display','none');
 
                $('#tbody_filtered').css('display','');
 

	
 
                if (match.length==0){
 
                  match.push('<tr><td>{0}</td><td colspan="5"></td></tr>'.format(_TM['No matching files']));
 
                }
 

	
 
                YUD.get('tbody_filtered').innerHTML = match.join("");
 
            }
 
            else{
 
                YUD.setStyle('tbody','display','');
 
                YUD.setStyle('tbody_filtered','display','none');
 
                $('#tbody').css('display','');
 
                $('#tbody_filtered').css('display','none');
 
            }
 
        }
 
    };
 

	
 
    YUE.on(YUD.get('filter_activate'),'click',function(){
 
            F.initFilter();
 
        })
 
    YUE.on(n_filter,'click',function(){
 
            if(YUD.hasClass(n_filter,'init')){
 
                n_filter.value = '';
 
                YUD.removeClass(n_filter,'init');
 
            }
 
        });
 
    YUE.on(n_filter,'keyup',function(e){
 
            clearTimeout(F.filterTimeout);
 
            F.filterTimeout = setTimeout(F.updateFilter(e),600);
 
        });
 
};
 

	
 

	
 
var initCodeMirror = function(textAreadId,resetUrl){
 
    var myCodeMirror = CodeMirror.fromTextArea(YUD.get(textAreadId),{
 
            mode:  "null",
 
            lineNumbers:true,
 
            indentUnit: 4
 
        });
 
    YUE.on('reset','click',function(e){
 
            window.location=resetUrl
 
        });
 

	
 
    YUE.on('file_enable','click',function(){
 
            YUD.setStyle('editor_container','display','');
 
            YUD.setStyle('upload_file_container','display','none');
 
            YUD.setStyle('filename_container','display','');
 
            $('#editor_container').css('display','');
 
            $('#upload_file_container').css('display','none');
 
            $('#filename_container').css('display','');
 
        });
 

	
 
    YUE.on('upload_file_enable','click',function(){
 
            YUD.setStyle('editor_container','display','none');
 
            YUD.setStyle('upload_file_container','display','');
 
            YUD.setStyle('filename_container','display','none');
 
            $('#editor_container').css('display','none');
 
            $('#upload_file_container').css('display','');
 
            $('#filename_container').css('display','none');
 
        });
 

	
 
    return myCodeMirror
 
};
 

	
 
var setCodeMirrorMode = function(codeMirrorInstance, mode) {
 
    codeMirrorInstance.setOption("mode", mode);
 
    CodeMirror.autoLoadMode(codeMirrorInstance, mode);
 
}
 

	
 

	
 
var getIdentNode = function(n){
 
    //iterate thru nodes untill matched interesting node !
 

	
 
    if (typeof n == 'undefined'){
 
        return -1
 
    }
 

	
 
    if(typeof n.id != "undefined" && n.id.match('L[0-9]+')){
 
        return n
 
    }
 
    else{
 
        return getIdentNode(n.parentNode);
 
    }
 
};
 

	
 
var getSelectionLink = function(e) {
 
    //get selection from start/to nodes
 
    if (typeof window.getSelection != "undefined") {
 
        s = window.getSelection();
 

	
 
        from = getIdentNode(s.anchorNode);
 
        till = getIdentNode(s.focusNode);
 

	
 
        f_int = parseInt(from.id.replace('L',''));
 
        t_int = parseInt(till.id.replace('L',''));
 

	
 
        if (f_int > t_int){
 
            //highlight from bottom
 
            offset = -35;
 
            ranges = [t_int,f_int];
 

	
 
        }
 
        else{
 
            //highligth from top
 
            offset = 35;
 
            ranges = [f_int,t_int];
 
        }
 
        // if we select more than 2 lines
 
        if (ranges[0] != ranges[1]){
 
            if(YUD.get('linktt') == null){
 
                hl_div = document.createElement('div');
 
                hl_div.id = 'linktt';
 
            }
 
            hl_div.innerHTML = '';
 

	
 
            anchor = '#L'+ranges[0]+'-'+ranges[1];
 
            var link = document.createElement('a');
 
            link.href = location.href.substring(0,location.href.indexOf('#'))+anchor;
 
            link.innerHTML = _TM['Selection link'];
 
            hl_div.appendChild(link);
 
            YUD.get('body').appendChild(hl_div);
 

	
 
            xy = YUD.getXY(till.id);
 

	
 
            YUD.addClass('linktt', 'hl-tip-box');
 
            YUD.setStyle('linktt','top',xy[1]+offset+'px');
 
            YUD.setStyle('linktt','left',xy[0]+'px');
 
            YUD.setStyle('linktt','visibility','visible');
 
            $('#linktt').css('top',xy[1]+offset+'px');
 
            $('#linktt').css('left',xy[0]+'px');
 
            $('#linktt').css('visibility','visible');
 

	
 
        }
 
        else{
 
            YUD.setStyle('linktt','visibility','hidden');
 
            $('#linktt').css('visibility','hidden');
 
        }
 
    }
 
};
 

	
 
var deleteNotification = function(url, notification_id,callbacks){
 
    var callback = {
 
        success:function(o){
 
            var obj = YUD.get(String("notification_"+notification_id));
 
            if(obj.parentNode !== undefined){
 
                obj.parentNode.removeChild(obj);
 
            }
 
            _run_callbacks(callbacks);
 
        },
 
        failure:function(o){
 
            alert("error");
 
        },
 
    };
 
    var postData = '_method=delete';
 
    var sUrl = url.replace('__NOTIFICATION_ID__',notification_id);
 
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,
 
                                                  callback, postData);
 
};
 

	
 
var readNotification = function(url, notification_id,callbacks){
 
    var callback = {
 
        success:function(o){
 
            var obj = YUD.get(String("notification_"+notification_id));
 
            YUD.removeClass(obj, 'unread');
 
            var r_button = YUD.getElementsByClassName('read-notification',null,obj.children[0])[0];
 

	
 
            if(r_button.parentNode !== undefined){
 
                r_button.parentNode.removeChild(r_button);
 
            }
 
            _run_callbacks(callbacks);
 
        },
 
        failure:function(o){
 
            alert("error");
 
        },
 
    };
 
    var postData = '_method=put';
 
    var sUrl = url.replace('__NOTIFICATION_ID__',notification_id);
 
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,
 
                                                  callback, postData);
 
};
 

	
 
/** MEMBERS AUTOCOMPLETE WIDGET **/
 

	
 
var MembersAutoComplete = function (divid, cont, users_list, groups_list) {
 
    var myUsers = users_list;
 
    var myGroups = groups_list;
 

	
 
    // Define a custom search function for the DataSource of users
 
    var matchUsers = function (sQuery) {
 
            // Case insensitive matching
 
            var query = sQuery.toLowerCase();
 
            var i = 0;
 
            var l = myUsers.length;
 
            var matches = [];
 

	
 
            // Match against each name of each contact
 
            for (; i < l; i++) {
 
                contact = myUsers[i];
 
                if (((contact.fname+"").toLowerCase().indexOf(query) > -1) ||
 
                     ((contact.lname+"").toLowerCase().indexOf(query) > -1) ||
 
                     ((contact.nname) && ((contact.nname).toLowerCase().indexOf(query) > -1))) {
 
                    matches[matches.length] = contact;
 
                }
 
            }
 
            return matches;
 
        };
 

	
 
    // Define a custom search function for the DataSource of userGroups
 
    var matchGroups = function (sQuery) {
 
            // Case insensitive matching
 
            var query = sQuery.toLowerCase();
 
            var i = 0;
 
            var l = myGroups.length;
 
            var matches = [];
 

	
 
            // Match against each name of each contact
 
            for (; i < l; i++) {
 
                matched_group = myGroups[i];
 
                if (matched_group.grname.toLowerCase().indexOf(query) > -1) {
 
                    matches[matches.length] = matched_group;
 
                }
 
            }
 
            return matches;
 
        };
 

	
 
    //match all
 
    var matchAll = function (sQuery) {
 
            u = matchUsers(sQuery);
 
            g = matchGroups(sQuery);
 
            return u.concat(g);
 
        };
 

	
 
    // DataScheme for members
 
    var memberDS = new YAHOO.util.FunctionDataSource(matchAll);
 
    memberDS.responseSchema = {
 
        fields: ["id", "fname", "lname", "nname", "grname", "grmembers", "gravatar_lnk"]
 
    };
 

	
 
    // DataScheme for owner
 
    var ownerDS = new YAHOO.util.FunctionDataSource(matchUsers);
 
    ownerDS.responseSchema = {
 
        fields: ["id", "fname", "lname", "nname", "gravatar_lnk"]
 
    };
 

	
 
    // Instantiate AutoComplete for perms
 
    var membersAC = new YAHOO.widget.AutoComplete(divid, cont, memberDS);
 
    membersAC.useShadow = false;
 
    membersAC.resultTypeList = false;
 
    membersAC.animVert = false;
 
    membersAC.animHoriz = false;
 
    membersAC.animSpeed = 0.1;
 

	
 
    // Instantiate AutoComplete for owner
 
    var ownerAC = new YAHOO.widget.AutoComplete("user", "owner_container", ownerDS);
 
    ownerAC.useShadow = false;
 
    ownerAC.resultTypeList = false;
 
    ownerAC.animVert = false;
 
    ownerAC.animHoriz = false;
 
    ownerAC.animSpeed = 0.1;
 

	
 
    // Helper highlight function for the formatter
 
    var highlightMatch = function (full, snippet, matchindex) {
 
            return full.substring(0, matchindex)
 
            + "<span class='match'>"
 
            + full.substr(matchindex, snippet.length)
 
            + "</span>" + full.substring(matchindex + snippet.length);
 
        };
 

	
 
    // Custom formatter to highlight the matching letters
 
    var custom_formatter = function (oResultData, sQuery, sResultMatch) {
 
            var query = sQuery.toLowerCase();
 
            var _gravatar = function(res, em, group){
 
                if (group !== undefined){
 
                    em = '/images/icons/group.png'
 
                }
 
                tmpl = '<div class="ac-container-wrap"><img class="perm-gravatar-ac" src="{0}"/>{1}</div>'
 
                return tmpl.format(em,res)
 
            }
 
            // group
 
            if (oResultData.grname != undefined) {
 
                var grname = oResultData.grname;
 
                var grmembers = oResultData.grmembers;
 
                var grnameMatchIndex = grname.toLowerCase().indexOf(query);
 
                var grprefix = "{0}: ".format(_TM['Group']);
 
                var grsuffix = " (" + grmembers + "  )";
 
                var grsuffix = " ({0}  {1})".format(grmembers, _TM['members']);
 

	
 
                if (grnameMatchIndex > -1) {
 
                    return _gravatar(grprefix + highlightMatch(grname, query, grnameMatchIndex) + grsuffix,null,true);
 
                }
 
                return _gravatar(grprefix + oResultData.grname + grsuffix, null,true);
 
            // Users
 
            } else if (oResultData.nname != undefined) {
 
                var fname = oResultData.fname || "";
 
                var lname = oResultData.lname || "";
 
                var nname = oResultData.nname;
 

	
 
                // Guard against null value
 
                var fnameMatchIndex = fname.toLowerCase().indexOf(query),
 
                    lnameMatchIndex = lname.toLowerCase().indexOf(query),
 
                    nnameMatchIndex = nname.toLowerCase().indexOf(query),
 
                    displayfname, displaylname, displaynname;
 

	
 
                if (fnameMatchIndex > -1) {
 
                    displayfname = highlightMatch(fname, query, fnameMatchIndex);
 
                } else {
 
                    displayfname = fname;
 
                }
 

	
 
                if (lnameMatchIndex > -1) {
 
                    displaylname = highlightMatch(lname, query, lnameMatchIndex);
 
                } else {
 
                    displaylname = lname;
 
                }
 

	
 
                if (nnameMatchIndex > -1) {
 
                    displaynname = "(" + highlightMatch(nname, query, nnameMatchIndex) + ")";
 
                } else {
 
                    displaynname = nname ? "(" + nname + ")" : "";
 
                }
 

	
 
                return _gravatar(displayfname + " " + displaylname + " " + displaynname, oResultData.gravatar_lnk);
 
            } else {
 
                return '';
 
            }
 
        };
 
    membersAC.formatResult = custom_formatter;
 
    ownerAC.formatResult = custom_formatter;
0 comments (0 inline, 0 general)