refactor(cient): Simplify char resize logic

closes #6836
This commit is contained in:
Blade Barringer
2016-09-18 21:26:06 -05:00
parent 7f5d070ee6
commit e3b9636c42

View File

@@ -186,9 +186,9 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
} }
$rootScope.charts = {}; $rootScope.charts = {};
$rootScope.resizeCharts = {}; var resizeChartFunctions = {};
$rootScope.toggleChart = function(id, task) { $rootScope.toggleChart = function(id, task) {
var history = [], matrix, data, options, container; var history = [], matrix, data;
switch (id) { switch (id) {
case 'exp': case 'exp':
history = User.user.history.exp; history = User.user.history.exp;
@@ -204,13 +204,12 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
if (task && task._editing) task._editing = false; if (task && task._editing) task._editing = false;
} }
if($rootScope.charts[id]) { if ($rootScope.charts[id] && !resizeChartFunctions[id]) {
var handleResize = _.debounce(function() { resizeChartFunctions[id] = function() {
drawChart(id, data); drawChart(id, data);
}, 300); };
} else if (!$rootScope.charts[id]) {
$rootScope.resizeCharts[id] = $rootScope.resizeCharts[id] || _.once(function() { $(window).resize(handleResize) }); delete resizeChartFunctions[id];
$rootScope.resizeCharts[id]();
} }
matrix = [[env.t('date'), env.t('score')]]; matrix = [[env.t('date'), env.t('score')]];
@@ -222,15 +221,15 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
drawChart(id, data); drawChart(id, data);
}; };
function drawChart(id, data, options) { function drawChart(id, data) {
var chart, width; var chart, width, options;
if(id === "exp") { if (id === 'exp') {
width = $(".row").width() - 20; width = $('.row').width() - 20;
} else if(id === "todos") { } else if (id === 'todos') {
width = $(".task-column.todos").width(); width = $('.task-column.todos').width();
} else { } else {
width = $(".task-text").width() - 15; width = $('.task-text').width() - 20;
} }
options = { options = {
@@ -430,5 +429,12 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
$scope.ctrlPressed = false; $scope.ctrlPressed = false;
} }
}); });
var resizeAllCharts = _.debounce(function () {
_.each(resizeChartFunctions, function (fn) {
fn();
});
}, 100);
$(window).resize(resizeAllCharts);
} }
]); ]);