forked from cironunes/angular-off-canvas
/
off-canvas.js
57 lines (49 loc) · 1.35 KB
/
off-canvas.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
/*
* angular-off-canvas v0.1.0
* (c) 2013 Ciro Nunes http://cironunes.github.io/
* License: MIT
*/
'use strict';
angular.module('cn.offCanvas', [])
.factory('cnOffCanvas', function($compile, $rootScope, $controller, $http, $templateCache, $q) {
return function (config) {
if((+!!config.template) + (+!!config.templateUrl) !== 1) {
throw new Error();
}
var container = angular.element(config.container || document.body),
containerClass = config.containerClass || 'is-off-canvas-opened',
controller = config.controller || angular.noop,
controllerAs = config.controllerAs,
element = null,
html;
if(config.template) {
var deferred = $q.defer();
deferred.resolve(config.template);
html = deferred.promise;
} else {
html = $http.get(config.templateUrl, {
cache: $templateCache
}).then(function(response) {
return response.data;
});
}
html.then(function(html) {
var scope = $rootScope.$new();
var ctrl = $controller(controller, {$scope: scope});
if(controllerAs) {
scope[controllerAs] = ctrl;
}
element = angular.element(html);
container.prepend(element);
$compile(element)(scope);
})
function toggle() {
this.isOpened = !this.isOpened;
container.toggleClass(containerClass);
}
return {
toggle: toggle,
isOpened: false
}
}
});