vue.js 源代码学习笔记 ----- codegenEvents.js
/* @flow */ const fnExpRE = /^\s*([\w$_]+|\([^)]*?\))\s*=>|^function\s*\(/
const simplePathRE = /^\s*[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['.*?']|\[".*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*\s*$/ // keyCode aliases
const keyCodes = {
esc: 27,
tab: 9,
enter: 13,
space: 32,
up: 38,
left: 37,
right: 39,
down: 40,
'delete': [8, 46]
} const modifierCode = {
stop: '$event.stopPropagation();',
prevent: '$event.preventDefault();',
self: 'if($event.target !== $event.currentTarget)return;'
} const isMouseEventRE = /^mouse|^pointer|^(click|dblclick|contextmenu|wheel)$/
const mouseEventModifierCode = {
ctrl: 'if(!$event.ctrlKey)return;',
shift: 'if(!$event.shiftKey)return;',
alt: 'if(!$event.altKey)return;',
meta: 'if(!$event.metaKey)return;'
} export function genHandlers (events: ASTElementHandlers, native?: boolean): string {
let res = native ? 'nativeOn:{' : 'on:{'
for (const name in events) {
res += `"${name}":${genHandler(name, events[name])},`
}
return res.slice(0, -1) + '}'
} function genHandler (
name: string,
handler: ASTElementHandler | Array<ASTElementHandler>
): string {
if (!handler) {
return 'function(){}'
} else if (Array.isArray(handler)) {
return `[${handler.map(handler => genHandler(name, handler)).join(',')}]`
} else if (!handler.modifiers) {
return fnExpRE.test(handler.value) || simplePathRE.test(handler.value)
? handler.value
: `function($event){${handler.value}}`
} else {
let code = ''
const keys = []
const isMouseEvnet = isMouseEventRE.test(name)
for (const key in handler.modifiers) {
if (modifierCode[key]) {
code += modifierCode[key]
} else if (isMouseEvnet && mouseEventModifierCode[key]) {
code += mouseEventModifierCode[key]
} else {
keys.push(key)
}
}
if (keys.length) {
code = genKeyFilter(keys) + code
}
const handlerCode = simplePathRE.test(handler.value)
? handler.value + '($event)'
: handler.value
return 'function($event){' + code + handlerCode + '}'
}
} function genKeyFilter (keys: Array<string>): string {
const code = keys.length === 1
? normalizeKeyCode(keys[0])
: Array.prototype.concat.apply([], keys.map(normalizeKeyCode))
if (Array.isArray(code)) {
return `if(${code.map(c => `$event.keyCode!==${c}`).join('&&')})return;`
} else {
return `if($event.keyCode!==${code})return;`
}
} function normalizeKeyCode (key) {
return (
parseInt(key, 10) || // number keyCode
keyCodes[key] || // built-in alias
`_k(${JSON.stringify(key)})` // custom alias
)
}
vue.js 源代码学习笔记 ----- codegenEvents.js的更多相关文章
- vue.js 源代码学习笔记 ----- html-parse.js
/** * Not type-checking this file because it's mostly vendor code. */ /*! * HTML Parser By John Resi ...
- vue.js 源代码学习笔记 ----- helpers.js
/* @flow */ import { parseFilters } from './parser/filter-parser' export function baseWarn (msg: str ...
- vue.js 源代码学习笔记 ----- codegen.js
/* @flow */ import { genHandlers } from './events' import { baseWarn, pluckModuleFunction } from '.. ...
- vue.js 源代码学习笔记 ----- fillter-parse.js
/* @flow */ export function parseFilters (exp: string): string { let inSingle = false let inDouble = ...
- vue.js 源代码学习笔记 ----- text-parse.js
/* @flow */ import { cached } from 'shared/util' import { parseFilters } from './filter-parser' //找到 ...
- vue.js 源代码学习笔记 ----- 工具方法 lang
/* @flow */ // Object.freeze 使得这个对象不能增加属性, 修改属性, 这样就保证了这个对象在任何时候都是空的 export const emptyObject = Obje ...
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
- vue.js 源代码学习笔记 ----- 工具方法 share
/* @flow */ /** * Convert a value to a string that is actually rendered. { .. } [ .. ] 2 => '' */ ...
- vue.js 源代码学习笔记 ----- instance render
/* @flow */ import { warn, nextTick, toNumber, _toString, looseEqual, emptyObject, handleError, loos ...
随机推荐
- python os模块 os.chmod
os.chmod() 方法用于更改文件或目录的权限. os.chmod(path, mode) 参数 path -- 文件名路径或目录路径. flags -- 可用以下选项按位或操作生成, 目录的读权 ...
- LeetCode——Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- Spark提交应用程序之Spark-Submit分析
1.提交应用程序 在提交应用程序的时候,用到 spark-submit 脚本.我们来看下这个脚本: if [ -z "${SPARK_HOME}" ]; then export S ...
- 常微分方程初值问题:单步方法 [MATLAB]
#先上代码后补笔记# #可以直接复制粘贴调用的MATLAB函数代码!# 1. 朗格-库塔(Runge-Kutta)方法族 目前只实现了四阶Runge-Kutta方法. function [ YMat ...
- 系统管理命令之who
作为一名系统管理员,我们需要弄清楚在一个特定的时间点都有谁活跃在系统上.必须严密监视我们的服务器.但是,我们当然不可能一天24小时都盯着系统.在linux系统中,我们可以使用who命令查看登录者信息. ...
- HDU——2955 Robberies (0-1背包)
题意:有N个银行,每抢一个银行,可以获得\(v_i\)的前,但是会有\(p_i\)的概率被抓.现在要把被抓概率控制在\(P\)之下,求最多能抢到多少钱. 分析:0-1背包的变形,把重量变成了概率,因为 ...
- MySQL从删库到跑路(二)——MySQL字符集与乱码解析
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.字符集与编码 1.字符集简介 字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符 ...
- pycharm添加git ignore
pycharm现在提供了git ignore,很方便 从这里下载扩展 https://plugins.jetbrains.com/plugin/7495--ignore 放到pycharm根目录\pl ...
- C++虚函数分析
1.虚函数(impure virtual) c++虚函数主要是提供“运行时多态”,父类提供虚函数的默认实现,子类可以虚函数进行重写. 2.纯虚函数(pure virtual) c++纯虚函 ...
- bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower
P2895 [USACO08FEB]流星雨Meteor Shower 给每个点标记一下能够走的最迟时间,蓝后bfs处理一下 #include<iostream> #include<c ...