<commit_before>
if (typeof cloudCare === 'undefined') {
    var cloudCare = {};
}

cloudCare.dispatch = _.extend({}, Backbone.Events);

cloudCare.AppNavigation = Backbone.Router.extend({

    initialize: function() {
        _.bindAll(this, 'setView');
    },

    routes: {
        // NOTE: if you edit these, you should also look at the views.py file
        "view/:app":                                 "app",
        "view/:app/:module":                         "app:module",
        "view/:app/:module/:form":                   "app:module:form",
        "view/:app/:module/:form/enter/":            "app:module:form:enter",
        "view/:app/:module/:form/case/:case":        "app:module:form:case",
        "view/:app/:module/:form/case/:case/enter/": "app:module:form:case:enter",
        "":                                          "clear"
    },
    setView: function (view) {
        this.view = view;
    }
});


cloudCare.AppSummary = Backbone.Model.extend({
    idAttribute: "_id"
});

cloudCare.AppSummaryView = Selectable.extend({
    tagName: 'li',
    initialize: function() {
        _.bindAll(this, 'render', 'toggle', 'select', 'deselect', 'enable', 'disable');
    },
    render: function() {
        $("<a />").text(this.model.get("name")).appendTo($(this.el));
        return this;
    }
});

cloudCare.AppList = Backbone.Collection.extend({
    model: cloudCare.AppSummary
});

cloudCare.AppListView = Backbone.View.extend({
    el: $('#app-list'),

    initialize: function(){
        _.bindAll(this, 'render', 'appendItem', "getAppView", "clearSelectionState");
        this.appList = new cloudCare.AppList();
        this.appList.reset(this.options.apps);
        this._appViews = {};
        this.render();
    },

    render: function () {
        var self = this;
        var ul = $("<ul />").addClass("nav nav-list").appendTo($(self.el));
        $("<li />").addClass("nav-header").text("Apps").appendTo(ul);
        _(self.appList.models).each(function(item){
            self.appendItem(item);
        });
    },
    appendItem: function (item) {
        var self = this;
        var appView = new cloudCare.AppSummaryView({
            model: item
        });

        self._appViews[item.id] = appView;

        appView.on("selected", function () {
            if (self.selectedAppView) {
                self.selectedAppView.deselect();
            }
            if (self.selectedAppView !== this) {
                self.selectedAppView = this;
                cloudCare.dispatch.trigger("app:selected", this);
            }
        });
        appView.on("deselected", function () {
            self.selectedAppView = null;
            cloudCare.dispatch.trigger("app:deselected", this);
        });

        $('ul', this.el).append(appView.render().el);
    },
    getAppView: function (appId) {
        return this._appViews[appId];
    },

    clearSelectionState: function () {
        this.selectedAppView = null;
        _(_(this._appViews).values()).each(function (view) {
            view.deselect();
        });
    }
});

cloudCare.App = LocalizableModel.extend({
    idAttribute: "_id",
    initialize: function () {
        var self = this;
        self.constructor.__super__.initialize.apply(self, [self.options]);
        _.bindAll(self, "updateModules", "url", "urlRoot", "getSubmitUrl");

        self.updateModules();
        self.on("change", function () {
            this.updateModules();
        });
    },
    url: function () {
        // HT: http://stackoverflow.com/questions/10555962/enable-django-and-tastypie-support-for-trailing-slashes
        var original_url = Backbone.Model.prototype.url.call( this );
        var parsed_url = original_url + ( original_url.charAt( original_url.length - 1 ) == '/' ? '' : '/' );
        return parsed_url;
    },
    urlRoot: function () {
        return this.get("urlRoot");
    },
    getSubmitUrl: function () {
        return this.get('post_url');
    },
    updateModules: function () {
        var self = this;
        if (self.get("modules")) {
            var index = 0;
            self.modules = _(self.get("modules")).map(function (module) {
                var ret = new cloudCare.Module(module);
                ret.set("app_id", self.id);
                ret.set("index", index);
                index++;
                return ret;
            });
        }
    }
});

cloudCare.Form = LocalizableModel.extend({
});

cloudCare.FormView = Selectable.extend({
    tagName: 'li',
    initialize: function() {
        var self = this;
        _.bindAll(self, 'render', 'toggle', 'select', 'deselect', 'enable', 'disable');
        cloudCare.dispatch.on("form:enter", function (form, caseModel) {
            self.disable();
        });
        cloudCare.dispatch.on("form:ready form:error", function (form, caseModel) {
            self.enable();
        });
    },
    render: function () {
        $("<a />").text(this.model.getLocalized("name", this.options.language)).appendTo($(this.el));
        return this;
    }
});

