Commit 387ec89b by Torkel Ödegaard

more angular 1.3 upgrade changes

parent 60dd6849
...@@ -95,7 +95,7 @@ function (angular, _, config, $) { ...@@ -95,7 +95,7 @@ function (angular, _, config, $) {
$scope.openSearch = function (evt) { $scope.openSearch = function (evt) {
if (evt) { if (evt) {
$element.find('.dropdown-toggle').dropdown('toggle'); $element.next().find('.dropdown-toggle').dropdown('toggle');
} }
$scope.giveSearchFocus = $scope.giveSearchFocus + 1; $scope.giveSearchFocus = $scope.giveSearchFocus + 1;
......
...@@ -56,7 +56,6 @@ ...@@ -56,7 +56,6 @@
</li> </li>
<li class="dropdown grafana-menu-load" ng-controller="SearchCtrl" ng-init="init()" ng-include="'app/partials/search.html'"> <li class="dropdown grafana-menu-load" ng-controller="SearchCtrl" ng-init="init()" ng-include="'app/partials/search.html'">
</li> </li>
<li class="grafana-menu-home"><a bs-tooltip="'Goto saved default'" data-placement="bottom" href='#/'><i class='icon-home'></i></a></li> <li class="grafana-menu-home"><a bs-tooltip="'Goto saved default'" data-placement="bottom" href='#/'><i class='icon-home'></i></a></li>
......
'use strict'; (function () {
/** "use strict";
* Bindonce - Zero watches binding for AngularJs /**
* @version v0.2.1 - 2013-05-07 * Bindonce - Zero watches binding for AngularJs
* @link https://github.com/Pasvaz/bindonce * @version v0.3.1
* @author Pasquale Vazzana <pasqualevazzana@gmail.com> * @link https://github.com/Pasvaz/bindonce
* @license MIT License, http://www.opensource.org/licenses/MIT * @author Pasquale Vazzana <pasqualevazzana@gmail.com>
*/ * @license MIT License, http://www.opensource.org/licenses/MIT
*/
angular.module('pasvaz.bindonce', []) var bindonceModule = angular.module('pasvaz.bindonce', []);
.directive('bindonce', function() bindonceModule.directive('bindonce', function ()
{
var toBoolean = function(value)
{ {
if (value && value.length !== 0) var toBoolean = function (value)
{ {
var v = angular.lowercase("" + value); if (value && value.length !== 0)
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]'); {
} var v = angular.lowercase("" + value);
else value = !(v === 'f' || v === '0' || v === 'false' || v === 'no' || v === 'n' || v === '[]');
}
else
{
value = false;
}
return value;
};
var msie = parseInt((/msie (\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
if (isNaN(msie))
{ {
value = false; msie = parseInt((/trident\/.*; rv:(\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
} }
return value;
}
var msie = parseInt((/msie (\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
if (isNaN(msie))
{
msie = parseInt((/trident\/.*; rv:(\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
}
var bindonceDirective = var bindonceDirective =
{
restrict: "AM",
controller: ['$scope', '$element', '$attrs', '$interpolate', function($scope, $element, $attrs, $interpolate)
{ {
var showHideBinder = function(elm, attr, value) restrict: "AM",
controller: ['$scope', '$element', '$attrs', '$interpolate', function ($scope, $element, $attrs, $interpolate)
{ {
var show = (attr == 'show') ? '' : 'none'; var showHideBinder = function (elm, attr, value)
var hide = (attr == 'hide') ? '' : 'none'; {
elm.css('display', toBoolean(value) ? show : hide); var show = (attr === 'show') ? '' : 'none';
} var hide = (attr === 'hide') ? '' : 'none';
var classBinder = function(elm, value) elm.css('display', toBoolean(value) ? show : hide);
{ };
if (angular.isObject(value) && !angular.isArray(value)) var classBinder = function (elm, value)
{ {
var results = []; if (angular.isObject(value) && !angular.isArray(value))
angular.forEach(value, function(value, index) {
var results = [];
angular.forEach(value, function (value, index)
{
if (value) results.push(index);
});
value = results;
}
if (value)
{ {
if (value) results.push(index); elm.addClass(angular.isArray(value) ? value.join(' ') : value);
}
};
var transclude = function (transcluder, scope)
{
transcluder.transclude(scope, function (clone)
{
var parent = transcluder.element.parent();
var afterNode = transcluder.element && transcluder.element[transcluder.element.length - 1];
var parentNode = parent && parent[0] || afterNode && afterNode.parentNode;
var afterNextSibling = (afterNode && afterNode.nextSibling) || null;
angular.forEach(clone, function (node)
{
parentNode.insertBefore(node, afterNextSibling);
});
}); });
value = results; };
}
if (value) var ctrl =
{ {
elm.addClass(angular.isArray(value) ? value.join(' ') : value); watcherRemover: undefined,
} binders: [],
} group: $attrs.boName,
element: $element,
ran: false,
var ctrl = addBinder: function (binder)
{ {
watcherRemover : undefined, this.binders.push(binder);
binders : [],
group : $attrs.boName,
element : $element,
ran : false,
addBinder : function(binder) // In case of late binding (when using the directive bo-name/bo-parent)
{ // it happens only when you use nested bindonce, if the bo-children
this.binders.push(binder); // are not dom children the linking can follow another order
if (this.ran)
{
this.runBinders();
}
},
// In case of late binding (when using the directive bo-name/bo-parent) setupWatcher: function (bindonceValue)
// it happens only when you use nested bindonce, if the bo-children
// are not dom children the linking can follow another order
if (this.ran)
{ {
this.runBinders(); var that = this;
} this.watcherRemover = $scope.$watch(bindonceValue, function (newValue)
}, {
if (newValue === undefined) return;
that.removeWatcher();
that.checkBindonce(newValue);
}, true);
},
setupWatcher : function(bindonceValue) checkBindonce: function (value)
{
var that = this;
this.watcherRemover = $scope.$watch(bindonceValue, function(newValue)
{ {
if (newValue == undefined) return; var that = this, promise = (value.$promise) ? value.$promise.then : value.then;
that.removeWatcher(); // since Angular 1.2 promises are no longer
that.runBinders(); // undefined until they don't get resolved
}, true); if (typeof promise === 'function')
}, {
promise(function ()
{
that.runBinders();
});
}
else
{
that.runBinders();
}
},
removeWatcher : function() removeWatcher: function ()
{
if (this.watcherRemover != undefined)
{ {
this.watcherRemover(); if (this.watcherRemover !== undefined)
this.watcherRemover = undefined; {
} this.watcherRemover();
}, this.watcherRemover = undefined;
}
},
runBinders : function() runBinders: function ()
{
var i, max;
for (i = 0, max = this.binders.length; i < max; i ++)
{ {
var binder = this.binders[i]; while (this.binders.length > 0)
if (this.group && this.group != binder.group ) continue;
var value = binder.scope.$eval((binder.interpolate) ? $interpolate(binder.value) : binder.value);
switch(binder.attr)
{ {
case 'if': var binder = this.binders.shift();
if (toBoolean(value)) if (this.group && this.group != binder.group) continue;
{ var value = binder.scope.$eval((binder.interpolate) ? $interpolate(binder.value) : binder.value);
binder.transclude(binder.scope.$new(), function (clone) switch (binder.attr)
{
case 'boIf':
if (toBoolean(value))
{
transclude(binder, binder.scope.$new());
}
break;
case 'boSwitch':
var selectedTranscludes, switchCtrl = binder.controller[0];
if ((selectedTranscludes = switchCtrl.cases['!' + value] || switchCtrl.cases['?']))
{ {
var parent = binder.element.parent(); binder.scope.$eval(binder.attrs.change);
var afterNode = binder.element && binder.element[binder.element.length - 1]; angular.forEach(selectedTranscludes, function (selectedTransclude)
var parentNode = parent && parent[0] || afterNode && afterNode.parentNode;
var afterNextSibling = (afterNode && afterNode.nextSibling) || null;
angular.forEach(clone, function(node)
{ {
parentNode.insertBefore(node, afterNextSibling); transclude(selectedTransclude, binder.scope.$new());
}); });
});
}
break;
case 'hide':
case 'show':
showHideBinder(binder.element, binder.attr, value);
break;
case 'class':
classBinder(binder.element, value);
break;
case 'text':
binder.element.text(value);
break;
case 'html':
binder.element.html(value);
break;
case 'style':
binder.element.css(value);
break;
case 'src':
binder.element.attr(binder.attr, value);
if (msie) binder.element.prop('src', value);
case 'attr':
angular.forEach(binder.attrs, function(attrValue, attrKey)
{
var newAttr, newValue;
if (attrKey.match(/^boAttr./) && binder.attrs[attrKey])
{
newAttr = attrKey.replace(/^boAttr/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
newValue = binder.scope.$eval(binder.attrs[attrKey]);
binder.element.attr(newAttr, newValue);
} }
}); break;
break; case 'boSwitchWhen':
case 'href': var ctrl = binder.controller[0];
case 'alt': ctrl.cases['!' + binder.attrs.boSwitchWhen] = (ctrl.cases['!' + binder.attrs.boSwitchWhen] || []);
case 'title': ctrl.cases['!' + binder.attrs.boSwitchWhen].push({ transclude: binder.transclude, element: binder.element });
case 'id': break;
case 'value': case 'boSwitchDefault':
binder.element.attr(binder.attr, value); var ctrl = binder.controller[0];
break; ctrl.cases['?'] = (ctrl.cases['?'] || []);
ctrl.cases['?'].push({ transclude: binder.transclude, element: binder.element });
break;
case 'hide':
case 'show':
showHideBinder(binder.element, binder.attr, value);
break;
case 'class':
classBinder(binder.element, value);
break;
case 'text':
binder.element.text(value);
break;
case 'html':
binder.element.html(value);
break;
case 'style':
binder.element.css(value);
break;
case 'src':
binder.element.attr(binder.attr, value);
if (msie) binder.element.prop('src', value);
break;
case 'attr':
angular.forEach(binder.attrs, function (attrValue, attrKey)
{
var newAttr, newValue;
if (attrKey.match(/^boAttr./) && binder.attrs[attrKey])
{
newAttr = attrKey.replace(/^boAttr/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
newValue = binder.scope.$eval(binder.attrs[attrKey]);
binder.element.attr(newAttr, newValue);
}
});
break;
case 'href':
case 'alt':
case 'title':
case 'id':
case 'value':
binder.element.attr(binder.attr, value);
break;
}
} }
this.ran = true;
} }
this.ran = true; };
this.binders = [];
}
}
return ctrl; return ctrl;
}], }],
link: function(scope, elm, attrs, bindonceController) link: function (scope, elm, attrs, bindonceController)
{
var value = (attrs.bindonce) ? scope.$eval(attrs.bindonce) : true;
if (value != undefined)
{ {
bindonceController.runBinders(); var value = attrs.bindonce && scope.$eval(attrs.bindonce);
} if (value !== undefined)
else {
{ bindonceController.checkBindonce(value);
bindonceController.setupWatcher(attrs.bindonce); }
elm.bind("$destroy", bindonceController.removeWatcher); else
{
bindonceController.setupWatcher(attrs.bindonce);
elm.bind("$destroy", bindonceController.removeWatcher);
}
} }
} };
};
return bindonceDirective;
});
return bindonceDirective; angular.forEach(
}); [
{ directiveName: 'boShow', attribute: 'show' },
{ directiveName: 'boHide', attribute: 'hide' },
{ directiveName: 'boClass', attribute: 'class' },
{ directiveName: 'boText', attribute: 'text' },
{ directiveName: 'boBind', attribute: 'text' },
{ directiveName: 'boHtml', attribute: 'html' },
{ directiveName: 'boSrcI', attribute: 'src', interpolate: true },
{ directiveName: 'boSrc', attribute: 'src' },
{ directiveName: 'boHrefI', attribute: 'href', interpolate: true },
{ directiveName: 'boHref', attribute: 'href' },
{ directiveName: 'boAlt', attribute: 'alt' },
{ directiveName: 'boTitle', attribute: 'title' },
{ directiveName: 'boId', attribute: 'id' },
{ directiveName: 'boStyle', attribute: 'style' },
{ directiveName: 'boValue', attribute: 'value' },
{ directiveName: 'boAttr', attribute: 'attr' },
angular.forEach( { directiveName: 'boIf', transclude: 'element', terminal: true, priority: 1000 },
[ { directiveName: 'boSwitch', require: 'boSwitch', controller: function () { this.cases = {}; } },
{directiveName:'boShow', attribute: 'show'}, { directiveName: 'boSwitchWhen', transclude: 'element', priority: 800, require: '^boSwitch' },
{directiveName:'boIf', attribute: 'if', transclude: 'element', terminal: true, priority:1000}, { directiveName: 'boSwitchDefault', transclude: 'element', priority: 800, require: '^boSwitch' }
{directiveName:'boHide', attribute:'hide'}, ],
{directiveName:'boClass', attribute:'class'}, function (boDirective)
{directiveName:'boText', attribute:'text'},
{directiveName:'boHtml', attribute:'html'},
{directiveName:'boSrcI', attribute:'src', interpolate:true},
{directiveName:'boSrc', attribute:'src'},
{directiveName:'boHrefI', attribute:'href', interpolate:true},
{directiveName:'boHref', attribute:'href'},
{directiveName:'boAlt', attribute:'alt'},
{directiveName:'boTitle', attribute:'title'},
{directiveName:'boId', attribute:'id'},
{directiveName:'boStyle', attribute:'style'},
{directiveName:'boValue', attribute:'value'},
{directiveName:'boAttr', attribute:'attr'}
],
function(boDirective)
{
var childPriority = 200;
return angular.module('pasvaz.bindonce').directive(boDirective.directiveName, function()
{ {
var bindonceDirective = var childPriority = 200;
return bindonceModule.directive(boDirective.directiveName, function ()
{ {
priority: boDirective.priority || childPriority, var bindonceDirective =
transclude: boDirective.transclude || false,
terminal: boDirective.terminal || false,
require: '^bindonce',
compile: function (tElement, tAttrs, transclude)
{ {
return function(scope, elm, attrs, bindonceController) priority: boDirective.priority || childPriority,
transclude: boDirective.transclude || false,
terminal: boDirective.terminal || false,
require: ['^bindonce'].concat(boDirective.require || []),
controller: boDirective.controller,
compile: function (tElement, tAttrs, transclude)
{ {
var name = attrs.boParent; return function (scope, elm, attrs, controllers)
if (name && bindonceController.group != name)
{ {
var element = bindonceController.element.parent(); var bindonceController = controllers[0];
bindonceController = undefined; var name = attrs.boParent;
var parentValue; if (name && bindonceController.group !== name)
while (element[0].nodeType != 9 && element.length)
{ {
if ((parentValue = element.data('$bindonceController')) var element = bindonceController.element.parent();
&& parentValue.group == name) bindonceController = undefined;
var parentValue;
while (element[0].nodeType !== 9 && element.length)
{ {
bindonceController = parentValue if ((parentValue = element.data('$bindonceController'))
break; && parentValue.group === name)
{
bindonceController = parentValue;
break;
}
element = element.parent();
}
if (!bindonceController)
{
throw new Error("No bindonce controller: " + name);
} }
element = element.parent();
}
if (!bindonceController)
{
throw Error("No bindonce controller: " + name);
} }
}
bindonceController.addBinder( bindonceController.addBinder(
{ {
element : elm, element: elm,
attr : boDirective.attribute, attr: boDirective.attribute || boDirective.directiveName,
attrs : attrs, attrs: attrs,
value : attrs[boDirective.directiveName], value: attrs[boDirective.directiveName],
interpolate : boDirective.interpolate, interpolate: boDirective.interpolate,
group : name, group: name,
transclude : transclude, transclude: transclude,
scope : scope controller: controllers.slice(1),
}); scope: scope
});
};
} }
} };
}
return bindonceDirective; return bindonceDirective;
}); });
}); })
\ No newline at end of file })();
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment