vue-ext.js 3.15 KB
define('vue-ext', ['vue-plugins', 'vue-directives'], function() {

});

define('vue-plugins', ['vue-loader']);

define('vue-directives', ['direct-proxy', 'direct-class', 'direct-scroll']);

define('vue-loader', ['vue'], function(Vue) {
	var loader = {};

	loader.install = function(Vue, options) {
		Vue.components = function(name) {
			if(Vue.component(name)) return;

			Vue.component(name, function(resolve, reject) {
				require([name], function(component) {
					Vue.importCssText(name, component.cssText, component.document);
					resolve({
						template: component.template,
						data: data
					});
				});
			});
		};

		Vue.importCssText = function( id, cssText, doc) {
			doc = doc || document;

			if(doc.getElementById( id+'.css' )) return;

			var style = doc.createElement('style');
			
			cssText += "\n/*# sourceURL=" + id + " */"; // 添加source URL 便于分清css来源
			style.appendChild( doc.createTextNode(cssText) );
			style.id = id + '.css';
			doc.querySelector('head').appendChild(style);
		};

		Vue.parseJSON = function(data) {
			return JSON.parse( JSON.stringify(data) );
		}
	};

	Vue.use(loader);
});

//example: v-proxy:click="{selector: 'a', fn: show}"
define('direct-proxy', ['jquery', 'vue'], function ($, Vue) {
	return Vue.directive('proxy', {
		bind: function(el, binding) {
			var value = binding.value,
			    event = binding.arg;

			$(el).off(event, value.selector)
				.on(event, value.selector, function(e) {
					value.fn(e.currentTarget);
				});
		}
	});		
});

define('direct-class', ['jquery', 'vue'], function($, Vue) {
	return Vue.directive('class', {
		bind: function(el, binding) {
			var value = binding.value,
				event = binding.arg || 'click',
				modifiers = binding.modifiers;

			$(el).off(event, value.selector)
				.on(event, value.selector, function(e) {
					var vclass= value.vclass,
						fn = value.fn,
					    method = modifiers.only ? "addClass" : "toggleClass";

					$(this)[method](vclass).siblings().removeClass(vclass); // toggle	

					$.isFunction(fn) && fn(e.currentTarget);
				});
		}
	});
});

define('direct-scroll', ['perfect-scrollbar', 'vue', 'css!ps_css'], function(Ps, Vue) {
	return Vue.directive('scroll', {
		componentUpdated: function(el, binding) {
			var settings = binding.value || {};
			
			$(el).css({
				'height': el.outerHeight
			})
			.perfectScrollbar(
				$.extend({
					wheelSpeed: 1,
				    wheelPropagation: true,
					minScrollbarLength: 10,
					suppressScrollX: true
                }, settings)
             );
			 
			if(settings.labelfixed) {
	            $(el).scroll(function(e) {
	                var that = $(this),
	                    top = parseInt(that.offset().top);

	                that.find('section').each(function(index, section) {
	                    var sTop, label;

	                    section = $(section);
	                    sTop = parseInt(section.offset().top);
	                    label = section.find('label');
						
						label.toggleClass("fixed", sTop <= top);
					});
	            });
			}
		},
		update: function(el, binding) {
			setTimeout(function() { 
				// 需要等待dom更新操作完成后执行
				$(el).perfectScrollbar('update');
			}, 0);
		}
	});
});