Skip to content

Instantly share code, notes, and snippets.

@jimmont

jimmont/snippets.js

Last active Oct 2, 2016
Embed
What would you like to do?
snippet collection
// work with angular since Batarang is giving me problems
(function(){
function $scope($0){ $0 = angular.element(
typeof($0) === 'string' ? document.querySelectorAll($0) : $0
).scope(); $scope.$0 = $0; return $scope; }
window.$scope = $scope;
console.log('window.$scope($0) returns scope for element $0',$scope(document));
})();
// $http for convenient http requests
(function(){
window.$http =
angular.element(document.body).injector()
.get('$http');
window.http = function(u){
if(typeof u === 'string') u = {url:u};
u = angular.extend({method:'GET',cache:false}, u);
console.log('$http',u);
u._res = function(data, status, headers, config){
console.log('http.res>stat/hdr/conf/data',status, headers, config, data);
};
return $http(u).success(u._res).error(u._res);
};
console.log("usage:"
+ "\nhttp('/a/relative/')"
+ "\nhttp({url: ..., other:stuff...});"
);
return 'window.http() and $http() ready to go...'
})()
/* mutationObserver
http://caniuse.com/#feat=mutationobserver
https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
*/
(function(){
window._mutation = {
observers: {}
,observe: function(sel,fn, options){
// select the target node
var target = sel.nodeType ? sel : document.querySelector(sel);
if(sel.nodeType){
sel = '-node' + Date.now();
}
target.setAttribute('observer',sel);
// create an observer instance
var observer = new MutationObserver(fn || function(mutation,observer) {
mutation.forEach(function(mutation,i) { console.log(i,mutation.type, mutation); });
});
// configuration of the observer:
/*
childList Set to true if additions and removals of the target node's child elements (including text nodes) are to be observed.
attributes Set to true if mutations to target's attributes are to be observed.
characterData Set to true if mutations to target's data are to be observed.
subtree Set to true if mutations to target and target's descendants are to be observed.
attributeOldValue Set to true if attributes is set to true and target's attribute value before the mutation needs to be recorded.
characterDataOldValue Set to true if characterData is set to true and target's data before the mutation needs to be recorded.
attributeFilter Set to an array of attribute local names (without namespace) if not all attribute mutations need to be observed.
*/
var config = Object.assign({ attributes: true, childList: true, characterData: true }, options);
// pass in the target node, as well as the observer options
console.log('observe',target,'('+sel+') with config ',config);
observer.observe(target, config);
this.observers[sel] = observer;
// later, you can stop observing
// observer.disconnect();
}
,stop:function(sel){
if(sel.nodeType) sel = sel.getAttribute('observer');
sel = this.observers[sel];
if(!sel) return;
sel.disconnect();
this.observers[sel] = false;
}
};
console.log('setup: _mutation.observe(DOMNode | "css-selector", optional-handler-function, optional-config-settings);');
console.log('stop: _mutation.stop(DOMNode | "css-selector");');
console.log('like this: var b=$0;_mutation.observe($0,function(){ console.log(b.className,getComputedStyle(b.querySelector(".item")).top); }, {subtree: true});');
})();
// globals
/*
log-globals
by Sindre Sorhus
https://github.com/sindresorhus/log-globals
MIT License
*/
(function () {
'use strict';
function getIframe() {
var el = document.createElement('iframe');
el.style.display = 'none';
document.body.appendChild(el);
var win = el.contentWindow;
document.body.removeChild(el);
return win;
}
function detectGlobals() {
var iframe = getIframe();
var ret = Object.create(null);
for (var prop in window) {
if (!(prop in iframe)) {
ret[prop] = window[prop];
}
}
return ret;
}
console.log(detectGlobals());
})();
// hammerTime
(function(){
/* https://github.com/EightMedia/hammer.js/wiki/Getting-Started
var hammertime = Hammer(element).on("tap", function(event) {
alert('hello!');
});
var hammertime = Hammer(element, {
drag: false,
transform: false
});
hold
tap
doubletap
drag, dragstart, dragend, dragup, dragdown, dragleft, dragright
swipe, swipeup, swipedown, swipeleft, swiperight
transform, transformstart, transformend
rotate
pinch, pinchin, pinchout
touch
release
gesture
*/
window.spy=function(e){
console.log(e.type,e.target,e);
if(e.type === 'gesture') e.gesture.preventDefault();
}
window.hmDoc = Hammer(document);
var et, list = 'tap,gesture,pinch'.split(',');
while(et = list.shift()) hmDoc.on(et,spy);
//hmDoc.on('gesture',stop);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.