<commit_before>/* TQApp Javascript */

TQ = {};

"use strict";

TQ.setAlert = function(msg, where, type) {
    if (!$(where) || !$(where).length) {
        where = '#alerts';
    }
    if (!type) type = 'danger';
    
    var div = '<div class="alert alert-'+type+' alert-dismissable">';
    div += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
    div += msg;
    div += '</div>';
    $(where).html(div);
}

TQ.STATUS = {
    C : 'Complete',
    A : 'In Process'
};

TQ.SPINNER = null;

TQ.getMedia = function(el, user, pg) {
    if (!user) user = TQ.User;
    if (!pg)   pg   = 1;

    // cache the contents of the 'loading' img/message the first time
    if (!TQ.SPINNER) {
        TQ.SPINNER = $('#media .panel-body').html();
    }

    // pull all the media for the user
    var mediaUri =  TQ.APIBase+'/media/search/';
        mediaUri += '?cxc-minimal=1&cxc-order=updated_at+DESC&cxc-page_size=10&cxc-page='+pg;
    $.getJSON(mediaUri, function(resp) {
        //console.log(resp); 
        var media = '<table class="table table-striped">';
            media += '<tr><th>Name</th><th>URI</th><th>Status</th><th>Last Modified</th></tr>';
        $.each(resp.results, function(idx, m) {
            //console.log(m);
            var n    = m.name ? m.name : m.uuid;
            var link = '<a href="#" data-uuid="'+m.uuid+'" onclick="TQ.showMedia($(this))">'+n+'</a>';
            var d    = m.updated_at;
            var stat = TQ.STATUS[m.status];
            var row = '<tr><td class="tq-uuid">'+link+'</td><td class="tq-uri">'+m.uri+'</td><td>'+stat+'</td><td>'+d+'</td></tr>';
            media += row;
        });
        media += '</table>';

        el.html(media);
        TQ.makeMediaPager(el, user, resp);  // call after .html() because it appends

        // reload again in 30 seconds
        TQ.MEDIA_RELOADER = setTimeout( function() { 
            //el.html(TQ.SPINNER);
            TQ.getMedia(el,user);
        }, 30000);
    });

}

TQ.makeMediaPager = function(parentEl, user, resp) {

// http://getbootstrap.com/components/#pagination
    console.log(resp);
    var pgSize = parseInt(resp.query.limit);
    var pager = '<div id="media-pager"></div>';
    parentEl.append(pager);
    $('#media-pager').wPaginate({
        spread: 4,
        total: parseInt(resp.count),
        limit: pgSize,
        index: parseInt(resp.query.offset),
        prev: null,
        next: null,
        //first: null,
        //last: null,
        url: function(idx) {
            var newPg = idx+1;
            clearTimeout(TQ.MEDIA_RELOADER); // reset reloader
            parentEl.html(TQ.SPINNER);
            TQ.getMedia(parentEl, user, newPg);
        },  
        ajax: true
    });
}

TQ.createMedia = function(btn, el, user) {
    if (!user) user = TQ.User;
    var uri = $('[name="uri"]').val();
    var mname = $('[name="name"]').val();
    var mediaUri = TQ.APIBase + '/user/' + user.guid + '/media';
    var payload = { uri : uri, name: mname };
    var alerts = $('#create-media-alerts');

    if (!uri) {
        TQ.setAlert("URI value is required", alerts);
        return;
    }

    // prevent multi-clicks
    btn.attr('disabled', true);
    btn.addClass('disabled');

    $.ajax({
        type: 'POST',
        url: mediaUri,
        data: JSON.stringify(payload),<commit_msg>disable media panel automatic reload. too clever.<commit_after>/* TQApp Javascript */

TQ = {};

"use strict";

TQ.setAlert = function(msg, where, type) {
    if (!$(where) || !$(where).length) {
        where = '#alerts';
    }
    if (!type) type = 'danger';
    
    var div = '<div class="alert alert-'+type+' alert-dismissable">';
    div += '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
    div += msg;
    div += '</div>';
    $(where).html(div);
}

TQ.STATUS = {
    C : 'Complete',
    A : 'In Process'
};

TQ.SPINNER = null;

TQ.getMedia = function(el, user, pg) {
    if (!user) user = TQ.User;
    if (!pg)   pg   = 1;

    // cache the contents of the 'loading' img/message the first time
    if (!TQ.SPINNER) {
        TQ.SPINNER = $('#media .panel-body').html();
    }

    // pull all the media for the user
    var mediaUri =  TQ.APIBase+'/media/search/';
        mediaUri += '?cxc-minimal=1&cxc-order=updated_at+DESC&cxc-page_size=10&cxc-page='+pg;
    $.getJSON(mediaUri, function(resp) {
        //console.log(resp); 
        var media = '<table class="table table-striped">';
            media += '<tr><th>Name</th><th>URI</th><th>Status</th><th>Last Modified</th></tr>';
        $.each(resp.results, function(idx, m) {
            //console.log(m);
            var n    = m.name ? m.name : m.uuid;
            var link = '<a href="#" data-uuid="'+m.uuid+'" onclick="TQ.showMedia($(this))">'+n+'</a>';
            var d    = m.updated_at;
            var stat = TQ.STATUS[m.status];
            var row = '<tr><td class="tq-uuid">'+link+'</td><td class="tq-uri">'+m.uri+'</td><td>'+stat+'</td><td>'+d+'</td></tr>';
            media += row;
        });
        media += '</table>';

        el.html(media);
        TQ.makeMediaPager(el, user, resp);  // call after .html() because it appends

        // reload again in 30 seconds
        /*
        TQ.MEDIA_RELOADER = setTimeout( function() { 
            //el.html(TQ.SPINNER);
            TQ.getMedia(el,user);
        }, 30000);
        */
    });

}

TQ.makeMediaPager = function(parentEl, user, resp) {

// http://getbootstrap.com/components/#pagination
    console.log(resp);
    var pgSize = parseInt(resp.query.limit);
    var pager = '<div id="media-pager"></div>';
    parentEl.append(pager);
    $('#media-pager').wPaginate({
        spread: 4,
        total: parseInt(resp.count),
        limit: pgSize,
        index: parseInt(resp.query.offset),
        prev: null,
        next: null,
        //first: null,
        //last: null,
        url: function(idx) {
            var newPg = idx+1;
            //clearTimeout(TQ.MEDIA_RELOADER); // reset reloader
            parentEl.html(TQ.SPINNER);
            TQ.getMedia(parentEl, user, newPg);
        },  
        ajax: true
    });
}

TQ.createMedia = function(btn, el, user) {
    if (!user) user = TQ.User;
    var uri = $('[name="uri"]').val();
    var mname = $('[name="name"]').val();
    var mediaUri = TQ.APIBase + '/user/' + user.guid + '/media';
    var payload = { uri : uri, name: mname };
    var alerts = $('#create-media-alerts');

    if (!uri) {
        TQ.setAlert("URI value is required", alerts);
        return;
    }

    // prevent multi-clicks
    btn.attr('disabled', true);
    btn.addClass('disabled');

    $.ajax({
        type: 'POST',
        url: mediaUri,
        data: JSON.stringify(payload),
{'fraction_non_alphanumeric': 0.1947399527186761, 'fraction_numerical': 0.0038416075650118202, 'mean_word_length': 2.227944682880305, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7191244', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2322, 'n_words': 483}