<commit_before>/*
 *     Copyright (c) 2012-2016 CoNWeT Lab., Universidad Politécnica de Madrid
 *
 *     This file is part of Wirecloud Platform.
 *
 *     Wirecloud Platform is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU Affero General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 *
 *     Wirecloud is distributed in the hope that it will be useful, but WITHOUT
 *     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 *     License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with Wirecloud Platform.  If not, see
 *     <http://www.gnu.org/licenses/>.
 *
 */

/*global CatalogueSearchView, gettext, interpolate, LayoutManagerFactory, Wirecloud, StyledElements*/

(function () {

    "use strict";

    var MyResourcesView = function MyResourcesView(id, options) {
        var resource_extra_context;

        options.class = 'catalogue myresources';
        StyledElements.Alternative.call(this, id, options);

        this.catalogue = Wirecloud.LocalCatalogue;
        this.alternatives = new StyledElements.Alternatives();
        this.appendChild(this.alternatives);

        var search_options = {
            catalogue: this,
            resource_painter: Wirecloud.ui.ResourcePainter,
            resource_extra_context: resource_extra_context,
            emptyTitle: gettext('Empty resource list'),
            emptyMessage: gettext('Currently, you do not have access to any component. You can get components using the Marketplace view or by uploading components manually using the Upload button.')
        };
        this.viewsByName = {
            'search': this.alternatives.createAlternative({alternative_constructor: CatalogueSearchView, containerOptions: search_options}),
            'details': this.alternatives.createAlternative({alternative_constructor: Wirecloud.ui.WirecloudCatalogue.ResourceDetailsView, containerOptions: {catalogue: this}})
        };
        this.viewsByName.search.init();

        this.uploadButton = new StyledElements.Button({
            'iconClass': 'icon-cloud-upload',
            'title': gettext('Upload')
        });
        var upload_dialog = new Wirecloud.ui.WirecloudCatalogue.UploadWindowMenu({catalogue: this.catalogue, mainview: this});
        this.uploadButton.addEventListener('click', function () {
            upload_dialog.show();
        });

        this.marketButton = new StyledElements.Button({
            'iconClass': 'icon-shopping-cart',
            'title': gettext('Get more components')
        });
        this.marketButton.addEventListener('click', function () {
            LayoutManagerFactory.getInstance().changeCurrentView('marketplace');
        });

        this.alternatives.addEventListener('preTransition', function (alternatives, old_alternative, new_alternative) {
            LayoutManagerFactory.getInstance().header._notifyViewChange();
        }.bind(this));
        this.alternatives.addEventListener('postTransition', function (alternatives, old_alternative, new_alternative) {
            setTimeout(Wirecloud.trigger.bind(Wirecloud, 'viewcontextchanged'), 0);
        }.bind(this));

        this.addEventListener('show', function () {
            Wirecloud.UserInterfaceManager.rootKeydownHandler = keydown_listener.bind(this);
            this.refresh_if_needed();
        }.bind(this));
    };
    MyResourcesView.prototype = new StyledElements.Alternative();

    MyResourcesView.prototype.view_name = 'myresources';

    MyResourcesView.prototype.buildStateData = function buildStateData() {
        var currentState, data, subview;

        currentState = Wirecloud.HistoryManager.getCurrentState();
        data = {
            workspace_owner: currentState.workspace_owner,
            workspace_name: currentState.workspace_name,
            view: 'myresources',
            subview: 'search'
        };

        subview = this.alternatives.getCurrentAlternative();
        if (subview.view_name != null) {
            data.subview = subview.view_name;
            if ('buildStateData' in subview) {
                subview.buildStateData(data);
            }
        }

        return data;
    };

    MyResourcesView.prototype.onHistoryChange = function onHistoryChange(state) {
        var details, parts, currentResource;

        if (state.subview === 'search') {
            this.changeCurrentView(state.subview, {});
        } else {
            parts = state.resource.split('/');
            details = {
                vendor: parts[0],
                name: parts[1]
            };

            currentResource = this.viewsByName.details.currentEntry;
            if (currentResource != null && currentResource.vendor == details.vendor && currentResource.name == details.name) {
                details = currentResource;
            }
            this.createUserCommand('showDetails', details, {tab: state.tab, version: parts[2]})();
        }
    };

    MyResourcesView.prototype.goUp = function goUp() {
        if (this.alternatives.getCurrentAlternative() === this.viewsByName.search) {
            LayoutManagerFactory.getInstance().changeCurrentView('workspace');
        }
        this.changeCurrentView('search');
    };

    MyResourcesView.prototype.getBreadcrum = function getBreadcrum() {
        var breadcrum = [gettext('My Resources')];

        if (this.alternatives.getCurrentAlternative() === this.viewsByName.details && this.viewsByName.details.currentEntry != null) {
            breadcrum.push(this.viewsByName.details.currentEntry.title);
        }

        return breadcrum;
    };

    MyResourcesView.prototype.getTitle = function getTitle() {
        var currentResource;

        if (this.alternatives.getCurrentAlternative() === this.viewsByName.details) {
            currentResource = this.viewsByName.details.currentEntry;
            if (currentResource != null) {
                return Wirecloud.Utils.interpolate(gettext('My Resources - %(resource)s'), {resource: currentResource.uri});
            }
        }
        return gettext('My Resources');
    };

    MyResourcesView.prototype.getToolbarButtons = function getToolbarButtons() {
        return [this.uploadButton, this.marketButton];
    };

    MyResourcesView.prototype.search = function search(onSuccess, onError, options) {
        return this.catalogue.search(onSuccess, onError, options);
    };

    MyResourcesView.prototype.changeCurrentView = function changeCurrentView(view_name, options) {
        if (!(view_name in this.viewsByName)) {
            throw new TypeError();
        }

        if (options == null) {
            options = {
                onComplete: function (alternatives, out_alternative) {
                    this.refresh_if_needed();
                    var new_status = this.buildStateData();
                    Wirecloud.HistoryManager.pushState(new_status);
                    Wirecloud.trigger('viewcontextchanged');
                    Wirecloud.Utils.callCallback(old_oncomplete, alternatives, out_alternative);
                }.bind(this)
            };
        }

        this.alternatives.showAlternative(this.viewsByName[view_name], options);
    };

    MyResourcesView.prototype.home = function home() {
        this.changeCurrentView('search');
    };

    MyResourcesView.prototype.createUserCommand = function createUserCommand(command) {
        return this.ui_commands[command].apply(this, Array.prototype.slice.call(arguments, 1));
    };

    MyResourcesView.prototype.ui_commands = {};

    MyResourcesView.prototype.ui_commands.install = function install(resource, catalogue_source) {
        return function () {
            var layoutManager;

            layoutManager = LayoutManagerFactory.getInstance();
            layoutManager._startComplexTask(gettext("Importing resource into local repository"), 3);
            layoutManager.logSubTask(gettext('Uploading resource'));

            this.catalogue.addResourceFromURL(resource.description_url, {
                onSuccess: function () {
                    LayoutManagerFactory.getInstance().logSubTask(gettext('Resource installed successfully'));<commit_msg>Remove unused code<commit_after>/*
 *     Copyright (c) 2012-2016 CoNWeT Lab., Universidad Politécnica de Madrid
 *
 *     This file is part of Wirecloud Platform.
 *
 *     Wirecloud Platform is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU Affero General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 *
 *     Wirecloud is distributed in the hope that it will be useful, but WITHOUT
 *     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 *     License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with Wirecloud Platform.  If not, see
 *     <http://www.gnu.org/licenses/>.
 *
 */

/*global CatalogueSearchView, gettext, interpolate, LayoutManagerFactory, Wirecloud, StyledElements*/

(function () {

    "use strict";

    var MyResourcesView = function MyResourcesView(id, options) {
        var resource_extra_context;

        options.class = 'catalogue myresources';
        StyledElements.Alternative.call(this, id, options);

        this.catalogue = Wirecloud.LocalCatalogue;
        this.alternatives = new StyledElements.Alternatives();
        this.appendChild(this.alternatives);

        var search_options = {
            catalogue: this,
            resource_painter: Wirecloud.ui.ResourcePainter,
            resource_extra_context: resource_extra_context,
            emptyTitle: gettext('Empty resource list'),
            emptyMessage: gettext('Currently, you do not have access to any component. You can get components using the Marketplace view or by uploading components manually using the Upload button.')
        };
        this.viewsByName = {
            'search': this.alternatives.createAlternative({alternative_constructor: CatalogueSearchView, containerOptions: search_options}),
            'details': this.alternatives.createAlternative({alternative_constructor: Wirecloud.ui.WirecloudCatalogue.ResourceDetailsView, containerOptions: {catalogue: this}})
        };
        this.viewsByName.search.init();

        this.uploadButton = new StyledElements.Button({
            'iconClass': 'icon-cloud-upload',
            'title': gettext('Upload')
        });
        var upload_dialog = new Wirecloud.ui.WirecloudCatalogue.UploadWindowMenu({catalogue: this.catalogue, mainview: this});
        this.uploadButton.addEventListener('click', function () {
            upload_dialog.show();
        });

        this.marketButton = new StyledElements.Button({
            'iconClass': 'icon-shopping-cart',
            'title': gettext('Get more components')
        });
        this.marketButton.addEventListener('click', function () {
            LayoutManagerFactory.getInstance().changeCurrentView('marketplace');
        });

        this.alternatives.addEventListener('preTransition', function (alternatives, old_alternative, new_alternative) {
            LayoutManagerFactory.getInstance().header._notifyViewChange();
        }.bind(this));
        this.alternatives.addEventListener('postTransition', function (alternatives, old_alternative, new_alternative) {
            setTimeout(Wirecloud.trigger.bind(Wirecloud, 'viewcontextchanged'), 0);
        }.bind(this));

        this.addEventListener('show', function () {
            Wirecloud.UserInterfaceManager.rootKeydownHandler = keydown_listener.bind(this);
            this.refresh_if_needed();
        }.bind(this));
    };
    MyResourcesView.prototype = new StyledElements.Alternative();

    MyResourcesView.prototype.view_name = 'myresources';

    MyResourcesView.prototype.buildStateData = function buildStateData() {
        var currentState, data, subview;

        currentState = Wirecloud.HistoryManager.getCurrentState();
        data = {
            workspace_owner: currentState.workspace_owner,
            workspace_name: currentState.workspace_name,
            view: 'myresources',
            subview: 'search'
        };

        subview = this.alternatives.getCurrentAlternative();
        if (subview.view_name != null) {
            data.subview = subview.view_name;
            if ('buildStateData' in subview) {
                subview.buildStateData(data);
            }
        }

        return data;
    };

    MyResourcesView.prototype.onHistoryChange = function onHistoryChange(state) {
        var details, parts, currentResource;

        if (state.subview === 'search') {
            this.changeCurrentView(state.subview, {});
        } else {
            parts = state.resource.split('/');
            details = {
                vendor: parts[0],
                name: parts[1]
            };

            currentResource = this.viewsByName.details.currentEntry;
            if (currentResource != null && currentResource.vendor == details.vendor && currentResource.name == details.name) {
                details = currentResource;
            }
            this.createUserCommand('showDetails', details, {tab: state.tab, version: parts[2]})();
        }
    };

    MyResourcesView.prototype.goUp = function goUp() {
        if (this.alternatives.getCurrentAlternative() === this.viewsByName.search) {
            LayoutManagerFactory.getInstance().changeCurrentView('workspace');
        }
        this.changeCurrentView('search');
    };

    MyResourcesView.prototype.getBreadcrum = function getBreadcrum() {
        var breadcrum = [gettext('My Resources')];

        if (this.alternatives.getCurrentAlternative() === this.viewsByName.details && this.viewsByName.details.currentEntry != null) {
            breadcrum.push(this.viewsByName.details.currentEntry.title);
        }

        return breadcrum;
    };

    MyResourcesView.prototype.getTitle = function getTitle() {
        var currentResource;

        if (this.alternatives.getCurrentAlternative() === this.viewsByName.details) {
            currentResource = this.viewsByName.details.currentEntry;
            if (currentResource != null) {
                return Wirecloud.Utils.interpolate(gettext('My Resources - %(resource)s'), {resource: currentResource.uri});
            }
        }
        return gettext('My Resources');
    };

    MyResourcesView.prototype.getToolbarButtons = function getToolbarButtons() {
        return [this.uploadButton, this.marketButton];
    };

    MyResourcesView.prototype.search = function search(onSuccess, onError, options) {
        return this.catalogue.search(onSuccess, onError, options);
    };

    MyResourcesView.prototype.changeCurrentView = function changeCurrentView(view_name, options) {
        if (!(view_name in this.viewsByName)) {
            throw new TypeError();
        }

        if (options == null) {
            options = {
                onComplete: function (alternatives, out_alternative) {
                    this.refresh_if_needed();
                    var new_status = this.buildStateData();
                    Wirecloud.HistoryManager.pushState(new_status);
                    Wirecloud.trigger('viewcontextchanged');
                }.bind(this)
            };
        }

        this.alternatives.showAlternative(this.viewsByName[view_name], options);
    };

    MyResourcesView.prototype.home = function home() {
        this.changeCurrentView('search');
    };

    MyResourcesView.prototype.createUserCommand = function createUserCommand(command) {
        return this.ui_commands[command].apply(this, Array.prototype.slice.call(arguments, 1));
    };

    MyResourcesView.prototype.ui_commands = {};

    MyResourcesView.prototype.ui_commands.install = function install(resource, catalogue_source) {
        return function () {
            var layoutManager;

            layoutManager = LayoutManagerFactory.getInstance();
            layoutManager._startComplexTask(gettext("Importing resource into local repository"), 3);
            layoutManager.logSubTask(gettext('Uploading resource'));

            this.catalogue.addResourceFromURL(resource.description_url, {
                onSuccess: function () {
                    LayoutManagerFactory.getInstance().logSubTask(gettext('Resource installed successfully'));
{'fraction_non_alphanumeric': 0.10175669216061185, 'fraction_numerical': 0.0017925430210325047, 'mean_word_length': 2.7094414893617023, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3108941', 'n_tokens_mistral': 4266, 'n_tokens_neox': 4144, 'n_words': 898}