|
|
|
@ -76,6 +76,9 @@ var inject = function () {
|
|
|
|
|
nu; // The new object or array
|
|
|
|
|
switch (typeof value) {
|
|
|
|
|
case 'object':
|
|
|
|
|
if (value instanceof HTMLElement) {
|
|
|
|
|
return value.innerHTML;
|
|
|
|
|
}
|
|
|
|
|
if (!value) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
@ -181,6 +184,73 @@ var inject = function () {
|
|
|
|
|
}());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// throttle based on _.throttle from Lo-Dash
|
|
|
|
|
// https://github.com/bestiejs/lodash/blob/master/lodash.js#L4625
|
|
|
|
|
var throttle = function (func, wait) {
|
|
|
|
|
var args,
|
|
|
|
|
result,
|
|
|
|
|
thisArg,
|
|
|
|
|
timeoutId,
|
|
|
|
|
lastCalled = 0;
|
|
|
|
|
|
|
|
|
|
function trailingCall() {
|
|
|
|
|
lastCalled = new Date();
|
|
|
|
|
timeoutId = null;
|
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
return function() {
|
|
|
|
|
var now = new Date(),
|
|
|
|
|
remaining = wait - (now - lastCalled);
|
|
|
|
|
|
|
|
|
|
args = arguments;
|
|
|
|
|
thisArg = this;
|
|
|
|
|
|
|
|
|
|
if (remaining <= 0) {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
timeoutId = null;
|
|
|
|
|
lastCalled = now;
|
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
else if (!timeoutId) {
|
|
|
|
|
timeoutId = setTimeout(trailingCall, remaining);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var debounce = function (func, wait, immediate) {
|
|
|
|
|
var args,
|
|
|
|
|
result,
|
|
|
|
|
thisArg,
|
|
|
|
|
timeoutId;
|
|
|
|
|
|
|
|
|
|
function delayed() {
|
|
|
|
|
timeoutId = null;
|
|
|
|
|
if (!immediate) {
|
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return function() {
|
|
|
|
|
var isImmediate = immediate && !timeoutId;
|
|
|
|
|
args = arguments;
|
|
|
|
|
thisArg = this;
|
|
|
|
|
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
timeoutId = setTimeout(delayed, wait);
|
|
|
|
|
|
|
|
|
|
if (isImmediate) {
|
|
|
|
|
result = func.apply(thisArg, args);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var updateScopeModelCache = function (scope) {
|
|
|
|
|
debug.models[scope.$id] = getScopeLocals(scope);
|
|
|
|
|
debug.scopeDirty[scope.$id] = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var popover = null;
|
|
|
|
|
|
|
|
|
|
// Public API
|
|
|
|
|
// ==========
|
|
|
|
@ -199,14 +269,21 @@ var inject = function () {
|
|
|
|
|
return ids;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// returns null or cached scope
|
|
|
|
|
getModel: function (id) {
|
|
|
|
|
if (debug.scopeDirty[id]) {
|
|
|
|
|
updateScopeModelCache(debug.scopes[id]);
|
|
|
|
|
return debug.models[id];
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getScopeTree: function (id) {
|
|
|
|
|
if (debug.rootScopeDirty[id] === false) {
|
|
|
|
|
if (debug.scopeTreeDirty[id] === false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var traverse = function (sc) {
|
|
|
|
|
var tree = {
|
|
|
|
|
id: sc.$id,
|
|
|
|
|
locals: debug.scopes[sc.$id],
|
|
|
|
|
children: []
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -224,9 +301,10 @@ var inject = function () {
|
|
|
|
|
var tree = traverse(root);
|
|
|
|
|
|
|
|
|
|
if (tree) {
|
|
|
|
|
debug.rootScopeDirty[id] = false;
|
|
|
|
|
debug.scopeTreeDirty[id] = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@ -266,6 +344,241 @@ var inject = function () {
|
|
|
|
|
var tree = traverse(root);
|
|
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
enable: function () {
|
|
|
|
|
if (popover) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var angular = window.angular;
|
|
|
|
|
popover = angular.element(
|
|
|
|
|
'<div style="position: fixed; left: 50px; top: 50px; z-index: 9999; background-color: #f1f1f1; box-shadow: 0 15px 10px -10px rgba(0, 0, 0, 0.5), 0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.1) inset;">' +
|
|
|
|
|
'<div style="position: relative" style="min-width: 300px; min-height: 100px;">' +
|
|
|
|
|
'<div style="min-width: 100px; min-height: 50px; padding: 5px;"><pre>{ Please select a scope }</pre></div>' +
|
|
|
|
|
'<button style="position: absolute; top: -15px; left: -15px; cursor: move;">⇱</button>' +
|
|
|
|
|
'<button style="position: absolute; top: -15px; left: 10px;">+</button>' +
|
|
|
|
|
'<button style="position: absolute; top: -15px; right: -15px;">x</button>' +
|
|
|
|
|
'<style>' +
|
|
|
|
|
'.ng-scope.batarang-selected { border: 1px solid red; } ' +
|
|
|
|
|
'.bat-indent { margin-left: 20px; }' +
|
|
|
|
|
'</style>' +
|
|
|
|
|
'</div>' +
|
|
|
|
|
'</div>');
|
|
|
|
|
angular.element(window.document.body).append(popover);
|
|
|
|
|
var popoverContent = angular.element(angular.element(popover.children('div')[0]).children()[0]);
|
|
|
|
|
var dragElt = angular.element(angular.element(popover.children('div')[0]).children()[1]);
|
|
|
|
|
var selectElt = angular.element(angular.element(popover.children('div')[0]).children()[2]);
|
|
|
|
|
var closeElt = angular.element(angular.element(popover.children('div')[0]).children()[3]);
|
|
|
|
|
|
|
|
|
|
console.log(closeElt);
|
|
|
|
|
|
|
|
|
|
var currentScope = null,
|
|
|
|
|
currentElt = null;
|
|
|
|
|
|
|
|
|
|
function onMove (ev) {
|
|
|
|
|
var x = ev.clientX,
|
|
|
|
|
y = ev.clientY;
|
|
|
|
|
|
|
|
|
|
if (x > window.outerWidth - 100) {
|
|
|
|
|
x = window.outerWidth - 100;
|
|
|
|
|
} else if (x < 0) {
|
|
|
|
|
x = 0;
|
|
|
|
|
}
|
|
|
|
|
if (y > window.outerHeight - 100) {
|
|
|
|
|
y = window.outerHeight - 100;
|
|
|
|
|
} else if (y < 0) {
|
|
|
|
|
y = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x += 5;
|
|
|
|
|
y += 5;
|
|
|
|
|
|
|
|
|
|
popover.css('left', x + 'px');
|
|
|
|
|
popover.css('top', y + 'px');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeElt.bind('click', function () {
|
|
|
|
|
popover.remove();
|
|
|
|
|
popover = null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
selectElt.bind('click', bindSelectScope);
|
|
|
|
|
|
|
|
|
|
var selecting = false;
|
|
|
|
|
function bindSelectScope () {
|
|
|
|
|
if (selecting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
selecting = true;
|
|
|
|
|
selectElt.attr('disabled', true);
|
|
|
|
|
angular.element(document.body).css('cursor', 'crosshair');
|
|
|
|
|
angular.element(document.getElementsByClassName('ng-scope'))
|
|
|
|
|
.bind('click', onSelectScope)
|
|
|
|
|
.bind('mouseover', onHoverScope);
|
|
|
|
|
}, 30);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hoverScopeElt = null;
|
|
|
|
|
|
|
|
|
|
function markHoverElt () {
|
|
|
|
|
if (hoverScopeElt) {
|
|
|
|
|
hoverScopeElt.addClass('batarang-selected');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
function unmarkHoverElt () {
|
|
|
|
|
if (hoverScopeElt) {
|
|
|
|
|
hoverScopeElt.removeClass('batarang-selected');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onSelectScope (ev) {
|
|
|
|
|
render(this);
|
|
|
|
|
angular.element(document.getElementsByClassName('ng-scope'))
|
|
|
|
|
.unbind('click', onSelectScope)
|
|
|
|
|
.unbind('mouseover', onHoverScope);
|
|
|
|
|
unmarkHoverElt();
|
|
|
|
|
selecting = false;
|
|
|
|
|
selectElt.attr('disabled', false);
|
|
|
|
|
angular.element(document.body).css('cursor', '');
|
|
|
|
|
hovering = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hovering = false;
|
|
|
|
|
function onHoverScope (ev) {
|
|
|
|
|
if (hovering) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
hovering = true;
|
|
|
|
|
var that = this;
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
unmarkHoverElt();
|
|
|
|
|
hoverScopeElt = angular.element(that);
|
|
|
|
|
markHoverElt();
|
|
|
|
|
hovering = false;
|
|
|
|
|
render(that);
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onUnhoverScope (ev) {
|
|
|
|
|
angular.element(this).css('border', '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dragElt.bind('mousedown', function (ev) {
|
|
|
|
|
ev.preventDefault();
|
|
|
|
|
rendering = true;
|
|
|
|
|
angular.element(document).bind('mousemove', onMove);
|
|
|
|
|
});
|
|
|
|
|
angular.element(document).bind('mouseup', function () {
|
|
|
|
|
angular.element(document).unbind('mousemove', onMove);
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
rendering = false;
|
|
|
|
|
}, 120);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function renderTree (data) {
|
|
|
|
|
var tree = angular.element('<div class="bat-indent"></div>');
|
|
|
|
|
angular.forEach(data, function (val, key) {
|
|
|
|
|
var toAppend;
|
|
|
|
|
if (val === undefined) {
|
|
|
|
|
toAppend = '<i>undefined</i>';
|
|
|
|
|
} else if (val === null) {
|
|
|
|
|
toAppend = '<i>null</i>';
|
|
|
|
|
} else if (val instanceof Array) {
|
|
|
|
|
toAppend = '[ ... ]';
|
|
|
|
|
} else if (val instanceof Object) {
|
|
|
|
|
toAppend = '{ ... }';
|
|
|
|
|
} else {
|
|
|
|
|
toAppend = val.toString();
|
|
|
|
|
}
|
|
|
|
|
if (data instanceof Array) {
|
|
|
|
|
toAppend = '<div>' +
|
|
|
|
|
toAppend +
|
|
|
|
|
((key===data.length-1)?'':',') +
|
|
|
|
|
'</div>';
|
|
|
|
|
} else {
|
|
|
|
|
toAppend = '<div>' +
|
|
|
|
|
key +
|
|
|
|
|
': ' +
|
|
|
|
|
toAppend +
|
|
|
|
|
(key!==0?'':',') +
|
|
|
|
|
'</div>';
|
|
|
|
|
}
|
|
|
|
|
toAppend = angular.element(toAppend);
|
|
|
|
|
if (val instanceof Array || val instanceof Object) {
|
|
|
|
|
function recur () {
|
|
|
|
|
toAppend.unbind('click', recur);
|
|
|
|
|
toAppend.html('');
|
|
|
|
|
toAppend
|
|
|
|
|
.append(angular.element('<span>' +
|
|
|
|
|
key + ': ' +
|
|
|
|
|
((val instanceof Array)?'[':'{') +
|
|
|
|
|
'<span>').bind('click', collapse))
|
|
|
|
|
.append(renderTree(val))
|
|
|
|
|
.append('<span>' + ((val instanceof Array)?']':'}') + '<span>');
|
|
|
|
|
}
|
|
|
|
|
function collapse () {
|
|
|
|
|
toAppend.html('');
|
|
|
|
|
toAppend.append(angular.element('<div>' +
|
|
|
|
|
key +
|
|
|
|
|
': ' +
|
|
|
|
|
((val instanceof Array)?'[ ... ]':'{ ... }') +
|
|
|
|
|
'</div>').bind('click', recur));
|
|
|
|
|
}
|
|
|
|
|
toAppend.bind('click', recur);
|
|
|
|
|
}
|
|
|
|
|
tree.append(toAppend);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isEmpty = function (object) {
|
|
|
|
|
var prop;
|
|
|
|
|
for (prop in object) {
|
|
|
|
|
if (object.hasOwnProperty(prop)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var objLength = function (object) {
|
|
|
|
|
var prop, len = 0;
|
|
|
|
|
for (prop in object) {
|
|
|
|
|
if (object.hasOwnProperty(prop)) {
|
|
|
|
|
len += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var rendering = false;
|
|
|
|
|
var render = function (elt) {
|
|
|
|
|
if (rendering) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rendering = true;
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
var scope = angular.element(elt).scope();
|
|
|
|
|
rendering = false;
|
|
|
|
|
if (scope === currentScope) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
currentScope = scope;
|
|
|
|
|
currentElt = elt;
|
|
|
|
|
|
|
|
|
|
var models = getScopeLocals(scope);
|
|
|
|
|
popoverContent.children().remove();
|
|
|
|
|
if (isEmpty(models)) {
|
|
|
|
|
popoverContent.append(angular.element('<i>This scope has no models</i>'));
|
|
|
|
|
} else {
|
|
|
|
|
popoverContent.append(renderTree(models));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
@ -282,14 +595,16 @@ var inject = function () {
|
|
|
|
|
watchPerf: {},
|
|
|
|
|
applyPerf: {},
|
|
|
|
|
|
|
|
|
|
// map of scope.$ids --> model objects
|
|
|
|
|
// map of scope.$ids --> $scope objects
|
|
|
|
|
scopes: {},
|
|
|
|
|
// map of scope.$ids --> model objects
|
|
|
|
|
models: {},
|
|
|
|
|
// map of $ids --> bools
|
|
|
|
|
scopeDirty: {},
|
|
|
|
|
|
|
|
|
|
// map of $ids --> refs to root scopes
|
|
|
|
|
// map of $ids --> refs to $rootScope objects
|
|
|
|
|
rootScopes: {},
|
|
|
|
|
|
|
|
|
|
// map of $ids --> bools
|
|
|
|
|
rootScopeDirty: {},
|
|
|
|
|
scopeTreeDirty: {},
|
|
|
|
|
|
|
|
|
|
deps: []
|
|
|
|
|
};
|
|
|
|
@ -363,7 +678,7 @@ var inject = function () {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$provide.decorator('$rootScope', function ($delegate) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var watchFnToHumanReadableString = function (fn) {
|
|
|
|
|
if (fn.exp) {
|
|
|
|
|
return fn.exp.trim();
|
|
|
|
@ -398,7 +713,7 @@ var inject = function () {
|
|
|
|
|
$delegate.__proto__.$watch = function (watchExpression, applyFunction) {
|
|
|
|
|
var thatScope = this;
|
|
|
|
|
var watchStr = watchFnToHumanReadableString(watchExpression);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!debug.watchPerf[watchStr]) {
|
|
|
|
|
debug.watchPerf[watchStr] = {
|
|
|
|
|
time: 0,
|
|
|
|
@ -442,8 +757,8 @@ var inject = function () {
|
|
|
|
|
var start = performance.now();
|
|
|
|
|
var ret = unpatchedApplyFunction.apply(this, arguments);
|
|
|
|
|
var end = performance.now();
|
|
|
|
|
debug.scopeDirty[this.$id] = true;
|
|
|
|
|
|
|
|
|
|
debug.scopes[thatScope.$id] = getScopeLocals(thatScope);
|
|
|
|
|
//TODO: move these checks out of here and into registering the watcher
|
|
|
|
|
if (!debug.applyPerf[applyStr]) {
|
|
|
|
|
debug.applyPerf[applyStr] = {
|
|
|
|
@ -453,7 +768,6 @@ var inject = function () {
|
|
|
|
|
}
|
|
|
|
|
debug.applyPerf[applyStr].time += (end - start);
|
|
|
|
|
debug.applyPerf[applyStr].calls += 1;
|
|
|
|
|
debug.rootScopeDirty[thatScope.$root.$id] = true;
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
@ -469,12 +783,16 @@ var inject = function () {
|
|
|
|
|
if (debug.watchers[this.$id]) {
|
|
|
|
|
delete debug.watchers[this.$id];
|
|
|
|
|
}
|
|
|
|
|
if (debug.models[this.$id]) {
|
|
|
|
|
delete debug.models[this.$id];
|
|
|
|
|
}
|
|
|
|
|
if (debug.scopes[this.$id]) {
|
|
|
|
|
delete debug.scopes[this.$id];
|
|
|
|
|
}
|
|
|
|
|
return _destroy.apply(this, arguments);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// patch $new
|
|
|
|
|
// ----------
|
|
|
|
|
var _new = $delegate.__proto__.$new;
|
|
|
|
@ -483,6 +801,7 @@ var inject = function () {
|
|
|
|
|
var ret = _new.apply(this, arguments);
|
|
|
|
|
if (ret.$root) {
|
|
|
|
|
debug.rootScopes[ret.$root.$id] = ret.$root;
|
|
|
|
|
debug.scopeTreeDirty[ret.$root.$id] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create empty watchers array for this scope
|
|
|
|
@ -490,11 +809,23 @@ var inject = function () {
|
|
|
|
|
debug.watchers[ret.$id] = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug.rootScopeDirty[ret.$root.$id] = true;
|
|
|
|
|
debug.scopes[ret.$id] = ret;
|
|
|
|
|
debug.scopeDirty[ret.$id] = true;
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// patch $digest
|
|
|
|
|
// -------------
|
|
|
|
|
var _digest = $delegate.__proto__.$digest;
|
|
|
|
|
$delegate.__proto__.$digest = function (fn) {
|
|
|
|
|
var ret = _digest.apply(this, arguments);
|
|
|
|
|
debug.scopeDirty[this.$id] = true;
|
|
|
|
|
return ret;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// patch $apply
|
|
|
|
|
// ------------
|
|
|
|
|
var _apply = $delegate.__proto__.$apply;
|
|
|
|
@ -502,6 +833,7 @@ var inject = function () {
|
|
|
|
|
var start = performance.now();
|
|
|
|
|
var ret = _apply.apply(this, arguments);
|
|
|
|
|
var end = performance.now();
|
|
|
|
|
debug.scopeDirty[this.$id] = true;
|
|
|
|
|
|
|
|
|
|
// If the debugging option is enabled, log to console
|
|
|
|
|
// --------------------------------------------------
|
|
|
|
@ -521,7 +853,7 @@ var inject = function () {
|
|
|
|
|
// Return a script element with the above code embedded in it
|
|
|
|
|
var script = window.document.createElement('script');
|
|
|
|
|
script.innerHTML = '(' + fn.toString() + '(window))';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return script;
|
|
|
|
|
}()));
|
|
|
|
|
};
|
|
|
|
|