'use strict';
var $ = require('jquery');
var $osf = require('js/osfHelpers');
var Raven = require('raven-js');
var m = require('mithril');
require('js/qToggle');
require('js/components/autocomplete');
require('js/projectsSelect.js');

$(function(){
    //var getUser = $.ajax({
    //    method: 'GET',
    //    url : $osf.apiV2Url('users/me/', { query : {}}),
    //    crossOrigin: true,
    //    xhrFields: {
    //        withCredentials: true
    //    }
    //});
    //getUser.done(function(result){
    //    var currentUser = result.data.id;
    //    $.ajax({
    //        method: 'GET',
    //        url : url,
    //        crossOrigin: true,
    //        xhrFields: {
    //            withCredentials: true
    //        }
    //    }).done(function(response) {
    //        var allNodes = response.data;
    //
    //        // If we need to change what nodes can be registered, filter here
    //        var registrationSelection = ko.utils.arrayFilter(allNodes, function(node) {
    //            return $.inArray(node.permissions, ['admin']) !== -1;
    //        });
    //
    //        $osf.applyBindings({
    //            nodes: registrationSelection,
    //            enableComponents: true
    //        }, '#existingProject');
    //        $osf.applyBindings({
    //            nodes: registrationSelection,
    //            enableComponents: true
    //        }, '#existingProjectXS');
    //    }).fail(function(xhr, textStatus, error) {
    //        Raven.captureMessage('Could not fetch dashboard nodes.', {
    //            url: '/api/v1/dashboard/get_nodes/', textStatus: textStatus, error: error
    //        });
    //    });
    //})
    //
    //var url = $osf.apiV2Url('users/me/nodes', { query : { embed : 'contributors'}});


    $('.prereg-button').qToggle();
    $('.prereg-button').click(function(){
        var target = $(this).attr('data-qToggle-target');
        var input = $(target).find('input').first().focus();
    });

    // New projects
    $('#newProject, #newProjectXS').click( function() {
        var title = $(this).parent().find('.new-project-title').val();
        if (!title) {
            return;
        }
        $osf.postJSON('/api/v1/project/new/', {
            title: title,
            campaign: 'prereg'
        }).done(function(response) {
            window.location = response.projectUrl + 'registrations/?campaign=prereg';
        }).fail(function() {
            $osf.growl('Project creation failed. Reload the page and try again.');
        });
    });

    // Existing Nodes
    var allNodes = [];
    function onSelectProject (event, data) {
        var link = data.links.html + 'registrations/';
        $('#existingProject .projectRegButton').removeClass('disabled').attr('href', link);
        $('#existingProjectXS .projectRegButton').removeClass('disabled').attr('href', link);
    }
    // Get all projects with multiple calls to get all pages
    function collectProjects (url) {
        var promise = $.ajax({
            method: 'GET',
            url: url,
            xhrFields: {
                withCredentials: true
            }
        });
        promise.done(function(result){
            // loop through items and check for admin permission first
            result.data.forEach(function(item){
                item.formattedDate = new $osf.FormattableDate(item.attributes.date_modified);
                if(item.attributes.current_user_permissions.indexOf('admin') > -1){
                    allNodes.push(item);
                }
            });
            if(result.links.next){
                collectProjects(result.links.next);
            }
            else {
                $('#projectSearch').projectsSelect({data : allNodes, complete : onSelectProject});
                $('#projectSearchXS').projectsSelect({data : allNodes, complete : onSelectProject});
            }
        });
        promise.fail(function(xhr, textStatus, error) {
            Raven.captureMessage('Next page load failed for user nodes.', {
                url: url, textStatus: textStatus, error: error
            });
        });
    }
    var nodeLink = $osf.apiV2Url('users/me/nodes/', { query : { 'page[size]' : 100}});
    collectProjects(nodeLink);

    function onSelectRegistrations (event, data) {
        $('#existingPrereg .regDraftButton').removeClass('disabled').attr('href', data.url);
        $('#existingPreregXS .regDraftButton').removeClass('disabled').attr('href', data.url);
    }

    // Existing Draft Registrations
    $.getJSON('/api/v1/prereg/draft_registrations/').then(function(response){
        if (response.draftRegistrations.length) {
            $('#regDraftSearch').projectsSelect({data : response.draftRegistrations, type : 'registration', complete : onSelectRegistrations});
            $('#regDraftSearchXS').projectsSelect({data : response.draftRegistrations, type : 'registration', complete : onSelectRegistrations});
        }
    });
});

@@ -246,1539 +246,8 @@
 ()%7B%0A
-    //var getUser = $.ajax(%7B%0A    //    method: 'GET',%0A    //    url : $osf.apiV2Url('users/me/', %7B query : %7B%7D%7D),%0A    //    crossOrigin: true,%0A    //    xhrFields: %7B%0A    //        withCredentials: true%0A    //    %7D%0A    //%7D);%0A    //getUser.done(function(result)%7B%0A    //    var currentUser = result.data.id;%0A    //    $.ajax(%7B%0A    //        method: 'GET',%0A    //        url : url,%0A    //        crossOrigin: true,%0A    //        xhrFields: %7B%0A    //            withCredentials: true%0A    //        %7D%0A    //    %7D).done(function(response) %7B%0A    //        var allNodes = response.data;%0A    //%0A    //        // If we need to change what nodes can be registered, filter here%0A    //        var registrationSelection = ko.utils.arrayFilter(allNodes, function(node) %7B%0A    //            return $.inArray(node.permissions, %5B'admin'%5D) !== -1;%0A    //        %7D);%0A    //%0A    //        $osf.applyBindings(%7B%0A    //            nodes: registrationSelection,%0A    //            enableComponents: true%0A    //        %7D, '#existingProject');%0A    //        $osf.applyBindings(%7B%0A    //            nodes: registrationSelection,%0A    //            enableComponents: true%0A    //        %7D, '#existingProjectXS');%0A    //    %7D).fail(function(xhr, textStatus, error) %7B%0A    //        Raven.captureMessage('Could not fetch dashboard nodes.', %7B%0A    //            url: '/api/v1/dashboard/get_nodes/', textStatus: textStatus, error: error%0A    //        %7D);%0A    //    %7D);%0A    //%7D)%0A    //%0A    //var url = $osf.apiV2Url('users/me/nodes', %7B query : %7B embed : 'contributors'%7D%7D);%0A%0A%0A
     

Remove old api v1 project list call [skip ci]