Last active
December 17, 2015 14:19
-
-
Save justindarc/5623192 to your computer and use it in GitHub Desktop.
AngularJS Directive for Snap.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('angular-snap', []) | |
.directive('snap', function() { | |
var parseBoolean = function(attrValue, defaultValue) { | |
var strValue = '' + attrValue; | |
if (strValue === 'true' || | |
strValue === 'yes' || | |
strValue === '1') return true; | |
if (strValue === 'false' || | |
strValue === 'no' || | |
strValue === '0') return false; | |
return (defaultValue || false); | |
}; | |
return { | |
restrict: 'A', | |
require: '?ngModel', | |
priority: 0, | |
scope: true, | |
compile: function(element, attrs, b, c) { | |
return function(scope, element, attrs, controller) { | |
var disable = attrs.snapDisable || 'none'; | |
var resistance = window.parseFloat(attrs.snapResistance || '0.5'); | |
var flickThreshold = window.parseInt(attrs.snapFlickThreshold || '50', 10); | |
var transitionSpeed = window.parseFloat(attrs.snapTransitionSpeed || '0.3'); | |
var easing = attrs.snapEasing || 'ease'; | |
var maxPosition = window.parseInt(attrs.snapMaxPosition || '266', 10); | |
var minPosition = window.parseInt(attrs.snapMinPosition || '-266', 10); | |
var tapToClose = parseBoolean(attrs.snapTapToClose, true); | |
var touchToDrag = parseBoolean(attrs.snapTouchToDrag, true); | |
var slideIntent = window.parseInt(attrs.snapSlideIntent || '40', 10); | |
var minDragDistance = window.parseInt(attrs.snapMinDragDistance || '5', 10); | |
var dragWithMouse = parseBoolean(attrs.snapDragWithMouse, true); | |
var isTouchSupported = !!('ontouchstart' in window); | |
if (!isTouchSupported && !dragWithMouse) touchToDrag = false; | |
var snap = element[0].snap = new Snap({ | |
element: element[0], | |
disable: disable, | |
resistance: resistance, | |
flickThreshold: flickThreshold, | |
transitionSpeed: transitionSpeed, | |
easing: easing, | |
maxPosition: maxPosition, | |
minPosition: minPosition, | |
tapToClose: tapToClose, | |
touchToDrag: touchToDrag, | |
slideIntent: slideIntent, | |
minDragDistance: minDragDistance | |
}); | |
if (controller) { | |
snap.on('animated', function() { | |
if (document.body.className.indexOf('snapjs-left') > -1) { | |
scope.$apply(function() { | |
controller.$setViewValue('left'); | |
}); | |
} | |
else if (document.body.className.indexOf('snapjs-right') > -1) { | |
scope.$apply(function() { | |
controller.$setViewValue('right'); | |
}); | |
} | |
else { | |
scope.$apply(function() { | |
controller.$setViewValue(undefined); | |
}); | |
} | |
}); | |
scope.$watch(attrs.ngModel, function(newValue, oldValue) { | |
switch (newValue) { | |
case 'left': | |
snap.open('left'); | |
break; | |
case 'right': | |
snap.open('right'); | |
break; | |
default: | |
snap.close(); | |
break; | |
} | |
}); | |
} | |
}; | |
} | |
}; | |
}) | |
.directive('snapOpen', function() { | |
return { | |
restrict: 'A', | |
priority: 0, | |
compile: function(element, attrs, b, c) { | |
return function(scope, element, attrs, controller) { | |
element.on('click', function(evt) { | |
var snapElement = $('[snap]')[0]; | |
if (!snapElement) return; | |
var snap = snapElement.snap; | |
if (!snap) return; | |
snap.open(attrs.snapOpen); | |
}); | |
}; | |
} | |
}; | |
}); |
Can you point me to a working example snap.js and your angular-snap? Thank You
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Special thanks to @jakiestfu for the super-awesome Snap.js project!! This little AngularJS directive should make integrating it into Angular projects a breeze!!