{"size": 3116, "ext": "js", "max_stars_repo_path": "public/js/views/projects/NewProjectView.js", "max_stars_repo_name": "kinow/nestor", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "public/js/views/projects/NewProjectView.js", "max_issues_repo_name": "kinow/nestor", "max_issues_repo_issues_event_min_datetime": "2015-01-04T17:02:40.000Z", "max_issues_repo_issues_event_max_datetime": "2017-03-02T11:37:16.000Z", "max_forks_repo_path": "public/js/views/projects/NewProjectView.js", "max_forks_repo_name": "kinow/nestor", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 49, "max_forks_count": null, "avg_line_length": 37.5421686747, "max_line_length": 129, "alphanum_fraction": 0.4624518614}
define([
    'jquery',
    'underscore',
    'backbone',
    'app',
    'simplemde',
    'models/project/ProjectModel',
    'views/projects/ProjectsListView',
    'text!templates/projects/newProjectTemplate.html'
], function($, _, Backbone, app, SimpleMDE, ProjectModel, ProjectsListView, newProjectTemplate) {

    var NewProjectView = Backbone.View.extend({
        el: $("#page"),

        initialize: function(options) {
            _.bindAll(this, 'render', 'save');
            this.collection = options.collection;
        },

        events: {
            'click #new-project-btn': 'save'
        },

        render: function() {
            $('.item').removeClass('active');
            $('.item a[href="#/projects"]').parent().addClass('active');

            this.$el.html(newProjectTemplate);
            this.simplemde = new SimpleMDE({
                autoDownloadFontAwesome: true, 
                autofocus: false,
                autosave: {
                    enabled: false
                },
                element: $('#project-description-input')[0],
                indentWithTabs: false,
                spellChecker: false,
                tabSize: 4
            });
            this.delegateEvents();
        },

        save: function(event) {
            var self = this;
            if (this.$("#new-project-form").parsley().validate()) {
                var project = this.collection.create({
                    name: this.$("#project-name-input").val(),
                    description: this.simplemde.value(),
                    created_by: app.session.get('user_id')
                }, {
                    wait: true,
                    success: function(mod, res) {
                        app.showAlert('Success!', 'New project ' + this.$("#project-name-input").val() + ' created!', 'success');
                        var changedAttributes = project.changedAttributes();
                        var projectId = changedAttributes.id;
                        self.collection.trigger('change');
                        Backbone.history.navigate("#/projects", {
                            trigger: false
                        });
                    },
                    error: function(model, response, options) {
                        var message = _.has(response, 'statusText') ? response.statusText : 'Unknown error!';
                        if (
                            _.has(response, 'responseJSON') &&
                            _.has(response.responseJSON, 'name') &&
                            _.has(response.responseJSON.name, 'length') &&
                            response.responseJSON.name.length > 0
                        ) {
                            message = response.responseJSON.name[0];
                        }
                        app.showAlert('Failed to add new Project', message, 'error');
                    }
                });
            } else {
                if (typeof DEBUG != 'undefined' && DEBUG) console.log("Did not pass clientside validation");
            }
        }

    });

    return NewProjectView;

});