Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active November 20, 2015 17:43
Show Gist options
  • Select an option

  • Save typeoneerror/2f00ec956769747bcf31 to your computer and use it in GitHub Desktop.

Select an option

Save typeoneerror/2f00ec956769747bcf31 to your computer and use it in GitHub Desktop.
sidebar toggler with ember
.sidebar-animated {
nav {
@for $n from 1 through 6 {
> li:nth-child(#{$n}) {
.nav-label, > a > .ss-icon { transition-delay: #{$n*60}ms; }
}
}
}
}
export function initialize(container, application) {
console.log('LocalStorageService::initialize');
application.inject('route', 'localStorage', 'service:local-storage');
application.inject('controller', 'localStorage', 'service:local-storage');
application.inject('component', 'localStorage', 'service:local-storage');
application.inject('view', 'localStorage', 'service:local-storage');
}
export default {
name: 'local-storage-service',
initialize: initialize,
after: 'const'
};
import Ember from 'ember';
export default Ember.Object.extend({
isAvailable: function() {
return Modernizr.localstorage;
},
get: function(property) {
return localStorage.getItem(property);
},
set: function(property, value) {
localStorage.setItem(property, value);
},
delete: function(property) {
localStorage.removeItem(property);
}
});
<a {{action "toggleNavState"}}>
<span {{bind-attr class=":fa :fa-fw directionalIcon"}}></span>
</a>
// components/nav-toggler.js
import Ember from 'ember';
import computed from 'doki/utils/computed';
export default Ember.Component.extend({
navState: 0, // 0=minimized, 1=expanded
isExpanded: Ember.computed.bool('navState'),
isMinimized: Ember.computed.not('isExpanded'),
directionalIcon: computed.ternary('isMinimized', 'fa-chevron-right', 'fa-chevron-left'),
tagName: 'li',
classNames: ['nav-toggler'],
toggledState: function() {
var navState = this.get('navState');
$('body').toggleClass('sidebar-minimized', this.get('isMinimized'));
$('body').toggleClass('sidebar-expanded', this.get('isExpanded'));
if (this.localStorage.isAvailable()) {
this.localStorage.set('navState', navState);
}
}.observes('navState').on('didInsertElement'),
init: function() {
this._super();
if (this.localStorage.isAvailable()) {
var navState = parseInt(this.localStorage.get('navState') || 0);
this.set('navState', navState);
}
},
didInsertElement: function() {
Ember.run.scheduleOnce('afterRender', this, this.didRenderElement);
},
didRenderElement: function() {
Ember.run.later(this, function() {
$('body').addClass('sidebar-animated');
})
},
willRemoveElement: function() {
$('body').removeClass('sidebar-animated');
},
actions: {
toggleNavState: function() {
var navState = Math.abs(this.get('navState') - 1);
this.set('navState', navState);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment