Skip to content

Commit 7d8e3d0

Browse files
authored
Merge pull request #116 from offirgolan/fixes-on-fixes
Code cleanup and logic fixes
2 parents cea4238 + fd2332f commit 7d8e3d0

File tree

11 files changed

+73
-47
lines changed

11 files changed

+73
-47
lines changed

addon/-private/Record.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
import Ember from 'ember';
12
import { unwrapValue } from 'ember-time-machine/utils/value';
23

4+
const {
5+
isArray
6+
} = Ember;
7+
38
export default class Record {
4-
constructor(target, path, key, before, after, isArray = false) {
9+
constructor({ target, path, key, before, after }) {
510
this.target = target;
611
this.path = path;
712
this.key = key;
813
this.before = unwrapValue(before);
914
this.after = unwrapValue(after);
10-
this.isArray = isArray;
15+
this.isArray = isArray(target) && typeof key === 'number';
1116

1217
this.pathToKey = path.join('.');
1318
this.fullPath = path.concat(this.key).join('.');

addon/mixins/time-machine.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export default Ember.Mixin.create({
230230
let content = this.get('content');
231231

232232
if (isArray(content)) {
233-
return emberArray(content).invoke(...arguments);
233+
return emberArray(content).invoke(methodName, ...args);
234234
} else {
235235
return tryInvoke(content, methodName, args);
236236
}
@@ -323,7 +323,9 @@ export default Ember.Mixin.create({
323323
} else {
324324
RecordUtils.redoArrayRecord(record);
325325
}
326-
} else if (isLast || record.fullPath !== nextRecord.fullPath) {
326+
} else if (isLast ||
327+
record.fullPath !== nextRecord.fullPath ||
328+
record.target !== nextRecord.target) {
327329
/*
328330
Apply the last object property change that occured in a row.
329331
ex) If firstName changed 5 times in a row and we undo, then apply only
@@ -340,10 +342,10 @@ export default Ember.Mixin.create({
340342
* Extract the specified number of records from the given stack
341343
*
342344
* @method _extractRecords
343-
* @param {String} type 'undo' or 'redo'
344-
* @param {Number} numRecords Number of records to apply
345-
* @param {Object} options
346-
* @return {Array} Records that were extracted
345+
* @param {Array} stack
346+
* @param {Number} numRecords Number of records to apply
347+
* @param {Object} options
348+
* @return {Array} Records that were extracted
347349
* @private
348350
*/
349351
_extractRecords(stack, numRecords, options = {}) {

addon/proxies/array.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default Ember.ArrayProxy.extend(RecordKeeperMixin, {
1212
replaceContent(startIndex, numRemoved, objects) {
1313
let state = this.get('_rootMachineState');
1414
let path = this.get('_path');
15+
let content = this.get('content');
1516
let before, after;
1617

1718
if (state && !pathInGlobs(path.join('.'), state.get('frozenProperties'))) {
@@ -21,7 +22,13 @@ export default Ember.ArrayProxy.extend(RecordKeeperMixin, {
2122
after = objects;
2223
}
2324

24-
this._addRecord(new Record(this.get('content'), path, startIndex, before, after, true));
25+
this._addRecord(new Record({
26+
target: content,
27+
path,
28+
key: startIndex,
29+
before,
30+
after
31+
}));
2532

2633
return this._super(startIndex, numRemoved, unwrapValue(objects));
2734
}

addon/proxies/object.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ export default Ember.ObjectProxy.extend(RecordKeeperMixin, {
1515
let path = this.get('_path');
1616

1717
if (state && !pathInGlobs(path.concat(key).join('.'), state.get('frozenProperties'))) {
18-
this._addRecord(new Record(content, path, key, content.get(key), value, false));
18+
this._addRecord(new Record({
19+
target: content,
20+
path,
21+
key,
22+
before: content.get(key),
23+
after: value
24+
}));
25+
1926
return this._super(key, unwrapValue(value));
2027
}
2128

addon/utils/utils.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import require from 'require';
2-
3-
export function requireModule(module) {
4-
return require.has(module) ? require(module)['default'] : undefined;
5-
}
6-
71
export function pathInGlobs(path = '', globs = []) {
82
for (let i = 0; i < globs.length; i++) {
9-
let regex = (`.${globs[i]}`).replace(new RegExp(/\.\*/, 'g'), '\\.(\\w+)').replace(new RegExp(/\.@each/, 'g'), '\\.(\\d+)');
10-
11-
if ((`.${path}`).match(regex)) {
3+
if ((`.${path}`).match(regexFromGlob(globs[i]))) {
124
return true;
135
}
146
}
157

168
return false;
179
}
10+
11+
function regexFromGlob(glob) {
12+
return (`.${glob}`)
13+
.replace(new RegExp(/\.\*/, 'g'), '\\.(\\w+)')
14+
.replace(new RegExp(/\.@each/, 'g'), '\\.(\\d+)');
15+
}

addon/utils/value.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function wrapValue(obj, key, value) {
1717
return availableMachines.get(value);
1818
}
1919

20-
if (shouldWrapValue(...arguments)) {
20+
if (shouldWrapValue(obj, key, value)) {
2121
if (isArray(value)) {
2222
Machine = TimeMachine.Array;
2323
} else if (typeof value === 'object') {
@@ -40,12 +40,12 @@ export function wrapValue(obj, key, value) {
4040
}
4141

4242
export function unwrapValue(value) {
43-
if (value && isArray(value)) {
44-
return value.map((v) => get(v, 'isTimeMachine') ? unwrapValue(get(v, 'content')) : v);
43+
if (value && get(value, 'isTimeMachine')) {
44+
return unwrapValue(get(value, 'content'));
4545
}
4646

47-
if (value && (value instanceof Ember.ObjectProxy || get(value, 'isTimeMachine'))) {
48-
return unwrapValue(get(value, 'content'));
47+
if (value && isArray(value)) {
48+
return value.map((v) => get(v, 'isTimeMachine') ? unwrapValue(get(v, 'content')) : v);
4949
}
5050

5151
return value;
@@ -54,12 +54,11 @@ export function unwrapValue(value) {
5454
function shouldWrapValue(obj, key, value) {
5555
let state = obj.get('_rootMachineState');
5656
let maxDepth = state.get('maxDepth');
57-
let shouldWrapValue = state.get('shouldWrapValue');
58-
5957
let fullPath = obj.get('_path').concat(key);
6058
let valueType = typeOf(value);
61-
let trackCurrDepth = maxDepth < 0 || fullPath.length <= maxDepth;
62-
let correctValueType = valueType === 'object' || valueType === 'instance' || valueType === 'array';
6359

64-
return value && correctValueType && trackCurrDepth && !get(value, 'isTimeMachine') && shouldWrapValue(value, obj, key);
60+
return (valueType === 'object' || valueType === 'instance' || valueType === 'array') &&
61+
(maxDepth < 0 || fullPath.length <= maxDepth) &&
62+
!get(value, 'isTimeMachine') &&
63+
state.shouldWrapValue(value, obj, key);
6564
}

tests/dummy/app/controllers/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import Ember from 'ember';
22

3+
const {
4+
isEmpty
5+
} = Ember;
6+
37
export default Ember.Controller.extend({
48
store: Ember.inject.service(),
59

@@ -8,15 +12,20 @@ export default Ember.Controller.extend({
812

913
actions: {
1014
addTask() {
11-
let newTask = this.get('store').createRecord('task', {
12-
title: this.get('newTask')
13-
});
15+
let title = this.get('newTask');
16+
17+
if (isEmpty(title)) {
18+
return;
19+
}
20+
21+
let newTask = this.get('store').createRecord('task', { title });
1422

1523
if (this.get('model.settings.newOnTop')) {
1624
this.get('model.tasks').insertAt(0, newTask);
1725
} else {
1826
this.get('model.tasks').pushObject(newTask);
1927
}
28+
2029
this.set('newTask', '');
2130
},
2231
removeTask(task) {

tests/dummy/app/models/user.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ const { computed } = Ember;
77
export default DS.Model.extend({
88
firstName: attr('string'),
99
lastName: attr('string'),
10-
username: attr('string'),
1110
avatar: attr('string'),
1211

12+
username: computed('firstName', function() {
13+
return `${this.get('firstName')}ster`;
14+
}).readOnly(),
15+
1316
settings: DS.belongsTo('setting'),
1417
tasks: DS.hasMany('task'),
1518

tests/dummy/app/styles/app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ nav {
105105
margin: 0;
106106
}
107107
.new-task-icon {
108+
cursor: pointer;
108109
margin-top: 30px;
109110
font-size: 36px;
110111
font-weight: 200;

tests/dummy/app/templates/index.hbs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
<div class="row">
44
<div class="col s4">
55
{{#md-card title=model.displayName image=model.avatar activator=true class="user-card"}}
6-
{{#md-card-content}}
7-
{{!-- <div class="row center-align">
8-
<img src={{model.avatar}} alt="" width="200px" class="circle deep-orange lighten-2 responsive-img"> <!-- notice the "circle" class -->
9-
</div> --}}
10-
{{/md-card-content}}
6+
{{#md-card-content}}{{/md-card-content}}
117

128
{{#md-card-reveal}}
139
<div class="row">
14-
{{md-input value=model.username label='Username' class="col s12"}}
10+
{{md-input value=model.username label='Username' class="col s12" disabled=true}}
1511
{{md-input value=model.firstName label='First Name' class="col s12"}}
1612
{{md-input value=model.lastName label='Last Name' class="col s12"}}
1713
</div>
@@ -29,7 +25,7 @@
2925
<br>
3026
<p>{{md-switch checked=model.settings.showTabs name="Show Tabs"}}</p>
3127
<br>
32-
<p>{{md-switch checked=model.settings.sortableCards name="Use Sortable Cards"}}</p>
28+
<p>{{md-switch checked=model.settings.sortableCards name="Sortable Cards"}}</p>
3329
</div>
3430
</div>
3531
{{/md-card-content}}
@@ -41,7 +37,7 @@
4137
{{#md-card-content}}
4238
<div class="row">
4339
<div class="col s1 center-align">
44-
<span class="new-task-icon">+</span>
40+
<span class="new-task-icon" {{action 'addTask'}}>+</span>
4541
</div>
4642
<div class="col s11">
4743
{{input value=newTask placeholder="What needs to be done?" enter=(action 'addTask')}}
@@ -109,25 +105,25 @@
109105
class="fixed-btns"
110106
btnIcon="fa fa-undo"
111107
btnClass="btn-large deep-orange darken-1"
112-
action="undo"}}
108+
action=(action 'undo')}}
113109

114110
{{md-fixed-btn
115111
btnClass="blue-grey lighten-1"
116112
btnIcon="fa fa-floppy-o"
117-
action="commit"}}
113+
action=(action 'commit')}}
118114

119115
{{md-fixed-btn
120116
btnClass="yellow darken-1"
121117
btnIcon="fa fa-repeat all"
122-
action="redoAll"}}
118+
action=(action 'redoAll')}}
123119

124120
{{md-fixed-btn
125121
btnClass="amber lighten-1"
126122
btnIcon="fa fa-undo all"
127-
action="undoAll"}}
123+
action=(action 'undoAll')}}
128124

129125
{{md-fixed-btn
130126
btnClass="orange darken-1"
131127
btnIcon="fa fa-repeat"
132-
action="redo"}}
128+
action=(action 'redo')}}
133129
{{/md-fixed-btns}}

0 commit comments

Comments
 (0)