wallace 0.15.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/component.js CHANGED
@@ -1,10 +1,14 @@
1
1
  const throwAway = document.createElement("template");
2
2
  const NO_LOOKUP = "__";
3
3
 
4
+ const defaultSetFunction = function (props, /* #INCLUDE-IF: allowCtrl */ ctrl) {
5
+ this.props = props;
6
+ /* #INCLUDE-IF: allowCtrl */ this.ctrl = ctrl;
7
+ };
8
+
4
9
  const ComponentPrototype = {
5
10
  render: function (props, /* #INCLUDE-IF: allowCtrl */ ctrl) {
6
- this.props = props;
7
- /* #INCLUDE-IF: allowCtrl */ this.ctrl = ctrl;
11
+ this.set(props, /* #INCLUDE-IF: allowCtrl */ ctrl);
8
12
  this.update();
9
13
  },
10
14
 
@@ -57,7 +61,7 @@ const ComponentPrototype = {
57
61
  detacher = displayToggle.d;
58
62
  if (query !== undefined) {
59
63
  lookupTrue = !!lookups[query](props, this);
60
- shouldBeVisible = displayToggle.r ? lookupTrue : !lookupTrue;
64
+ shouldBeVisible = displayToggle.r ? !lookupTrue : lookupTrue;
61
65
  }
62
66
  if (detacher) {
63
67
  detacher.apply(element, shouldBeVisible, elements, stash);
@@ -100,7 +104,7 @@ const ComponentPrototype = {
100
104
 
101
105
  const ComponentBase = {
102
106
  prototype: ComponentPrototype,
103
- /* #INCLUDE-IF: allowStubs */ stubs: {}
107
+ /* #INCLUDE-IF: allowStubs */ stub: {}
104
108
  };
105
109
 
106
110
  /**
@@ -129,12 +133,12 @@ export const initConstructor = (ComponentFunction, BaseComponentFunction) => {
129
133
  Object.defineProperty(ComponentFunction, "methods", {
130
134
  set: function (value) {
131
135
  throw new Error(
132
- 'Flag "allowMethods" must be set to true in the config for this feature.'
136
+ "Flag `allowMethods` must be set to true in the config for this feature."
133
137
  );
134
138
  },
135
139
  get: function () {
136
140
  throw new Error(
137
- 'Flag "allowMethods" must be set to true in the config for this feature.'
141
+ "Flag `allowMethods` must be set to true in the config for this feature."
138
142
  );
139
143
  }
140
144
  });
@@ -142,7 +146,7 @@ export const initConstructor = (ComponentFunction, BaseComponentFunction) => {
142
146
  }
143
147
 
144
148
  /* #INCLUDE-IF: allowStubs */
145
- ComponentFunction.stubs = Object.assign({}, BaseComponentFunction.stubs);
149
+ ComponentFunction.stub = Object.assign({}, BaseComponentFunction.stub);
146
150
 
147
151
  return ComponentFunction;
148
152
  };
@@ -152,12 +156,14 @@ export const defineComponent = (
152
156
  watches,
153
157
  queries,
154
158
  contructor,
159
+ setFunction,
155
160
  /* #INCLUDE-IF: allowDismount */ dismountKeys,
156
161
  inheritFrom
157
162
  ) => {
158
163
  const ComponentDefinition = initConstructor(contructor, inheritFrom || ComponentBase);
159
164
  const proto = ComponentDefinition.prototype;
160
165
  throwAway.innerHTML = html;
166
+ proto.set = setFunction || defaultSetFunction;
161
167
  proto._w = watches;
162
168
  proto._q = queries;
163
169
  proto._t = throwAway.content.firstChild;
@@ -172,12 +178,12 @@ export const defineComponent = (
172
178
  Object.defineProperty(proto, "base", {
173
179
  set: function (value) {
174
180
  throw new Error(
175
- 'Flag "allowBase" must be set to true in the config for this feature.'
181
+ "Flag `allowBase` must be set to true in the config for this feature."
176
182
  );
177
183
  },
178
184
  get: function () {
179
185
  throw new Error(
180
- 'Flag "allowBase" must be set to true in the config for this feature.'
186
+ "Flag `allowBase` must be set to true in the config for this feature."
181
187
  );
182
188
  }
183
189
  });
package/lib/detacher.js CHANGED
@@ -16,7 +16,7 @@ It is affected by:
16
16
  - initialIndex as passed into Detacher constructor.
17
17
  - offsetTracker's initial value.
18
18
 
19
- It take either:
19
+ It takes either:
20
20
  - an element, which starts attached.
21
21
  - a nester, whose element isn't attached to begin with.
22
22
 
package/lib/router.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- The Router is a component which mounts other components based on hash (URL bit after #).
2
+ The Router is a component which mounts other components based on URL hash.
3
3
 
4
4
  It currently expects the route to be made of chunks separated by / which are either
5
5
  text or placeholders:
@@ -24,10 +24,31 @@ export const Router = () => <div></div>;
24
24
 
25
25
  Object.assign(Router.prototype, {
26
26
  render(props, /* #INCLUDE-IF: allowCtrl */ ctrl) {
27
- const defaultError = (error, router) => (router.el.innerHTML = error.message);
28
- this.error = props.error || defaultError;
27
+ if (wallaceConfig.flags.allowBase) {
28
+ } else {
29
+ throw new Error(
30
+ "Flag `allowBase` must be set to true in the config for this feature."
31
+ );
32
+ }
33
+ if (wallaceConfig.flags.allowDismount) {
34
+ } else {
35
+ throw new Error(
36
+ "Flag `allowDismount` must be set to true in the config for this feature."
37
+ );
38
+ }
39
+ this._alive = true;
40
+ this.error =
41
+ props.error ||
42
+ (error => {
43
+ throw error;
44
+ });
29
45
  this.current = null;
30
- events.forEach(e => window.addEventListener(e, () => this.onHashChange()));
46
+ this.handlers = events.map(e => {
47
+ const handler = () => this.onHashChange();
48
+ window.addEventListener(e, handler);
49
+ return { e, handler };
50
+ });
51
+
31
52
  if (props.atts) {
32
53
  Object.keys(props.atts).forEach(k => {
33
54
  this.el.setAttribute(k, props.atts[k]);
@@ -49,6 +70,7 @@ Object.assign(Router.prototype, {
49
70
  routeData,
50
71
  /* #INCLUDE-IF: allowCtrl */ this.ctrl
51
72
  );
73
+ if (!this._alive) return;
52
74
  this.current && this.current.cleanup();
53
75
  this.mount(component);
54
76
  this.current = route;
@@ -56,6 +78,7 @@ Object.assign(Router.prototype, {
56
78
  }
57
79
  i++;
58
80
  }
81
+ if (!this._alive) return;
59
82
  throw new Error(`Router unable to match path "${path}"`);
60
83
  } catch (error) {
61
84
  this.error(error, this);
@@ -64,6 +87,19 @@ Object.assign(Router.prototype, {
64
87
  mount(component) {
65
88
  this.el.textContent = "";
66
89
  this.el.appendChild(component.el);
90
+ },
91
+ /* #INCLUDE-IF: allowDismount */
92
+ dismount() {
93
+ this._alive = false;
94
+ if (this.current) {
95
+ this.current.cleanup();
96
+ }
97
+ this.base.dismount.call(this);
98
+ if (this._handlers) {
99
+ this._handlers.forEach(({ e, handler }) => {
100
+ window.removeEventListener(e, handler);
101
+ });
102
+ }
67
103
  }
68
104
  });
69
105
 
@@ -108,10 +144,10 @@ Route.prototype = {
108
144
  return this.component;
109
145
  },
110
146
  /**
111
- * Allows user to delete component or perform other cleanup.
147
+ * Allows user to dismount component or perform other cleanup.
112
148
  */
113
149
  cleanup() {
114
- this._cleanup(this);
150
+ this._cleanup(this.component);
115
151
  }
116
152
  };
117
153
 
package/lib/stubs.js CHANGED
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Gets a stub by name.
3
3
  */
4
- export const getStub = (component, name) => component.constructor.stubs[name];
4
+ export const getStub = (component, name) => component.constructor.stub[name];