Last active
November 27, 2019 22:29
-
-
Save housker/700c55cb6d5cce425afe189e5d0ab72a to your computer and use it in GitHub Desktop.
New Twiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
import { A } from '@ember/array'; | |
import EmberObject, { computed } from '@ember/object'; | |
export default Ember.Controller.extend({ | |
displayData: Ember.computed('[email protected]', function() { | |
return this.data.sort((a, b) => a.priority - b.priority); | |
}), | |
data: A([ | |
EmberObject.create({ | |
priority: 1, | |
name: 'Stuff', | |
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' | |
}), | |
EmberObject.create({ | |
priority: 2, | |
name: 'Thing', | |
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' | |
}), | |
EmberObject.create({ | |
priority: 3, | |
name: 'Object', | |
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' | |
}), | |
EmberObject.create({ | |
priority: 4, | |
name: 'Item', | |
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' | |
}), | |
EmberObject.create({ | |
priority: 5, | |
name: 'Blob', | |
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.' | |
}) | |
]), | |
actions: { | |
updatePriority(index, event) { | |
const begVal = parseInt(this.data[index].priority); | |
let endVal = parseInt(event.target.value) || begVal; | |
if(endVal > this.data.length) { | |
endVal = this.data.length; | |
} | |
if(endVal <= 0) { | |
endVal = 1; | |
} | |
const delta = endVal - begVal; | |
if(delta > 0) { | |
this.data.forEach((obj, i) => { | |
if(obj.priority > begVal && obj.priority <= endVal) { | |
this.data[i].set('priority', parseInt(obj.priority) - 1); | |
} | |
}); | |
} else if(delta < 0) { | |
this.data.forEach((obj, i) => { | |
if(obj.priority >= endVal && obj.priority < begVal) { | |
this.data[i].set('priority', parseInt(obj.priority) + 1); | |
} | |
}); | |
} | |
if(endVal == begVal) { | |
// reset if input not valid, i.e. letters, negative number, etc. | |
// could also do this, but is a code smell: | |
// this.data[index].notifyPropertyChange('priority'); | |
event.target.value = endVal; | |
} else { | |
this.data[index].set('priority', endVal); | |
} | |
} | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 12px 16px; | |
font-family: Courier; | |
font-size: 12pt; | |
} | |
.container { | |
display: grid; | |
grid-template-columns: 80px 100px auto; | |
} | |
.container > * { | |
margin: 10px; | |
align-self: center; | |
color: #808080; | |
} | |
.priority { | |
text-align: center; | |
align-self: center; | |
border: none; | |
} | |
.name { | |
justify-self: center; | |
align-self: center; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment