UNPKG

silk-gui

Version:

GUI for developers and Node OS

138 lines (120 loc) 2.77 kB
var _ = require('../util') var inDoc = _.inDoc /** * Setup the instance's option events & watchers. * If the value is a string, we pull it from the * instance's methods by name. */ exports._initEvents = function () { var options = this.$options registerCallbacks(this, '$on', options.events) registerCallbacks(this, '$watch', options.watch) } /** * Register callbacks for option events and watchers. * * @param {Vue} vm * @param {String} action * @param {Object} hash */ function registerCallbacks (vm, action, hash) { if (!hash) return var handlers, key, i, j for (key in hash) { handlers = hash[key] if (_.isArray(handlers)) { for (i = 0, j = handlers.length; i < j; i++) { register(vm, action, key, handlers[i]) } } else { register(vm, action, key, handlers) } } } /** * Helper to register an event/watch callback. * * @param {Vue} vm * @param {String} action * @param {String} key * @param {*} handler */ function register (vm, action, key, handler) { var type = typeof handler if (type === 'function') { vm[action](key, handler) } else if (type === 'string') { var methods = vm.$options.methods var method = methods && methods[handler] if (method) { vm[action](key, method) } else { _.warn( 'Unknown method: "' + handler + '" when ' + 'registering callback for ' + action + ': "' + key + '".' ) } } } /** * Setup recursive attached/detached calls */ exports._initDOMHooks = function () { this.$on('hook:attached', onAttached) this.$on('hook:detached', onDetached) } /** * Callback to recursively call attached hook on children */ function onAttached () { this._isAttached = true this._children.forEach(callAttach) if (this._transCpnts.length) { this._transCpnts.forEach(callAttach) } } /** * Iterator to call attached hook * * @param {Vue} child */ function callAttach (child) { if (!child._isAttached && inDoc(child.$el)) { child._callHook('attached') } } /** * Callback to recursively call detached hook on children */ function onDetached () { this._isAttached = false this._children.forEach(callDetach) if (this._transCpnts.length) { this._transCpnts.forEach(callDetach) } } /** * Iterator to call detached hook * * @param {Vue} child */ function callDetach (child) { if (child._isAttached && !inDoc(child.$el)) { child._callHook('detached') } } /** * Trigger all handlers for a hook * * @param {String} hook */ exports._callHook = function (hook) { var handlers = this.$options[hook] if (handlers) { for (var i = 0, j = handlers.length; i < j; i++) { handlers[i].call(this) } } this.$emit('hook:' + hook) }