-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathember-responsive-area.js
72 lines (55 loc) · 2.22 KB
/
ember-responsive-area.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ==========================================================================
// Project: Ember Responsive Area
// Copyright: Copyright 2014 Diogo Edegar Mafra
// License: Licensed under MIT license
// ==========================================================================
(function() {
var EmberResponsiveArea = window.EmberResponsiveArea = {};
// This mixin can be used in a View or Component.
// It will render only if the element is visible.
EmberResponsiveArea.Mixin = Ember.Mixin.create({
_canRender: false,
render: function() {
if (this.get('_canRender')) {
return this._super.apply(this, arguments);
}
},
_initialVisibilityCheck: (function() {
// This method is called again after `rerender()`, this avoids an infinite recursion
if (this.get('_canRender')) {
return;
}
// Make the initial check
this._checkVisibility();
// If it's not already visible, setup the listener
if (!this.get('_canRender')) {
return this._registerListener();
}
}).on('didInsertElement'),
_checkVisibility: function(dontRerender) {
if (this.$().is(':visible')) {
// When it becomes visible, remove the listener and rerender
this.set('_canRender', true);
this._removeListener();
return this.rerender();
}
},
_registerListener: function() {
var that = this;
// Check the visibility when the window is resized (once per run-loop)
return $(window).on('resize.responsive-area', function() {
return Ember.run.once(that, '_checkVisibility');
});
},
_removeListener: (function() {
return $(window).off('resize.responsive-area');
}).on('willDestroyElement')
});
EmberResponsiveArea.Component = Ember.Component.extend(EmberResponsiveArea.Mixin);
// This helper can be used in areas that are invisible depending on the size of the screen.
// It will render the template only if the element is visible.
// Example: {{#responsive-area}}My content{{/responsive-area}}
Ember.Handlebars.registerHelper('responsive-area', function(options) {
return Ember.Handlebars.helpers.view.call(this, EmberResponsiveArea.Component, options);
});
}).call(this);