<commit_before>/*
 * Copyright (C) 2013 Vanderbilt University, All rights reserved.
 *
 * Author: <NAME>
 */

"use strict";

define(['logManager',
    './DiagramDesignerWidget.Constants',
    './ErrorDecorator'], function (logManager,
                                 DiagramDesignerWidgetConstants,
                                 ErrorDecorator) {

    var DesignerItem,
        EVENT_POSTFIX = "DesignerItem",
        HOVER_CLASS = "hover",
        SELECTABLE_CLASS = "selectable";

    DesignerItem = function (objId, canvas) {
        this.id = objId;
        this.canvas = canvas;

        this.__initialize();

        this.logger = logManager.create("DesignerItem_" + this.id);
        this.logger.debug("Created");
    };

    DesignerItem.prototype.__initialize = function () {
        this._decoratorInstance = null;
        this._decoratorClass = null;

        this._decoratorID = "";

        this.selected = false;
        this.selectedInMultiSelection = false;

        //location and dimension information
        this.positionX = 0;
        this.positionY = 0;

        this.width = 0;
        this.height = 0;

        this._initializeUI();
    };

    DesignerItem.prototype.__setDecorator = function (decoratorName, decoratorClass, control, metaInfo) {
        if (decoratorClass === undefined) {
            //the required decorator is not available
            metaInfo = metaInfo || {};
            metaInfo["__missingdecorator__"] = decoratorName;
            decoratorClass = ErrorDecorator;
        }
        if (this._decoratorID !== decoratorClass.prototype.DECORATORID) {

            if (this._decoratorInstance) {
                //destroy old decorator
                this._callDecoratorMethod("destroy");
                this.$el.empty();
            }

            this._decoratorID = decoratorClass.prototype.DECORATORID;

            this._decoratorClass = decoratorClass;

            this._decoratorInstance = new decoratorClass({'host': this});
            this._decoratorInstance.setControl(control);
            this._decoratorInstance.setMetaInfo(metaInfo);
        }
    };

    DesignerItem.prototype.$_DOMBase = $('<div/>').attr({ "class": DiagramDesignerWidgetConstants.DESIGNER_ITEM_CLASS });

    DesignerItem.prototype._initializeUI = function () {
        //generate skin DOM and cache it
        this.$el = this.$_DOMBase.clone();

        //set additional CSS properties
        this.$el.attr({"id": this.id});

        this.$el.css({ "position": "absolute",
            "left": this.positionX,
            "top": this.positionY });

        this._attachUserInteractions();
    };

    DesignerItem.prototype._attachUserInteractions = function () {
        var i,
            self = this;

        this._events = {"mouseenter": { "fn": "onMouseEnter",
                                        "stopPropagation": true,
                                        "preventDefault": true,
                                        "enabledInReadOnlyMode": true},
                        "mouseleave": { "fn": "onMouseLeave",
                                        "stopPropagation": true,
                                        "preventDefault": true,
                                        "enabledInReadOnlyMode": true},
                        "dblclick": { "fn": "onDoubleClick",
                                        "stopPropagation": true,
                                        "preventDefault": true,
                                        "enabledInReadOnlyMode": true}};

        for (i in this._events) {
            if (this._events.hasOwnProperty(i)) {
                this.$el.on( i + '.' + EVENT_POSTFIX, null, null, function (event) {
                    var eventHandlerOpts = self._events[event.type],
                        handled = false,
                        enabled = true;

                    if (self.canvas.mode !== self.canvas.OPERATING_MODES.READ_ONLY &&
                        self.canvas.mode !== self.canvas.OPERATING_MODES.DESIGN) {
                        return;
                    }

                    if (eventHandlerOpts) {
                        if (self.canvas.mode === self.canvas.OPERATING_MODES.READ_ONLY) {
                            enabled = eventHandlerOpts.enabledInReadOnlyMode;
                        }

                        if (enabled) {
                            //call decorators event handler first
                            handled = self._callDecoratorMethod(eventHandlerOpts.fn, event);

                            if (handled !== true) {
                                handled = self[eventHandlerOpts.fn].call(self, event);
                            }

                            //if still not marked as handled
                            if (handled !== true) {
                                //finally marked handled if needed
                                if (eventHandlerOpts.stopPropagation === true) {
                                    event.stopPropagation();
                                }

                                if (eventHandlerOpts.preventDefault === true) {
                                    event.preventDefault();
                                }
                            }
                        }
                    }
                });
            }
        }
    };

    DesignerItem.prototype._detachUserInteractions = function () {
        var i;

        for (i in this._events) {
            if (this._events.hasOwnProperty(i)) {
                this.$el.off( i + '.' + EVENT_POSTFIX);
            }
        }
    };

    DesignerItem.prototype.addToDocFragment = function (docFragment) {
        this._callDecoratorMethod("on_addTo");

        this.$el.html(this._decoratorInstance.$el);

        docFragment.appendChild( this.$el[0] );

        this.logger.debug("DesignerItem with id:'" + this.id + "' added to canvas.");
    };

    DesignerItem.prototype.renderGetLayoutInfo = function () {
        this._callDecoratorMethod("onRenderGetLayoutInfo");
    };

    DesignerItem.prototype.renderSetLayoutInfo = function () {
        this._callDecoratorMethod("onRenderSetLayoutInfo");
    };

    DesignerItem.prototype._remove = function() {
        this._containerElement = null;
        this.$el.remove();
        this.$el.empty();
        this._detachUserInteractions();
        this.$el = null;
    };

    DesignerItem.prototype.destroy = function () {
        this._destroying = true;

        //destroy old decorator
        this._callDecoratorMethod("destroy");

        this._remove();

        this.logger.debug("Destroyed");
    };

    DesignerItem.prototype.getBoundingBox = function () {
        return {"x": this.positionX,
                "y": this.positionY,
                "width": this.width,
                "height": this.height,
                "x2": this.positionX + this.width,
                "y2":  this.positionY + this.height};
    };

    DesignerItem.prototype.onMouseEnter = function (/*event*/) {
        var classes = [];

        this.logger.debug("onMouseEnter: " + this.id);

        //add few classes by default
        classes.push(HOVER_CLASS);
        classes.push(SELECTABLE_CLASS);

        this.$el.addClass(classes.join(' '));

        //in edit mode and when not participating in a multiple selection,
        //show connectors
        if (this.canvas.mode === this.canvas.OPERATING_MODES.DESIGN) {
            if (this.selectedInMultiSelection === false) {
                this.showSourceConnectors();
            }
        }

        //sign we need the default preventDefault and stopPropagation to be executed
        return false;
    };<commit_msg>bugfix: show source connectors on mouseenter only if there is no connection draw in progress<commit_after>/*
 * Copyright (C) 2013 Vanderbilt University, All rights reserved.
 *
 * Author: <NAME>
 */

"use strict";

define(['logManager',
    './DiagramDesignerWidget.Constants',
    './ErrorDecorator'], function (logManager,
                                 DiagramDesignerWidgetConstants,
                                 ErrorDecorator) {

    var DesignerItem,
        EVENT_POSTFIX = "DesignerItem",
        HOVER_CLASS = "hover",
        SELECTABLE_CLASS = "selectable";

    DesignerItem = function (objId, canvas) {
        this.id = objId;
        this.canvas = canvas;

        this.__initialize();

        this.logger = logManager.create("DesignerItem_" + this.id);
        this.logger.debug("Created");
    };

    DesignerItem.prototype.__initialize = function () {
        this._decoratorInstance = null;
        this._decoratorClass = null;

        this._decoratorID = "";

        this.selected = false;
        this.selectedInMultiSelection = false;

        //location and dimension information
        this.positionX = 0;
        this.positionY = 0;

        this.width = 0;
        this.height = 0;

        this._initializeUI();
    };

    DesignerItem.prototype.__setDecorator = function (decoratorName, decoratorClass, control, metaInfo) {
        if (decoratorClass === undefined) {
            //the required decorator is not available
            metaInfo = metaInfo || {};
            metaInfo["__missingdecorator__"] = decoratorName;
            decoratorClass = ErrorDecorator;
        }
        if (this._decoratorID !== decoratorClass.prototype.DECORATORID) {

            if (this._decoratorInstance) {
                //destroy old decorator
                this._callDecoratorMethod("destroy");
                this.$el.empty();
            }

            this._decoratorID = decoratorClass.prototype.DECORATORID;

            this._decoratorClass = decoratorClass;

            this._decoratorInstance = new decoratorClass({'host': this});
            this._decoratorInstance.setControl(control);
            this._decoratorInstance.setMetaInfo(metaInfo);
        }
    };

    DesignerItem.prototype.$_DOMBase = $('<div/>').attr({ "class": DiagramDesignerWidgetConstants.DESIGNER_ITEM_CLASS });

    DesignerItem.prototype._initializeUI = function () {
        //generate skin DOM and cache it
        this.$el = this.$_DOMBase.clone();

        //set additional CSS properties
        this.$el.attr({"id": this.id});

        this.$el.css({ "position": "absolute",
            "left": this.positionX,
            "top": this.positionY });

        this._attachUserInteractions();
    };

    DesignerItem.prototype._attachUserInteractions = function () {
        var i,
            self = this;

        this._events = {"mouseenter": { "fn": "onMouseEnter",
                                        "stopPropagation": true,
                                        "preventDefault": true,
                                        "enabledInReadOnlyMode": true},
                        "mouseleave": { "fn": "onMouseLeave",
                                        "stopPropagation": true,
                                        "preventDefault": true,
                                        "enabledInReadOnlyMode": true},
                        "dblclick": { "fn": "onDoubleClick",
                                        "stopPropagation": true,
                                        "preventDefault": true,
                                        "enabledInReadOnlyMode": true}};

        for (i in this._events) {
            if (this._events.hasOwnProperty(i)) {
                this.$el.on( i + '.' + EVENT_POSTFIX, null, null, function (event) {
                    var eventHandlerOpts = self._events[event.type],
                        handled = false,
                        enabled = true;

                    if (self.canvas.mode !== self.canvas.OPERATING_MODES.READ_ONLY &&
                        self.canvas.mode !== self.canvas.OPERATING_MODES.DESIGN) {
                        return;
                    }

                    if (eventHandlerOpts) {
                        if (self.canvas.mode === self.canvas.OPERATING_MODES.READ_ONLY) {
                            enabled = eventHandlerOpts.enabledInReadOnlyMode;
                        }

                        if (enabled) {
                            //call decorators event handler first
                            handled = self._callDecoratorMethod(eventHandlerOpts.fn, event);

                            if (handled !== true) {
                                handled = self[eventHandlerOpts.fn].call(self, event);
                            }

                            //if still not marked as handled
                            if (handled !== true) {
                                //finally marked handled if needed
                                if (eventHandlerOpts.stopPropagation === true) {
                                    event.stopPropagation();
                                }

                                if (eventHandlerOpts.preventDefault === true) {
                                    event.preventDefault();
                                }
                            }
                        }
                    }
                });
            }
        }
    };

    DesignerItem.prototype._detachUserInteractions = function () {
        var i;

        for (i in this._events) {
            if (this._events.hasOwnProperty(i)) {
                this.$el.off( i + '.' + EVENT_POSTFIX);
            }
        }
    };

    DesignerItem.prototype.addToDocFragment = function (docFragment) {
        this._callDecoratorMethod("on_addTo");

        this.$el.html(this._decoratorInstance.$el);

        docFragment.appendChild( this.$el[0] );

        this.logger.debug("DesignerItem with id:'" + this.id + "' added to canvas.");
    };

    DesignerItem.prototype.renderGetLayoutInfo = function () {
        this._callDecoratorMethod("onRenderGetLayoutInfo");
    };

    DesignerItem.prototype.renderSetLayoutInfo = function () {
        this._callDecoratorMethod("onRenderSetLayoutInfo");
    };

    DesignerItem.prototype._remove = function() {
        this._containerElement = null;
        this.$el.remove();
        this.$el.empty();
        this._detachUserInteractions();
        this.$el = null;
    };

    DesignerItem.prototype.destroy = function () {
        this._destroying = true;

        //destroy old decorator
        this._callDecoratorMethod("destroy");

        this._remove();

        this.logger.debug("Destroyed");
    };

    DesignerItem.prototype.getBoundingBox = function () {
        return {"x": this.positionX,
                "y": this.positionY,
                "width": this.width,
                "height": this.height,
                "x2": this.positionX + this.width,
                "y2":  this.positionY + this.height};
    };

    DesignerItem.prototype.onMouseEnter = function (/*event*/) {
        var classes = [];

        this.logger.debug("onMouseEnter: " + this.id);

        //add few classes by default
        classes.push(HOVER_CLASS);
        classes.push(SELECTABLE_CLASS);

        this.$el.addClass(classes.join(' '));

        //in edit mode and when not participating in a multiple selection,
        //show source connectors
        //NOTE: show only if there is no connection draw in progress
        if (this.canvas.mode === this.canvas.OPERATING_MODES.DESIGN &&
            this.canvas.connectionDrawingManager.isDrawInProgress() === false) {
            if (this.selectedInMultiSelection === false) {
                this.showSourceConnectors();
            }
        }

        //sign we need the default preventDefault and stopPropagation to be executed
        return false;
    };
{'fraction_non_alphanumeric': 0.10698270339525945, 'fraction_numerical': 0.0014093529788597053, 'mean_word_length': 1.5001601537475977, 'pattern_counts': {'":': 52, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6195550', 'n_tokens_mistral': 3916, 'n_tokens_neox': 3769, 'n_words': 740}