Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Added promises that are resolved when the menu is closed. #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ controller('MyCtrl', function (myOffCanvas) {
</div>
```

### Promises

When the menu is opened with toggle(), it will return a promise that is resolved when the menu is closed.

```javascript
myOffCanvas.toggle()
.then(function() {
console.log('The menu has been closed!');
});

myOffCanvas.toggle(); // The menu will close and display the message.
```

If the menu is already opened, then the promise will be immediately rejected.

```javascript
myOffCanvas.toggle(); // The menu will open and nothing will be displayed in the console.
myOffCanvas.toggle()
.then(function() {
console.log('The menu has been closed!');
}, function() {
console.log('The menu was already opened.');
}); // The menu will close and the message "The menu was already open" will be displayed immediately.
```


## API

Expand Down Expand Up @@ -136,7 +161,8 @@ A `navService` has just two methods: `activate` and `deactivate`.

#### `navService.toggle`

Add or remove a class to open/hide the nav with CSS
Add or remove a class to open/hide the nav with CSS.
Returns a promise that will be resolved when the off canvas menu closes, or is immediately rejected if the toggle closes an already opened menu.

## Tests

Expand Down
32 changes: 29 additions & 3 deletions off-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ angular.module('cn.offCanvas', [])
controller = config.controller || angular.noop,
controllerAs = config.controllerAs,
element = null,
promise = null,
html;

if(config.template) {
Expand All @@ -45,13 +46,38 @@ angular.module('cn.offCanvas', [])
})

function toggle() {
this.isOpened = !this.isOpened;
container.toggleClass(containerClass);
if (this.isOpened) {
return this.close();
} else {
return this.open();
}
}

function open() {
if (!this.isOpened) {
this.isOpened = true;
container.toggleClass(containerClass);
}
promise = $q.defer();
return promise.promise;
}

function close() {
if (this.isOpened) {
this.isOpened = false;
container.toggleClass(containerClass);
}
promise.resolve();
var defered = $q.defer();
defered.reject();
return defered.promise;
}

return {
toggle: toggle,
open: open,
close: close,
isOpened: false
}
}
});
});
2 changes: 1 addition & 1 deletion off-canvas.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions off-canvas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,44 @@ describe('cnOffCanvas', function() {
expect(container.hasClass('is-opened-nav')).toBeTruthy();
});
});

describe('promises', function() {
var offCanvas;
beforeEach(function() {
offCanvas = cnOffCanvas({
templateUrl: 'test.html',
container: container
});
});
Copy link
Owner

@cironunes cironunes Aug 25, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a line break here

it('should be created on toggle', function() {
var promise = offCanvas.toggle();
expect(promise).toBeDefined();
promise = offCanvas.toggle();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions are duplicated. Besides that, it doesn't really exercise the code to see if there's a promise here. You can use the $q service to help.

expect(promise).toBeDefined();
});

it('should be resolved after closed', inject(function($rootScope) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of injecting $rootScope again, you can just use rootScope

var resolved = false;
offCanvas.toggle()
.then(function() {
resolved = true;
});
expect(resolved).toBe(false);
offCanvas.toggle();
$rootScope.$apply();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rootScope.$apply()

expect(resolved).toBe(true);
}));

it('should be rejected if the toggle closes', inject(function($rootScope) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use rootScope

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please change to: 'it should be rejected if the toggle closes the nav'

offCanvas.toggle();
var result = "nothing";
offCanvas.toggle()
.then(function() {
resolved = "success";
}, function() {
resolved = "rejected";
});
expect(resolved).toBe("rejected");
}));
});
});