<commit_before>/*
 * gui-builder - A simple WYSIWYG HTML5 app creator
 * Copyright (c) 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
"use strict";

// Project view widget

(function($, undefined) {

    $.widget('gb.projectView', {

        options: {
            model: null,
        },

        _create: function() {
            var o = this.options,
                e = this.element,
                widget = this;

            o.designReset = this._designResetHandler;
            o.selectionChanged = null;
            o.activePageChanged = null;
            o.modelUpdated = this._modelUpdatedHandler;

            // FIXME: This should work, but $.extend of options seems to be
            //        creating a copy of the ADM, which will not containt the
            //        same nodes and events as the master
            //o.model = o.model || ADM || undefined;
            if (o.model) {
                this._bindADMEvents(o.model);
            }

            $('<div/>').addClass(this.widgetName)
                .text(this.widgetName)
                .appendTo(this.element);

            // Add a hidden file input element, used to trigger the browsers
            // native file browse/find dialog when user wants to import a new
            // project file into the GUI Builder application
            $('<input type="file"/>')
                .attr({id:'importFile'})
                .addClass('hidden-accessible')
                .change(widget._importChangedCallback)
                .appendTo(this.element[0].ownerDocument.body);

            // Add a project setting dialog element, used to trigger to configure
            // project setting when user want to create or modify project setting
            this.options.projectDialog =  this._createSettingDialog();

            this.options.primaryTools = this._createPrimaryTools();
            this.options.secondaryTools = this._createSecondaryTools();

            this.refresh(null, this);

            return this;
        },

        _importChangedCallback: function (e) {
            var file;
            if (e.currentTarget.files.length === 1) {
                file = e.currentTarget.files[0];
                $.gb.pmUtils.importProject(file, function () {
                    // show the layout tab
                    $(document.body).tabs('select', 1);
                });
            } else {
                if (e.currentTarget.files.length <= 1) {
                    console.warn("No files specified to import");
                } else {
                    console.warn("Multiple file import not supported");
                }
            }
        },
        _setOption: function(key, value) {
            switch (key) {
                // Should this REALLY be done here, or plugin registration in
                // the "host"... using the functions mapped in widget options?
                case 'model':
                    this._unbindADMEvents();
                    this._bindADMEvents(value);
                    this.refresh(null, this);
                    break;
                default:
                    break;
            }
        },

        destroy: function() {
            // TODO: unbind any ADM event handlers
            $(this.element).find('.'+this.widgetName).remove();
            this.options.primaryTools.remove();
            this.options.secondaryTools.remove();
        },

        refresh: function(event, widget) {
            var pidArr, stage, container;
            widget = widget || this;
            container = $('.projectView');
            container.empty();
            //get project counts from PM-API
            pidArr = $.gb.pmUtils.listAllProject();
            if(!pidArr){
                console.error("Error: failed to list all projects.");
            }
            $.each(pidArr, function(index, value){
                // value is an object contains {"pid":XX, "date":XXX}
                widget.createProjectBox(value.pid, container, widget);
            });
            // Mark the active project box
            $('#'+$.gb.pmUtils.getActive(), container)
                .addClass('ui-state-active');
        },

        // Private functions
        _dialogOpenHandler: function (e, ui) {
            try {
                var name, isCreate, dialog;
                dialog = this.options && this.options.projectDialog;
                dialog = dialog || $(this).dialog('option', 'projectDialog');
                isCreate = dialog.data('new-project-dialog');
                name = $.gb.pmUtils.getName( $.gb.pmUtils.getActive()) ||
                       'Untitled';

                $("#projectName", dialog).val((isCreate)?'':name);
                $(".ui-button-text", dialog).text((isCreate)?'Next':'Done')
            }
            catch (err) {
                console.error(err.message);
                return false;
            }
        },

        _dialogCloseHandler: function (e, ui) {
            try {
                var opts = {}, isCreate, dialog;
                dialog = this.options && this.options.projectDialog;
                dialog = dialog || $(this).dialog('option', 'projectDialog');
                isCreate = dialog.data('new-project-dialog');
                opts.name = $("#projectName", dialog).val()|| "Untitled";
                //TODO: Add support for theme
                //options.theme = ("#themePicker", this).val();

                if (isCreate) {
                    //call project API to create a new project
                    $.gb.pmUtils.createProject(opts, function() {
                        // show the layout tab
                        $(document.body).tabs('select', 1);
                    });
                } else {
                    //call project API to update current project
                    $.gb.pmUtils.setProject(null, opts);
                    $(':gb-projectView').projectView('refresh');
                }
                // Blank out the title
                dialog && $('#projectName', dialog).val('');
                dialog && dialog.dialog("close");
                return true;
            }
            catch (err) {
                console.error(err.message);
                return false;
            }
        },

        _createPrimaryTools: function() {
            var tools, widget, projectDialog;
            widget = this;
            projectDialog = this.options.projectDialog;
            tools = $('<div/>').addClass('hbox').hide()
                .append('<button id="newProj"></button>')
                .append('<button id="importProj"></button>');
            tools.children().addClass('buttonStyle ui-state-default')
                .first().click( function(e) {
                    projectDialog.dialog('option', 'title', 'New Project')
                        .data('new-project-dialog',true)
                        .dialog("open");
            });
            return tools;
        },

        _createSecondaryTools: function() {
           var settingButton, projectDialog, widget;
           widget = this;
           settingButton = $('#setProj',document.body);
           projectDialog = this.options.projectDialog;
           settingButton.click(function(e){
                projectDialog.dialog('option', 'title', 'Project Settings')
                    .data('new-project-dialog',false)
                    .dialog("open");
                    e.stopPropagation();
                })
            return $('<div/>').addClass('hbox').hide()
                .append('<input type="search" />')
                .children().attr({
                    name: "filter",
                    placeholder: "filter projects"
                });
        },

        _bindADMEvents: function(a) {
            var o = this.options,
                d = this.designRoot;

            if (a) {
                o.model = a;

                if (o.designReset) {
                    a.bind("designReset", o.designReset, this);
                }
                if (o.selectionChanged) {
                    a.bind("selectionChanged", o.selectionChanged, this);
                }
                if (o.activePageChanged) {
                    a.bind("activePageChanged", o.activePageChanged, this);
                }

                // Since model changed, need to call our designReset hander
                // to sync up the ADMDesign modelUpdated event handler
                if (o.designReset) {
                    o.designReset({design: a.getDesignRoot()}, this);
                }
            }
        },

        _unbindADMEvents: function() {
            var o = this.options,
                a = this.options.model,<commit_msg>[Project View] Remove unsupported "filter projects" search bar<commit_after>/*
 * gui-builder - A simple WYSIWYG HTML5 app creator
 * Copyright (c) 2011, Intel Corporation.
 *
 * This program is licensed under the terms and conditions of the
 * Apache License, version 2.0.  The full text of the Apache License is at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 */
