New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor domain label (ready) #79
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/** | ||
* @fileOverview | ||
* Component to display the start/end values of a domain. | ||
*/ | ||
define([ | ||
'core/object', | ||
'core/config', | ||
'core/string', | ||
'core/format', | ||
'd3-ext/util', | ||
'components/mixins', | ||
'components/label' | ||
], | ||
function(obj, configMixin, string, format, d3util, mixins, label) { | ||
'use strict'; | ||
|
||
return function() { | ||
|
||
// PRIVATE | ||
|
||
var defaults, | ||
config, | ||
root, | ||
dataCollection, | ||
innerLabel; | ||
|
||
config = {}; | ||
|
||
defaults = { | ||
type: 'domainLabel', | ||
cid: null, | ||
target: null, | ||
cssClass: null, | ||
suffix: null, | ||
dataId: '$domain', | ||
layout: 'horizontal', | ||
position: 'center-right', | ||
formatter: format.timeDomainUTC, | ||
dimension: 'x', | ||
hiddenStates: ['loading'] | ||
}; | ||
|
||
// PUBLIC | ||
|
||
/** | ||
* The main function. | ||
* @return {components.domainLabel} | ||
*/ | ||
function domainLabel() { | ||
obj.extend(config, defaults); | ||
innerLabel = label(); | ||
domainLabel.rebind( | ||
innerLabel, | ||
'color', | ||
'fontFamily', | ||
'fontWeight', | ||
'fontSize'); | ||
return domainLabel; | ||
} | ||
|
||
// Apply Mixins | ||
obj.extend( | ||
domainLabel, | ||
configMixin.mixin( | ||
config, | ||
'cid', | ||
'target', | ||
'cssClass' | ||
), | ||
mixins.lifecycle, | ||
mixins.toggle); | ||
|
||
/** | ||
* Gets/Sets the data source to be used with the domainLabel. | ||
* @param {Object} data Any data source. | ||
* @return {Object|components.domainLabel} | ||
*/ | ||
domainLabel.data = function(data) { | ||
// Set data if provided. | ||
if (data) { | ||
dataCollection = data; | ||
return domainLabel; | ||
} | ||
// Otherwise return the entire raw data. | ||
return dataCollection; | ||
}; | ||
|
||
/** | ||
* Does the initial render to the document. | ||
* @param {d3.selection|Node|string} A d3.selection, DOM node, | ||
* or a selector string. | ||
* @return {components.domainLabel} | ||
*/ | ||
domainLabel.render = function(selection) { | ||
if (!root) { | ||
root = d3util.applyTarget(domainLabel, selection, function(target) { | ||
var root = target.append('g') | ||
.attr({ | ||
'class': string.classes('component', 'domain-label') | ||
}); | ||
return root; | ||
}); | ||
if (root) { | ||
innerLabel.render(root); | ||
} | ||
} | ||
domainLabel.update(); | ||
return domainLabel; | ||
}; | ||
|
||
/** | ||
* Triggers a document update based on new data/config. | ||
* @return {components.domainLabel} | ||
*/ | ||
domainLabel.update = function() { | ||
// Return early if no data or render() hasn't been called yet. | ||
if (!root) { | ||
return domainLabel; | ||
} | ||
if (config.cssClass) { | ||
root.classed(config.cssClass, true); | ||
} | ||
if (config.cid) { | ||
root.attr('gl-cid', config.cid); | ||
} | ||
innerLabel | ||
.config('dataId', config.dataId) | ||
.data(dataCollection) | ||
.text(function(domainData) { | ||
return config.formatter(domainData[config.dimension], config.suffix); | ||
}) | ||
.update(); | ||
root.position(config.position); | ||
return domainLabel; | ||
}; | ||
|
||
/** | ||
* Returns the formatted text. | ||
* @public | ||
* @return {String} | ||
*/ | ||
domainLabel.text = function() { | ||
return innerLabel.text(); | ||
}; | ||
|
||
/** | ||
* Returns the root | ||
* @return {d3.selection} | ||
*/ | ||
domainLabel.root = function () { | ||
return root; | ||
}; | ||
|
||
/** | ||
* Destroys the domainLabel and removes everything from the DOM. | ||
* @public | ||
*/ | ||
domainLabel.destroy = function() { | ||
if (root) { | ||
root.remove(); | ||
} | ||
root = null; | ||
config = null; | ||
defaults = null; | ||
innerLabel.destroy(); | ||
}; | ||
|
||
return domainLabel(); | ||
}; | ||
|
||
}); | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,26 +2,59 @@ define( | |
function() { | ||
'use strict'; | ||
|
||
function getSuffix(optSuffix) { | ||
return optSuffix ? ' ' + optSuffix : ''; | ||
} | ||
|
||
return { | ||
|
||
/** | ||
* Default formatter for a date/time domain. | ||
* Default formatter for a date/time domain using UTC time. | ||
* Text is in the format: | ||
* ShortMonth Day, HH:MM AM/PM - ShortMonth Day, HH:MM AM/PM UTC | ||
* @private | ||
* ShortMonth Day, HH:MM AM/PM - ShortMonth Day, HH:MM AM/PM | ||
* @public | ||
* @param {Array} domain Contains min and max of the domain. | ||
* @param {Boolean} optUseUTC Optional parameter to set utc format | ||
* @param {String} optSuffix | ||
* @return {String} | ||
* @see https://github.com/mbostock/d3/wiki/Time-Formatting#wiki-format | ||
*/ | ||
timeDomain: function(domain, optUseUTC) { | ||
var formatter, specifier; | ||
specifier = '%b %-e, %I:%M %p'; | ||
formatter = !!optUseUTC ? d3.time.format.utc(specifier) : | ||
d3.time.format(specifier); | ||
return formatter(domain[0]) + ' - ' + formatter(domain[1]) + ' UTC'; | ||
timeDomainUTC: function(domain, optSuffix) { | ||
var formatter; | ||
formatter = d3.time.format.utc('%b %-e, %I:%M %p'); | ||
return formatter(domain[0]) + ' - ' + | ||
formatter(domain[1]) + getSuffix(optSuffix); | ||
}, | ||
|
||
/** | ||
* Formatter for a date/time domain using local time. | ||
* Text is in the format: | ||
* ShortMonth Day, HH:MM AM/PM - ShortMonth Day, HH:MM AM/PM -0700 | ||
* @public | ||
* @param {Array} domain Contains min and max of the domain. | ||
* @param {String} optSuffix | ||
* @return {String} | ||
* @see https://github.com/mbostock/d3/wiki/Time-Formatting#wiki-format | ||
*/ | ||
timeDomain: function(domain, optSuffix) { | ||
var formatter; | ||
formatter = d3.time.format('%b %-e, %I:%M %p'); | ||
return formatter(domain[0]) + ' - ' + | ||
formatter(domain[1]) + getSuffix(optSuffix); | ||
}, | ||
|
||
/** | ||
* Standard formatter for non-time domains. | ||
* Text is in the format: | ||
* start - end unit | ||
* | ||
* @param {Array} domain | ||
* @param {String} optSuffix | ||
* @return {String} | ||
*/ | ||
standardDomain: function(domain, optSuffix) { | ||
return domain[0] + ' - ' + domain[1] + getSuffix(optSuffix); | ||
} | ||
|
||
}; | ||
|
||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we dont have any tests for this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, all that logic in graph.js did not make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't know why I put it there in the 1st place.