Permalink
Browse files

feat(Event): standard evt obj emulates native Event (#726)

* feat(event): synthetic event POC

* feat(event): make getters enumerable

* feat(Event): proposal for std evt obj like native evt

* fix(event): remove timestamp leftover

* refactor(event): remove extras & comment better

* fix(event): preventDefault should only work when cancelable
  • Loading branch information...
alexsasharegan authored and tmorehouse committed Aug 4, 2017
1 parent 0a3d4cb commit 919344be16de8935472efdc69ee2b69455cec38f
Showing with 72 additions and 19 deletions.
  1. +49 −0 lib/utils/BvEvent.js
  2. +23 −19 lib/utils/object.js
@@ -0,0 +1,49 @@
import { assign, defineProperty, defineProperties, readonlyDescriptor } from "../utils/object";

export class BvEvent {
constructor(type, eventInit = {}) {
// Start by emulating native Event constructor.
if (!type) {
throw new TypeError(
`Failed to construct '${this.constructor.name}'. 1 argument required, ${arguments.length} given.`
);
}
// Assign defaults first, the eventInit,
// and the type last so it can't be overwritten.
assign(this, BvEvent.defaults(), eventInit, { type });
// Freeze some props as readonly, but leave them enumerable.
defineProperties(this, {
type: readonlyDescriptor(),
cancelable: readonlyDescriptor(),
nativeEvent: readonlyDescriptor(),
target: readonlyDescriptor(),
vueTarget: readonlyDescriptor()
});
// Create a private variable using closure scoping.
let defaultPrevented = false;
// Recreate preventDefault method. One way setter.
this.preventDefault = function preventDefault() {
if (this.cancelable) {
defaultPrevented = true;
}
};
// Create 'defaultPrevented' publicly accessible prop
// that can only be altered by the preventDefault method.
defineProperty(this, "defaultPrevented", {
enumerable: true,
get() {
return defaultPrevented;
}
});
}

static defaults() {
return {
type: "",
cancelable: true,
nativeEvent: null,
target: null,
vueTarget: null
};
}
}
@@ -10,40 +10,44 @@
if (typeof Object.assign != "function") {
Object.assign = function(target, varArgs) {
// .length of function is 2
"use strict"

if (target == null) {
// TypeError if undefined or null
throw new TypeError("Cannot convert undefined or null to object")
throw new TypeError("Cannot convert undefined or null to object");
}

let to = Object(target)
let to = Object(target);

for (let index = 1; index < arguments.length; index++) {
const nextSource = arguments[index]
const nextSource = arguments[index];

if (nextSource != null) {
// Skip over if undefined or null
for (const nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey]
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to
}
return to;
};
}

export const assign = Object.assign
export const getOwnPropertyNames = Object.getOwnPropertyNames
export const keys = Object.keys
export const defineProperties = Object.defineProperties
export const defineProperty = Object.defineProperty
export const freeze = Object.freeze
export const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
export const getOwnPropertySymbols = Object.getOwnPropertySymbols
export const getPrototypeOf = Object.getPrototypeOf
export const create = Object.create
export const isFrozen = Object.isFrozen
export const is = Object.is
export const assign = Object.assign;
export const getOwnPropertyNames = Object.getOwnPropertyNames;
export const keys = Object.keys;
export const defineProperties = Object.defineProperties;
export const defineProperty = Object.defineProperty;
export const freeze = Object.freeze;
export const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
export const getOwnPropertySymbols = Object.getOwnPropertySymbols;
export const getPrototypeOf = Object.getPrototypeOf;
export const create = Object.create;
export const isFrozen = Object.isFrozen;
export const is = Object.is;

export function readonlyDescriptor() {
return { enumerable: true, configurable: false, writable: false };
}

0 comments on commit 919344b

Please sign in to comment.