"use strict";

// Project view widget

(function($, undefined) {

    $.widget('gb.projectView', {

        options: {
            model: null,
        },

        _create: function() {
            var o = this.options,
                e = this.element,
                widget = this;

            o.designReset = this._designResetHandler;
            o.selectionChanged = null;
            o.activePageChanged = null;
            o.modelUpdated = this._modelUpdatedHandler;

            // FIXME: This should work, but $.extend of options seems to be
            //        creating a copy of the ADM, which will not containt the
            //        same nodes and events as the master
            //o.model = o.model || ADM || undefined;
            if (o.model) {
                this._bindADMEvents(o.model);
            }

            $('<div/>').addClass(this.widgetName)
                .text(this.widgetName)
                .appendTo(this.element);

            // Add a hidden file input element, used to trigger the browsers
            // native file browse/find dialog when user wants to import a new
            // project file into the GUI Builder application
            $('<input type="file"/>')
                .attr({id:'importFile'})
                .addClass('hidden-accessible')
                .change(widget._importChangedCallback)
                .appendTo(this.element[0].ownerDocument.body);

            // Add a project setting dialog element, used to trigger to configure
            // project setting when user want to create or modify project setting
            this.options.projectDialog =  this._createSettingDialog();

            this.options.primaryTools = this._createPrimaryTools();
            this.options.secondaryTools = this._createSecondaryTools();

            this.refresh(null, this);

            return this;
        },

        _importChangedCallback: function (e) {
            var file;
            if (e.currentTarget.files.length === 1) {
                file = e.currentTarget.files[0];
                $.gb.pmUtils.importProject(file, function () {
                    // show the layout tab
                    $(document.body).tabs('select', 1);
                });
            } else {
                if (e.currentTarget.files.length <= 1) {
                    console.warn("No files specified to import");
                } else {
                    console.warn("Multiple file import not supported");
                }
            }
        },
        _setOption: function(key, value) {
            switch (key) {
                // Should this REALLY be done here, or plugin registration in
                // the "host"... using the functions mapped in widget options?
                case 'model':
                    this._unbindADMEvents();
                    this._bindADMEvents(value);
                    this.refresh(null, this);
                    break;
                default:
                    break;
            }
        },

        destroy: function() {
            // TODO: unbind any ADM event handlers
            $(this.element).find('.'+this.widgetName).remove();
            this.options.primaryTools.remove();
            this.options.secondaryTools.remove();
        },

        refresh: function(event, widget) {
            var pidArr, stage, container;
            widget = widget || this;
            container = $('.projectView');
            container.empty();
            //get project counts from PM-API
            pidArr = $.gb.pmUtils.listAllProject();
            if(!pidArr){
                console.error("Error: failed to list all projects.");
            }
            $.each(pidArr, function(index, value){
                // value is an object contains {"pid":XX, "date":XXX}
                widget.createProjectBox(value.pid, container, widget);
            });
            // Mark the active project box
            $('#'+$.gb.pmUtils.getActive(), container)
                .addClass('ui-state-active');
        },

        // Private functions
        _dialogOpenHandler: function (e, ui) {
            try {
                var name, isCreate, dialog;
                dialog = this.options && this.options.projectDialog;
                dialog = dialog || $(this).dialog('option', 'projectDialog');
                isCreate = dialog.data('new-project-dialog');
                name = $.gb.pmUtils.getName( $.gb.pmUtils.getActive()) ||
                       'Untitled';

                $("#projectName", dialog).val((isCreate)?'':name);
                $(".ui-button-text", dialog).text((isCreate)?'Next':'Done')
            }
            catch (err) {
                console.error(err.message);
                return false;
            }
        },

        _dialogCloseHandler: function (e, ui) {
            try {
                var opts = {}, isCreate, dialog;
                dialog = this.options && this.options.projectDialog;
                dialog = dialog || $(this).dialog('option', 'projectDialog');
                isCreate = dialog.data('new-project-dialog');
                opts.name = $("#projectName", dialog).val()|| "Untitled";
                //TODO: Add support for theme
                //options.theme = ("#themePicker", this).val();

                if (isCreate) {
                    //call project API to create a new project
                    $.gb.pmUtils.createProject(opts, function() {
                        // show the layout tab
                        $(document.body).tabs('select', 1);
                    });
                } else {
                    //call project API to update current project
                    $.gb.pmUtils.setProject(null, opts);
                    $(':gb-projectView').projectView('refresh');
                }
                // Blank out the title
                dialog && $('#projectName', dialog).val('');
                dialog && dialog.dialog("close");
                return true;
            }
            catch (err) {
                console.error(err.message);
                return false;
            }
        },

        _createPrimaryTools: function() {
            var tools, widget, projectDialog;
            widget = this;
            projectDialog = this.options.projectDialog;
            tools = $('<div/>').addClass('hbox').hide()
                .append('<button id="newProj"></button>')
                .append('<button id="importProj"></button>');
            tools.children().addClass('buttonStyle ui-state-default')
                .first().click( function(e) {
                    projectDialog.dialog('option', 'title', 'New Project')
                        .data('new-project-dialog',true)
                        .dialog("open");
            });
            return tools;
        },

        _createSecondaryTools: function() {
            var settingButton, projectDialog, widget;
            widget = this;
            settingButton = $('#setProj',document.body);
            projectDialog = this.options.projectDialog;
            settingButton.click(function(e){
                projectDialog.dialog('option', 'title', 'Project Settings')
                    .data('new-project-dialog',false)
                    .dialog("open");
                e.stopPropagation();
            })
            return [];
        },

        _bindADMEvents: function(a) {
            var o = this.options,
                d = this.designRoot;

            if (a) {
                o.model = a;

                if (o.designReset) {
                    a.bind("designReset", o.designReset, this);
                }
                if (o.selectionChanged) {
                    a.bind("selectionChanged", o.selectionChanged, this);
                }
                if (o.activePageChanged) {
                    a.bind("activePageChanged", o.activePageChanged, this);
                }

                // Since model changed, need to call our designReset hander
                // to sync up the ADMDesign modelUpdated event handler
                if (o.designReset) {
                    o.designReset({design: a.getDesignRoot()}, this);
                }
            }
        },

        _unbindADMEvents: function() {
            var o = this.options,
                a = this.options.model,
{'fraction_non_alphanumeric': 0.11809632373895697, 'fraction_numerical': 0.0017098888572242804, 'mean_word_length': 1.3755754129434064, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 19, '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': '2848113', 'n_tokens_mistral': 4360, 'n_tokens_neox': 4069, 'n_words': 1033}