<commit_before>/**
 * UI and function to quick search projects
 */

var $ = require('jquery');
var m = require('mithril');
var $osf = require('js/osfHelpers');

// CSS
require('css/quick-project-search-plugin.css');

// XHR config for apiserver connection
var xhrconfig = function(xhr) {
    xhr.withCredentials = true;
};

var quickSearchProject = {
    controller: function() {
        var self = this;
        self.nodes = [];
        self.displayedNodes = [];
        //self.lastLogin = '';
        // NEED TO FIGURE OUT WHAT TO DO ABOUT LASTLOGIN.
        self.lastLogin = '2016-01-01T15:20:11.531000';
        self.commentsCount = {};
        self.logsCount = {};
        self.sortState = m.prop();
        self.countState = m.prop();

        // Load node list
        var url = $osf.apiV2Url('users/me/nodes/', { query : { 'embed': 'contributors', 'page[size]': 100}});
        var promise = m.request({method: 'GET', url : url, config : xhrconfig});
        promise.then(function(result){
            result.data.forEach(function(node){
                self.nodes.push(node);
            });
            self.sortDateDescending();
            self.countState = 10;
            self.displayedNodes = self.nodes.splice(0, 10)
        });

        // Load last login
        self.getLastLoginDate = function () {
            var url = $osf.apiV2Url('users/me/', {});
            var promise = m.request({method: 'GET', url: url, config : xhrconfig});
            promise.then(function(result) {
                self.lastLogin = result.data.attributes.last_login
            }
            );
            return promise
        };

        self.getFamilyName = function(i, node) {
            return node.embeds.contributors.data[i].embeds.users.data.attributes.family_name
        };
        self.getContributors = function (node) {
            var numContributors = node.embeds.contributors.links.meta.total;
            if (numContributors === 1) {
                return self.getFamilyName(0, node)
            }
            else if (numContributors == 2) {
                return self.getFamilyName(0, node) + ' and ' +
                        self.getFamilyName(1, node)
            }
            else {
                return self.getFamilyName(0, node) + ', ' +
                        self.getFamilyName(1, node) + ', ' +
                        self.getFamilyName(2, node) + ' + ' + (numContributors - 3)
            }

        };

        self.getRecentComments = function (node) {
            var url = $osf.apiV2Url('nodes/' + node.id + '/comments/',
                { query : { 'filter[date_modified][gte]': self.lastLogin}}
            );
            var promise = m.request({method: 'GET', url : url, config: xhrconfig});
            promise.then(function(result) {
                self.commentsCount[node.id] = result.links.meta.total
            });
            return promise
        };

        self.loadRecentCommentCount = function (node) {
            if (node.id in self.commentsCount) {
                return self.commentsCount[node.id]
            }
            else {
                self.getRecentComments(node);
                return self.commentsCount[node.id]
            }
        };

        self.getRecentLogs = function (node) {
            var url = $osf.apiV2Url('nodes/' + node.id + '/logs/', { query : {'filter[action][ne]': 'comment_added',
                'filter[date][gte]': self.lastLogin}});
            var promise = m.request({method: 'GET', url : url, config: xhrconfig});
            promise.then(function(result){
                self.logsCount[node.id] = result.links.meta.total
            });
            return promise
        };

        self.loadRecentLogsCount = function(node) {
            if (node.id in self.logsCount){
                return self.logsCount[node.id]
            }
            else {
                self.getRecentLogs(node);
                return self.logsCount[node.id]
          }
        };

        self.loadUpToTen = function () {
            requested = self.nodes.splice(0, 10);
            for (i = 0; i < requested.length; i++) {
                self.displayedNodes.push(requested[i])
            }
            self.countState = self.displayedNodes.length;
            return self.displayedNodes
        };

        self.formatDate = function (node) {
            return new $osf.FormattableDate(node.attributes.date_modified).local
        };

        self.restoreToNodeList = function (missingNodes) {
            for (i = 0; i < missingNodes.length ; i++) {
                self.nodes.push(missingNodes[i])
            }
        };

        self.sortAlphabeticalAscending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.title.toUpperCase();
                var B = b.attributes.title.toUpperCase();
                return (A < B) ? -1 : (A > B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'alphaAsc';
            return self.displayedNodes
        };

        self.sortAlphabeticalDescending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.title.toUpperCase();
                var B = b.attributes.title.toUpperCase();
                return (A > B) ? -1 : (A < B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'alphaDesc';
            return self.displayedNodes

        };

        self.sortDateDescending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.date_modified;
                var B = b.attributes.date_modified;
                return (A > B) ? -1 : (A < B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'dateDesc';
            return self.displayedNodes
        };

        self.sortDateAscending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.date_modified;
                var B = b.attributes.date_modified;
                return (A < B) ? -1 : (A > B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'dateAsc';
            return self.displayedNodes
        };

        self.quickSearch = function () {
            var query = document.getElementById('searchQuery').value;
            self.restoreToNodeList(self.nonMatchingNodes);
            self.restoreToNodeList(self.displayedNodes);
            self.displayedNodes = [];
            self.nonMatchingNodes = [];
            // if backspace completely, previous nodes with prior sorting/count will be displayed
            if (query === '') {
                self.sortByPreviousField();
                return self.displayedNodes

            }
            else {
                for (var n = self.nodes.length - 1; n >= 0; n--) {
                    if (self.nodes[n].attributes.title.toUpperCase().indexOf(query.toUpperCase()) === -1) {
                        self.nonMatchingNodes.push(self.nodes[n]);
                        self.nodes.splice(n, 1)
                    }
                }<commit_msg>If search box is cleared, restore nodes in view to what was visible prior to search.  Add method to sort nodes  by previous field.<commit_after>/**
 * UI and function to quick search projects
 */

var $ = require('jquery');
var m = require('mithril');
var $osf = require('js/osfHelpers');

// CSS
require('css/quick-project-search-plugin.css');

// XHR config for apiserver connection
var xhrconfig = function(xhr) {
    xhr.withCredentials = true;
};

var quickSearchProject = {
    controller: function() {
        var self = this;
        self.nodes = [];
        self.displayedNodes = [];
        self.nonMatchingNodes = [];
        //self.lastLogin = '';
        // NEED TO FIGURE OUT WHAT TO DO ABOUT LASTLOGIN.
        self.lastLogin = '2016-01-01T15:20:11.531000';
        self.commentsCount = {};
        self.logsCount = {};
        self.sortState = m.prop();
        self.countState = m.prop();

        // Load node list
        var url = $osf.apiV2Url('users/me/nodes/', { query : { 'embed': 'contributors', 'page[size]': 100}});
        var promise = m.request({method: 'GET', url : url, config : xhrconfig});
        promise.then(function(result){
            result.data.forEach(function(node){
                self.nodes.push(node);
            });
            self.sortDateDescending();
            self.countState = 10;
            self.displayedNodes = self.nodes.splice(0, 10)
        });

        // Load last login
        self.getLastLoginDate = function () {
            var url = $osf.apiV2Url('users/me/', {});
            var promise = m.request({method: 'GET', url: url, config : xhrconfig});
            promise.then(function(result) {
                self.lastLogin = result.data.attributes.last_login
            }
            );
            return promise
        };

        self.getFamilyName = function(i, node) {
            return node.embeds.contributors.data[i].embeds.users.data.attributes.family_name
        };
        self.getContributors = function (node) {
            var numContributors = node.embeds.contributors.links.meta.total;
            if (numContributors === 1) {
                return self.getFamilyName(0, node)
            }
            else if (numContributors == 2) {
                return self.getFamilyName(0, node) + ' and ' +
                        self.getFamilyName(1, node)
            }
            else {
                return self.getFamilyName(0, node) + ', ' +
                        self.getFamilyName(1, node) + ', ' +
                        self.getFamilyName(2, node) + ' + ' + (numContributors - 3)
            }

        };

        self.getRecentComments = function (node) {
            var url = $osf.apiV2Url('nodes/' + node.id + '/comments/',
                { query : { 'filter[date_modified][gte]': self.lastLogin}}
            );
            var promise = m.request({method: 'GET', url : url, config: xhrconfig});
            promise.then(function(result) {
                self.commentsCount[node.id] = result.links.meta.total
            });
            return promise
        };

        self.loadRecentCommentCount = function (node) {
            if (node.id in self.commentsCount) {
                return self.commentsCount[node.id]
            }
            else {
                self.getRecentComments(node);
                return self.commentsCount[node.id]
            }
        };

        self.getRecentLogs = function (node) {
            var url = $osf.apiV2Url('nodes/' + node.id + '/logs/', { query : {'filter[action][ne]': 'comment_added',
                'filter[date][gte]': self.lastLogin}});
            var promise = m.request({method: 'GET', url : url, config: xhrconfig});
            promise.then(function(result){
                self.logsCount[node.id] = result.links.meta.total
            });
            return promise
        };

        self.loadRecentLogsCount = function(node) {
            if (node.id in self.logsCount){
                return self.logsCount[node.id]
            }
            else {
                self.getRecentLogs(node);
                return self.logsCount[node.id]
          }
        };

        self.loadUpToTen = function () {
            requested = self.nodes.splice(0, 10);
            for (i = 0; i < requested.length; i++) {
                self.displayedNodes.push(requested[i])
            }
            self.countState = self.displayedNodes.length;
            return self.displayedNodes
        };

        self.formatDate = function (node) {
            return new $osf.FormattableDate(node.attributes.date_modified).local
        };

        self.restoreToNodeList = function (missingNodes) {
            for (i = 0; i < missingNodes.length ; i++) {
                self.nodes.push(missingNodes[i])
            }
        };

        self.sortAlphabeticalAscending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.title.toUpperCase();
                var B = b.attributes.title.toUpperCase();
                return (A < B) ? -1 : (A > B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'alphaAsc';
            return self.displayedNodes
        };

        self.sortAlphabeticalDescending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.title.toUpperCase();
                var B = b.attributes.title.toUpperCase();
                return (A > B) ? -1 : (A < B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'alphaDesc';
            return self.displayedNodes

        };

        self.sortDateDescending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.date_modified;
                var B = b.attributes.date_modified;
                return (A > B) ? -1 : (A < B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'dateDesc';
            return self.displayedNodes
        };

        self.sortDateAscending = function () {
            self.restoreToNodeList(self.displayedNodes);
            self.nodes.sort(function(a,b){
                var A = a.attributes.date_modified;
                var B = b.attributes.date_modified;
                return (A < B) ? -1 : (A > B) ? 1 : 0;
            });
            self.displayedNodes = self.nodes.splice(0, self.countState);
            self.sortState = 'dateAsc';
            return self.displayedNodes
        };

        self.selectSameNumberOfNodes = function () {
            self.displayedNodes = self.nodes.splice(0, self.countState)
        }

        self.sortByPreviousField = function () {
            if (self.sortState === 'alphaAsc') {
                self.sortAlphabeticalAscending()
            }
            else if (self.sortState === 'alphaDesc') {
                self.sortAlphabeticalDescending()
            }
            else if (self.sortState === 'dateAsc') {
                self.sortDateAscending()
            }
            else {
                self.sortDateDescending()
            }
        };


        self.quickSearch = function () {
            var query = document.getElementById('searchQuery').value;
            self.restoreToNodeList(self.nonMatchingNodes);
            self.restoreToNodeList(self.displayedNodes);
            self.displayedNodes = [];
            self.nonMatchingNodes = [];
            // if backspace completely, previous nodes with prior sorting/count will be displayed
            if (query === '') {
                self.sortByPreviousField();
                return self.displayedNodes

            }
            else {
                for (var n = self.nodes.length - 1; n >= 0; n--) {
                    if (self.nodes[n].attributes.title.toUpperCase().indexOf(query.toUpperCase()) === -1) {
                        self.nonMatchingNodes.push(self.nodes[n]);
                        self.nodes.splice(n, 1)
                    }
                }
{'fraction_non_alphanumeric': 0.12473894057338143, 'fraction_numerical': 0.008417188785519903, 'mean_word_length': 1.5994406974831388, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '2358711', 'n_tokens_mistral': 4560, 'n_tokens_neox': 4321, 'n_words': 823}