/**
 * UI and function to quick search projects
 */

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

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


var quickSearchProject = {
    controller: function() {
        var self = this;
        self.nodes = [];
        self.getMyNodes = function () {
            var url = $osf.apiV2Url('users/me/nodes/', {});
            var promise = m.request({method: 'GET', url : url, config : xhrconfig});
            promise.then(function(result){
                //console.log(result)
                result.data.forEach(function(node){
                    self.nodes.push(node)
                })
            });
            return promise;
        };
        self.getMyNodes()

    },
    view : function(ctrl) {
        console.log(ctrl.nodes);
        return [
            m("h1", 'My nodes'),
            m('p', ctrl.nodes.map(function(n){
               return m('li', n.attributes.title)}))]
    }
};

module.exports = quickSearchProject;


@@ -449,16 +449,51 @@
 des/', %7B
+ query : %7B 'embed': 'contributors'%7D
 %7D);%0A    
@@ -811,16 +811,981 @@
 romise;%0A
+        %7D;%0A        self.getContributors = function (node) %7B%0A            var numContributors = node.embeds.contributors.links.meta.total%0A            if (numContributors === 1) %7B%0A                return node.embeds.contributors.data%5B0%5D.embeds.users.data.attributes.family_name%0A            %7D%0A            else if (numContributors == 2) %7B%0A                return node.embeds.contributors.data%5B0%5D.embeds.users.data.attributes.family_name + ' and ' +%0A                        node.embeds.contributors.data%5B1%5D.embeds.users.data.attributes.family_name%0A            %7D%0A            else %7B%0A                return node.embeds.contributors.data%5B0%5D.embeds.users.data.attributes.family_name + ', ' +%0A                        node.embeds.contributors.data%5B1%5D.embeds.users.data.attributes.family_name + ', ' +%0A                        node.embeds.contributors.data%5B2%5D.embeds.users.data.attributes.family_name + ' and ' +%0A                    (numContributors - 3) + ' others'%0A            %7D%0A%0A
         
@@ -1932,16 +1932,74 @@
 odes'),%0A
+            m('div', 'Name', 'Contributors', 'Modified'),%0A
         
@@ -2062,18 +2062,19 @@
 turn m('
-li
+div
 ', n.att
@@ -2086,16 +2086,70 @@
 es.title
+, ctrl.getContributors(n),  n.attributes.date_modified
 )%7D))%5D%0A  

Add contributors to project entries