cloudCare.FormSession = Backbone.Model.extend({
    initialize: function() {
        _.bindAll(this, "url");
    },
    url: function () {
        // HT: http://stackoverflow.com/questions/10555962/enable-django-and-tastypie-support-for-trailing-slashes
        var original_url = Backbone.Model.prototype.url.call( this );
        var parsed_url = original_url + ( original_url.charAt( original_url.length - 1 ) == '/' ? '' : '/' );
        return parsed_url;
    }
});

cloudCare.SessionView = Selectable.extend({
    tagName: 'li',
    initialize: function() {
        _.bindAll(this, 'render', 'toggle', 'select', 'deselect', 'enable', 'disable');
    },
    render: function() {
        var self = this;
        var deleteEl = $("<a />").addClass("close").text("x");
        deleteEl.appendTo($(this.el));
        deleteEl.click(function (e) {
            e.stopPropagation();
            var dialog = confirm("Permanently delete this form? You will not be able to return to it later.");
            if (dialog == true) {
                self.model.destroy();
                showSuccess('Form was deleted.', $("#cloudcare-notifications"));
            }
        });
        $("<a />").text(this.model.get('display')).appendTo($(this.el));
        return this;
    }
});

cloudCare.SessionList = Backbone.Collection.extend({
    initialize: function () {
        var self = this;
        _.bindAll(self, 'url', 'setUrl');
        self.casedb = {};
    },
    model: cloudCare.FormSession,
    url: function () {
        return this.sessionUrl;
    },
    setUrl: function (url) {
        this.sessionUrl = url;
    }
    // TODO need to filter by form id
    // a) what is the best way to identify a form? it has to be with
    //    information available to touchforms
});

