Skip to content

Commit

Permalink
Added initial test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wildhoney committed Sep 7, 2014
1 parent 20537c9 commit 4154ecd
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 231 deletions.
21 changes: 19 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ module.exports = function(grunt) {
dest: 'releases/master.zip'
}

},

/**
* @property jasmine
* @type {Object}
*/
jasmine: {
pivotal: {
src: ['module/*.js'],
options: {
specs: 'tests/spec.js',
helpers: [
'example/js/vendor/angular/angular.js',
'example/js/vendor/angular-mocks/angular-mocks.js'
]
}
}
}

});
Expand All @@ -91,7 +108,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-copy');

grunt.registerTask('build', ['concat', 'uglify', 'copy', 'compress']);
grunt.registerTask('test', ['jshint']);
grunt.registerTask('default', ['jshint', 'concat', 'copy', 'uglify', 'compress']);
grunt.registerTask('test', ['jshint', 'jasmine']);
grunt.registerTask('default', ['test', 'build']);

};
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ Sometimes you may wish to invoke this behaviour yourself, in which case you need

## Exceptions

Thrown when the specified context menu path cannot be found:

> Invalid context menu path: *templateName*.
Thrown when the specified context menu path cannot be found.
> Context menu is adding *number* immediate children.
Thrown when the partial is adding more than one child – when **only** one is expected.
Thrown when the partial is adding more than one child – when **only** one is expected:

