/**
 * Created by huangxinghui on 2015/5/15.
 */

var $ = require('jquery');
var _ = require('underscore');
var Class = require('./clazz');
var delegateEventSplitter = /^(\S+)\s*(.*)$/;

module.exports = Class.extend({
    options: null,
    events: null,

    init: function (element, options) {
        this.$element = $(element);
        this.options = $.extend({}, this.options, this.$element.data(), options);

        this._create();
        this.delegateEvents();
    },

    _create: $.noop,

    delegateEvents: function (events) {
        if (!(events || (events = this.events))) return;

        this.undelegateEvents();
        for (var key in events) {
            var method = events[key];
            if (!_.isFunction(method)) method = this[events[key]];
            if (!method) continue;
            var match = key.match(delegateEventSplitter);
            this.delegate(match[1], match[2], _.bind(method, this));
        }
    },

    delegate: function (eventName, selector, listener) {
        this.$element.on(eventName + '.' + this.widgetName, selector, listener);
    },

    undelegateEvents: function () {
        if (this.$element)
            this.$element.off('.' + this.widgetName);
    },

    undelegate: function (eventName, selector, listener) {
        this.$element.off(eventName + '.' + this.widgetName, selector, listener);
    },

    trigger: function (type, event, data) {
        var prop, orig,
            callback = this.options[type];

        data = data || {};
        event = $.Event(event);
        event.type = (type + '.' + this.widgetName).toLowerCase();
        event.target = this.$element[0];

        // copy original event properties over to the new event
        orig = event.originalEvent;
        if (orig) {
            for (prop in orig) {
                if (!( prop in event )) {
                    event[prop] = orig[prop];
                }
            }
        }

        this.$element.trigger(event, data);
    },

    destroy: function () {
        this.undelegateEvents();
        this.$element.removeData(this.widgetName);
    }
});
@@ -1396,15 +1396,8 @@
 ype,
- event,
  dat
@@ -1413,555 +1413,74 @@
     
-var prop, orig,%0A            callback = this.options%5Btype%5D;%0A%0A        data = data %7C%7C %7B%7D;%0A        event = $.Event(event);%0A        event.type = (type + '.' + this.widgetName).toLowerCase();%0A        event.target = this.$element%5B0%5D;%0A%0A        // copy original event properties over to the new event%0A        orig = event.originalEvent;%0A        if (orig) %7B%0A            for (prop in orig) %7B%0A                if (!( prop in event )) %7B%0A                    event%5Bprop%5D = orig%5Bprop%5D;%0A                %7D%0A            %7D%0A        %7D%0A%0A        this.$element.trigger(event
+this.$element.trigger((type + '.' + this.widgetName).toLowerCase()
 , da

change widget trigger method