cloudCare.SessionListView = Backbone.View.extend({
    el: $('#sessions'),
    initialize: function(){
        var self = this;<commit_msg>timeout success message after 10 seconds<commit_after>
if (typeof cloudCare === 'undefined') {
    var cloudCare = {};
}

cloudCare.dispatch = _.extend({}, Backbone.Events);

cloudCare.AppNavigation = Backbone.Router.extend({

    initialize: function() {
        _.bindAll(this, 'setView');
    },

    routes: {
        // NOTE: if you edit these, you should also look at the views.py file
        "view/:app":                                 "app",
        "view/:app/:module":                         "app:module",
        "view/:app/:module/:form":                   "app:module:form",
        "view/:app/:module/:form/enter/":            "app:module:form:enter",
        "view/:app/:module/:form/case/:case":        "app:module:form:case",
        "view/:app/:module/:form/case/:case/enter/": "app:module:form:case:enter",
        "":                                          "clear"
    },
    setView: function (view) {
        this.view = view;
    }
});


cloudCare.AppSummary = Backbone.Model.extend({
    idAttribute: "_id"
});

cloudCare.AppSummaryView = Selectable.extend({
    tagName: 'li',
    initialize: function() {
        _.bindAll(this, 'render', 'toggle', 'select', 'deselect', 'enable', 'disable');
    },
    render: function() {
        $("<a />").text(this.model.get("name")).appendTo($(this.el));
        return this;
    }
});

cloudCare.AppList = Backbone.Collection.extend({
    model: cloudCare.AppSummary
});

cloudCare.AppListView = Backbone.View.extend({
    el: $('#app-list'),

    initialize: function(){
        _.bindAll(this, 'render', 'appendItem', "getAppView", "clearSelectionState");
        this.appList = new cloudCare.AppList();
        this.appList.reset(this.options.apps);
        this._appViews = {};
        this.render();
    },

    render: function () {
        var self = this;
        var ul = $("<ul />").addClass("nav nav-list").appendTo($(self.el));
        $("<li />").addClass("nav-header").text("Apps").appendTo(ul);
        _(self.appList.models).each(function(item){
            self.appendItem(item);
        });
    },
    appendItem: function (item) {
        var self = this;
        var appView = new cloudCare.AppSummaryView({
            model: item
        });

        self._appViews[item.id] = appView;

        appView.on("selected", function () {
            if (self.selectedAppView) {
                self.selectedAppView.deselect();
            }
            if (self.selectedAppView !== this) {
                self.selectedAppView = this;
                cloudCare.dispatch.trigger("app:selected", this);
            }
        });
        appView.on("deselected", function () {
            self.selectedAppView = null;
            cloudCare.dispatch.trigger("app:deselected", this);
        });

        $('ul', this.el).append(appView.render().el);
    },
    getAppView: function (appId) {
        return this._appViews[appId];
    },

    clearSelectionState: function () {
        this.selectedAppView = null;
        _(_(this._appViews).values()).each(function (view) {
            view.deselect();
        });
    }
});

cloudCare.App = LocalizableModel.extend({
    idAttribute: "_id",
    initialize: function () {
        var self = this;
        self.constructor.__super__.initialize.apply(self, [self.options]);
        _.bindAll(self, "updateModules", "url", "urlRoot", "getSubmitUrl");

        self.updateModules();
        self.on("change", function () {
            this.updateModules();
        });
    },
    url: function () {
        // HT: http://stackoverflow.com/questions/10555962/enable-django-and-tastypie-support-for-trailing-slashes
        var original_url = Backbone.Model.prototype.url.call( this );
        var parsed_url = original_url + ( original_url.charAt( original_url.length - 1 ) == '/' ? '' : '/' );
        return parsed_url;
    },
    urlRoot: function () {
        return this.get("urlRoot");
    },
    getSubmitUrl: function () {
        return this.get('post_url');
    },
    updateModules: function () {
        var self = this;
        if (self.get("modules")) {
            var index = 0;
            self.modules = _(self.get("modules")).map(function (module) {
                var ret = new cloudCare.Module(module);
                ret.set("app_id", self.id);
                ret.set("index", index);
                index++;
                return ret;
            });
        }
    }
});

cloudCare.Form = LocalizableModel.extend({
});

cloudCare.FormView = Selectable.extend({
    tagName: 'li',
    initialize: function() {
        var self = this;
        _.bindAll(self, 'render', 'toggle', 'select', 'deselect', 'enable', 'disable');
        cloudCare.dispatch.on("form:enter", function (form, caseModel) {
            self.disable();
        });
        cloudCare.dispatch.on("form:ready form:error", function (form, caseModel) {
            self.enable();
        });
    },
    render: function () {
        $("<a />").text(this.model.getLocalized("name", this.options.language)).appendTo($(this.el));
        return this;
    }
});

cloudCare.FormSession = Backbone.Model.extend({
    initialize: function() {
        _.bindAll(this, "url");
    },
    url: function () {
        // HT: http://stackoverflow.com/questions/10555962/enable-django-and-tastypie-support-for-trailing-slashes
        var original_url = Backbone.Model.prototype.url.call( this );
        var parsed_url = original_url + ( original_url.charAt( original_url.length - 1 ) == '/' ? '' : '/' );
        return parsed_url;
    }
});

cloudCare.SessionView = Selectable.extend({
    tagName: 'li',
    initialize: function() {
        _.bindAll(this, 'render', 'toggle', 'select', 'deselect', 'enable', 'disable');
    },
    render: function() {
        var self = this;
        var deleteEl = $("<a />").addClass("close").text("x");
        deleteEl.appendTo($(this.el));
        deleteEl.click(function (e) {
            e.stopPropagation();
            var dialog = confirm("Permanently delete this form? You will not be able to return to it later.");
            if (dialog == true) {
                self.model.destroy();
                showSuccess('Form was deleted.', $("#cloudcare-notifications"), 10000);
            }
        });
        $("<a />").text(this.model.get('display')).appendTo($(this.el));
        return this;
    }
});

cloudCare.SessionList = Backbone.Collection.extend({
    initialize: function () {
        var self = this;
        _.bindAll(self, 'url', 'setUrl');
        self.casedb = {};
    },
    model: cloudCare.FormSession,
    url: function () {
        return this.sessionUrl;
    },
    setUrl: function (url) {
        this.sessionUrl = url;
    }
    // TODO need to filter by form id
    // a) what is the best way to identify a form? it has to be with
    //    information available to touchforms
});

cloudCare.SessionListView = Backbone.View.extend({
    el: $('#sessions'),
    initialize: function(){
        var self = this;
{'fraction_non_alphanumeric': 0.16454154727793696, 'fraction_numerical': 0.0032234957020057307, 'mean_word_length': 2.142246230024758, 'pattern_counts': {'":': 14, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2273182', 'n_tokens_mistral': 4167, 'n_tokens_neox': 4080, 'n_words': 783}