This repository has been archived by the owner on Jul 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
off-canvas.js
56 lines (49 loc) · 1.45 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
/*
* 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) {
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, deferred, scope, ctrl;
if ((+!!config.template) + (+!!config.templateUrl) !== 1) {
throw new Error('You must specify either a `template` or a `templateUrl` to create an off-canvas navigation.');
}
if (config.template) {
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) {
scope = $rootScope.$new();
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
}
}
});