Skip to content

Commit

Permalink
Fixes jshint errors and removes unused timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Will committed Jun 20, 2014
1 parent 4f81a3f commit 052cd51
Showing 1 changed file with 64 additions and 62 deletions.
126 changes: 64 additions & 62 deletions src/angular-dc.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ angular.module('angularDc', [])
// Get additional options from chartElement's html attributes.
// All options are prepended with 'dc-'' to avoid clashing with html own meaning (e.g width)
// All options are parsed in angular's $parse language, so beware, it is not javascript!
var options = getOptionsFromAttrs(scope, iAttrs, validOptions);
options = getOptionsFromAttrs(scope, iAttrs, validOptions);

// we may have a dc-options attribute which contain a javascript object for stuff
// not writtable in $parse language
if ("options" in options) {
if ('options' in options) {
options = _.merge(options, options.options);
options.options = undefined;
}
// If we have a dc-name attribute, we populate the scope with the chart
// object dc-name
if ("name" in options) {
if ('name' in options) {
scope[options.name] = chart;
options.name = undefined;
}
Expand All @@ -79,7 +79,7 @@ angular.module('angularDc', [])
'postRedraw': options.onPostRedraw,
'filtered': options.onFiltered,
'zoomed': options.onZoomed,
}).omit(_.isUndefined)
}).omit(_.isUndefined);

// Register the eventHandlers with the chart (Dc.js)
eventHandlers.each(function(handler, evt) {
Expand All @@ -100,7 +100,7 @@ angular.module('angularDc', [])
return _(chart).functions()
.extend(directiveOptions)
.map(function(s) {
return "dc" + s.charAt(0).toUpperCase() + s.substring(1)
return 'dc' + s.charAt(0).toUpperCase() + s.substring(1);
})
.value();
}
Expand All @@ -112,7 +112,7 @@ angular.module('angularDc', [])
.map(function(key) {
var value = scope.$eval(iAttrs[key]);
// remove the dc- prefix if any
if (key.substring(0, 2) === "dc") {
if (key.substring(0, 2) === 'dc') {
key = key.charAt(2).toLowerCase() + key.substring(3);
}
return [key, value];
Expand All @@ -126,66 +126,66 @@ angular.module('angularDc', [])
var printExceptions = false;
// add dc, d3 and commonly used Date method to the scope to allow snippets to be configured in
// the templates
scope.dc = dc
scope.d3 = d3
scope.dc = dc;
scope.d3 = d3;
scope.DateTime = function(a, b, c, d, e, f) {
return new Date(a, b, c, d, e, f);
}
};
scope.Date = function(a, b, c) {
return new Date(a, b, c);
}
};
// watch for the scope to settle until all the attributes are defined
var unwatch = scope.$watch(function() {
var options = _(iAttrs.$attr)
.keys()
.filter(function(s) {
return s.substring(0, 2) === "dc" && s !== "dcChart" && s !== "dcChartGroup"
return s.substring(0, 2) === 'dc' && s !== 'dcChart' && s !== 'dcChartGroup';
})
.map(function(key) {
try {
// We ignore exception waiting for the data to be potentially loaded
// by the controller
var r = scope.$eval(iAttrs[key]);
if (_.isUndefined(r)) {
throw Error(iAttrs[key] + " is undefined")
throw Error(iAttrs[key] + ' is undefined');
}
return r
return r;
} catch (e) {
if (printExceptions) {
console.log("unable to eval" + key + ":" + iAttrs[key])
throw e
console.log('unable to eval' + key + ':' + iAttrs[key]);
throw e;
}
return undefined
return undefined;
}
});
if (options.any(_.isUndefined)) {
// return undefined if there is at least one undefined option
// so that the $watch dont call us again at this $digest time
return undefined
return undefined;
}
return options.value()
return options.value();
}, function(options) {
if (!_.isUndefined(options)) {
// Stop the $watch, as we now created the charts
unwatch()
unwatch();

var chart = setupChart(scope, iElement, iAttrs);
// populate the .reset childrens with necessary reset callbacks
var a = angular.element(iElement[0].querySelector("a.reset"));
a.on("click", function() {
var a = angular.element(iElement[0].querySelector('a.reset'));
a.on('click', function() {
chart.filterAll();
dc.redrawAll();
});
a.attr("href", "javascript:;")
a.css("display", "none")
a.attr('href', 'javascript:;');
a.css('display', 'none');
// watching the attributes is costly, so we stop after first rendering
chart.render();
}
});
// if after 4 second we still get exceptions, we should raise them
// to help debugging. $timeout will trigger another round of check.
$timeout(function() {
printExceptions = true
printExceptions = true;
}, 2000);

}
Expand All @@ -205,43 +205,45 @@ angular.module('angularDc', [])
with same dimension. This is a limitation of the underlying lib dc.js
*/
angular.module('angularDc')
.directive('dcSelect', ['$timeout', function ($timeout) {
return {
restrict: 'E',
scope: {
dcDimension: "=",
allLabel: "@"
},
template: "<select class='form-control' ng-model='selectModel' " + "ng-options='d.key for d in selectOptions'>",
link: function (scope, iElement, iAttrs) {
scope.$watch('dcDimension', function (dimension) {
var allkeys, chart;
if (dimension != null) {
// we make a fake chart so that the dimension is known by dc.filterAll()
chart = dc.baseMixin({});
chart.dimension(dimension);
chart.group(dimension);
chart._doRender = function () {};
chart._doRedraw = function () {};
scope.selectModel = {
key: scope.allLabel
};
allkeys = dimension.group().orderNatural().all();
scope.selectOptions = [scope.selectModel].concat(allkeys);
}
});
return scope.$watch('selectModel', function (sel) {
if (scope.dcDimension != null) {
if (sel !== null && sel.key !== scope.allLabel) {
scope.dcDimension.filter(function (d) {
return d === sel.key;
});
} else {
scope.dcDimension.filter(null);
}
dc.redrawAll();
.directive('dcSelect', [
function() {
return {
restrict: 'E',
scope: {
dcDimension: '=',
allLabel: '@'
},
template: '<select class="form-control" ng-model="selectModel" ' + 'ng-options="d.key for d in selectOptions">',
link: function(scope, iElement, iAttrs) {
scope.$watch('dcDimension', function(dimension) {
var allkeys, chart;
if (dimension !== null) {
// we make a fake chart so that the dimension is known by dc.filterAll()
chart = dc.baseMixin({});
chart.dimension(dimension);
chart.group(dimension);
chart._doRender = function() {};
chart._doRedraw = function() {};
scope.selectModel = {
key: scope.allLabel
};
allkeys = dimension.group().orderNatural().all();
scope.selectOptions = [scope.selectModel].concat(allkeys);
}
});
return scope.$watch('selectModel', function(sel) {
if (scope.dcDimension !== null) {
if (sel !== null && sel.key !== scope.allLabel) {
scope.dcDimension.filter(function(d) {
return d === sel.key;
});
} else {
scope.dcDimension.filter(null);
}
dc.redrawAll();
}
});
}
});
};
}
};
}]);
]);

0 comments on commit 052cd51

Please sign in to comment.