> Context menu is adding *number* immediate children.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-contextmenu",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "https://github.com/Wildhoney/ngContextMenu",
"authors": [
"Adam Timberlake <[email protected]>"
Expand Down
224 changes: 112 additions & 112 deletions dist/ng-contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,163 +54,163 @@
*/
module.directive('contextMenu', ['$window', '$http', '$interpolate', '$compile', '$templateCache', 'contextMenu',

function contextMenuDirective($window, $http, $interpolate, $compile, $templateCache, contextMenu) {

return {
function contextMenuDirective($window, $http, $interpolate, $compile, $templateCache, contextMenu) {

return {

/**
* @property restrict
* @type {String}
*/
restrict: 'EA',

/**
* @property require
* @type {String}
*/
require: 'ngModel',

/**
* @property scope
* @type {Object}
*/
scope: {
include: '@contextMenu',
model: '=ngModel'
},

/**
* @method controller
* @param $scope {Object}
* @return {void}
*/
controller: ['$scope', function controller($scope) {

/**
* @property restrict
* @property template
* @type {String}
*/
restrict: 'EA',
$scope.template = '';

/**
* @property require
* @type {String}
* @property menu
* @type {Object|null}
*/
require: 'ngModel',
$scope.menu = null;

/**
* @property scope
* @type {Object}
* @method throwException
* @throw Exception
* @param message {String}
* @return {void}
*/
scope: {
include: '@contextMenu',
model: '=ngModel'
},
$scope.throwException = function throwException(message) {
throw "ngContextMenu: " + message + ".";
};

/**
* @method controller
* @param $scope {Object}
* @method cacheTemplate
* @param templatePath {String}
* @param model {Object}
* @return {void}
*/
controller: ['$scope', function controller($scope) {

/**
* @property template
* @type {String}
*/
$scope.template = '';

/**
* @property menu
* @type {Object|null}
*/
$scope.menu = null;

/**
* @method throwException
* @throw Exception
* @param message {String}
* @return {void}
*/
$scope.throwException = function throwException(message) {
throw "ngContextMenu: " + message + ".";
};
$scope.cacheTemplate = function cacheTemplate(templatePath, model) {

/**
* @method cacheTemplate
* @param templatePath {String}
* @param model {Object}
* @return {void}
*/
$scope.cacheTemplate = function cacheTemplate(templatePath, model) {
$http.get(templatePath, { cache: $templateCache }).then(function then(response) {

$http.get(templatePath, { cache: $templateCache }).then(function then(response) {
// Interpolate the supplied template with the scope.
$scope.template = $interpolate(response.data)(model);

// Interpolate the supplied template with the scope.
$scope.template = $interpolate(response.data)(model);
}).catch(function catchError() {

}).catch(function catchError() {
// Unable to find the supplied template path.
$scope.throwException('Invalid context menu path: "' + templatePath + '"');

// Unable to find the supplied template path.
$scope.throwException('Invalid context menu path: "' + templatePath + '"');

});

};

/**
* @method cancel
* @return {void}
*/
$scope.cancel = function cancel() {

if ($scope.menu) {
$scope.menu.remove();
}

};
});

}],
};

/**
* @method link
* @param scope {Object}
* @param element {Object}
* @method cancel
* @return {void}
*/
link: function link(scope, element) {
$scope.cancel = function cancel() {

if (!contextMenu.attachedClick) {
if ($scope.menu) {
$scope.menu.remove();
}

// Subscribe to the onClick event of the BODY node to remove any context menus
// that may be open.
var body = $angular.element($window.document.getElementsByTagName('html'));
contextMenu.attachedClick = true;
};

body.bind('click', function onClick() {
}],

// Remove all of the open context menus.
scope.$apply(contextMenu.cancelAll);
/**
* @method link
* @param scope {Object}
* @param element {Object}
* @return {void}
*/
link: function link(scope, element) {

});
if (!contextMenu.attachedClick) {

}
// Subscribe to the onClick event of the BODY node to remove any context menus
// that may be open.
var body = $angular.element($window.document.getElementsByTagName('html'));
contextMenu.attachedClick = true;

// Evaluate the supplied template against the model.
scope.cacheTemplate(scope.include, scope.model);
body.bind('click', function onClick() {

// Listen for any attempts to cancel the current context menu.
scope.$watch(function setupObserver() {
return contextMenu.cancelIteration;
}, scope.cancel);
// Remove all of the open context menus.
scope.$apply(contextMenu.cancelAll);

element.bind('contextmenu', function onContextMenu(event) {
});

scope.$apply(function apply() {
}

// Remove any existing context menus for this element and other elements.
contextMenu.cancelAll();
// Evaluate the supplied template against the model.
scope.cacheTemplate(scope.include, scope.model);

});
// Listen for any attempts to cancel the current context menu.
scope.$watch(function setupObserver() {
return contextMenu.cancelIteration;
}, scope.cancel);

// Prevent the default context menu from opening, and make the user
// defined context menu appear instead.
event.preventDefault();
var compiledTemplate = $compile(scope.template)(scope.model);
element.bind('contextmenu', function onContextMenu(event) {

// Ensure the compiled template is only adding one child.
if (compiledTemplate.length > 1) {
scope.throwException('Context menu is adding ' + compiledTemplate.length + ' immediate children');
}
scope.$apply(function apply() {

element.append(compiledTemplate);
// Remove any existing context menus for this element and other elements.
contextMenu.cancelAll();

// Keep a track of the added context menu for removing it if necessary.
var nativeElement = element[0],
childCount = nativeElement.childNodes.length;
});

// Update the position of the newly added context menu.
scope.menu = $angular.element(nativeElement.childNodes[childCount - 1]);
scope.menu.css({ top: event.clientY + 'px', left: event.clientX + 'px' });
// Prevent the default context menu from opening, and make the user
// defined context menu appear instead.
event.preventDefault();
var compiledTemplate = $compile(scope.template)(scope.model);

});
// Ensure the compiled template is only adding one child.
if (compiledTemplate.length > 1) {
scope.throwException('Context menu is adding ' + compiledTemplate.length + ' immediate children');
}

}
element.append(compiledTemplate);

// Keep a track of the added context menu for removing it if necessary.
var nativeElement = element[0],
childCount = nativeElement.childNodes.length;

// Update the position of the newly added context menu.
scope.menu = $angular.element(nativeElement.childNodes[childCount - 1]);
scope.menu.css({ top: event.clientY + 'px', left: event.clientX + 'px' });

});

}

}]);
}

}]);

})(window.angular);
Loading

0 comments on commit 4154ecd

Please sign in to comment.