Last active
June 7, 2018 23:22
-
-
Save andybluntish/23b6923e1e3d5d8b7544f8bd3ee6b171 to your computer and use it in GitHub Desktop.
Accordion
This file contains 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'; | |
const { Controller } = Ember; | |
export default Controller.extend({ | |
title: 'Accordion', | |
actions: { | |
say(msg) { | |
console.log('working...') | |
return new Ember.RSVP.Promise((y, n) => window.setTimeout(y, 1000)) | |
.then(() => console.log(`Panel says: ${msg}`)) | |
}, | |
}, | |
}); |
This file contains 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'; | |
const { | |
A, | |
Component, | |
get, | |
isEqual, | |
set | |
} = Ember; | |
export default Component.extend({ | |
tagName: '', | |
items: null, | |
active: null, | |
initialOpenIndex: null, | |
init() { | |
this._super(...arguments) | |
set(this, 'items', A()) | |
}, | |
didInsertElement() { | |
const index = get(this, 'initialOpenIndex') | |
const itemKeys = get(this, 'items').mapBy('key') | |
const id = itemKeys.objectAt(index) | |
if (id) set(this, 'active', id) | |
}, | |
actions: { | |
register(item) { | |
const items = get(this, 'items') | |
items.addObject(item) | |
set(this, 'items', items.uniq()) | |
}, | |
deregister(item) { | |
const items = get(this, 'items') | |
items.removeObject(item) | |
set(this, 'items', items.uniq()) | |
}, | |
open(key) { | |
const items = get(this, 'items') | |
const oldItem = items.findBy('key', get(this, 'active')) | |
const newItem = items.findBy('key', key) | |
if (oldItem) oldItem.send('onClose') | |
set(this, 'active', key); | |
if (newItem) newItem.send('onOpen') | |
}, | |
close(key) { | |
const item = get(this, 'items').findBy('key', key) | |
if (item) item.send('onClose') | |
set(this, 'active', null); | |
}, | |
next(key) { | |
const itemKeys = get(this, 'items').mapBy('key') | |
let newIndex = itemKeys.indexOf(key) + 1 | |
if (newIndex > itemKeys.length - 1) newIndex = 0 | |
const nextKey = itemKeys.objectAt(newIndex) | |
this.send('open', nextKey) | |
}, | |
} | |
}); |
This file contains 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'; | |
const { | |
Component, | |
RSVP: { Promise }, | |
computed, | |
get, | |
guidFor, | |
isEqual | |
} = Ember; | |
export default Component.extend({ | |
tagName: '', | |
key: computed({ | |
get() { | |
return guidFor(this); | |
} | |
}), | |
_active: null, | |
_open: null, | |
_close: null, | |
_next: null, | |
beforeClose() {}, | |
afterClose() {}, | |
isActive: computed('_active', { | |
get() { | |
const key = get(this, 'key') | |
const active = get(this, '_active') | |
return isEqual(key, active); | |
}, | |
}).readOnly(), | |
didInsertElement() { | |
get(this, '_register')(this) | |
}, | |
willDestroyElement() { | |
get(this, '_deregister')(this) | |
}, | |
actions: { | |
open() { | |
const key = get(this, 'key') | |
const open = get(this, '_open') | |
open(key) | |
}, | |
onOpen() { | |
const callback = get(this, 'onOpen') | |
if (callback) callback() | |
}, | |
async close() { | |
const key = get(this, 'key') | |
const beforeClose = get(this, 'beforeClose') | |
const close = get(this, '_close') | |
const afterClose = get(this, 'afterClose') | |
await beforeClose() | |
close(key) | |
afterClose() | |
}, | |
onClose() { | |
const callback = get(this, 'onClose') | |
if (callback) callback() | |
}, | |
toggle() { | |
const key = get(this, 'key') | |
const isActive = get(this, 'isActive') | |
if (isActive) { | |
this.send('close') | |
} else { | |
this.send('open') | |
} | |
}, | |
async next() { | |
const key = get(this, 'key') | |
const beforeClose = get(this, 'beforeClose') | |
const next = get(this, '_next') | |
const afterClose = get(this, 'afterClose') | |
await beforeClose() | |
next(key) | |
afterClose() | |
}, | |
}, | |
}); |
This file contains 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.13.0", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.16.2", | |
"ember-template-compiler": "2.16.2", | |
"ember-testing": "2.16.2" | |
}, | |
"addons": { | |
"ember-data": "2.16.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment