-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from github/custom-elements-v1
Use Custom Elements v1 API
- Loading branch information
Showing
6 changed files
with
124 additions
and
153 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,61 @@ | ||
import {makeFormatter} from './utils' | ||
|
||
const ExtendedTimePrototype = Object.create(HTMLElement.prototype) | ||
|
||
// Internal: Refresh the time element's formatted date when an attribute changes. | ||
// | ||
// Returns nothing. | ||
ExtendedTimePrototype.attributeChangedCallback = function(attrName, oldValue, newValue) { | ||
if (attrName === 'datetime') { | ||
const millis = Date.parse(newValue) | ||
this._date = isNaN(millis) ? null : new Date(millis) | ||
} | ||
|
||
const title = this.getFormattedTitle() | ||
if (title) { | ||
this.setAttribute('title', title) | ||
export default class ExtendedTimeElement extends HTMLElement { | ||
static get observedAttributes() { | ||
return ['datetime', 'day', 'format', 'hour', 'minute', 'month', 'second', 'title', 'weekday', 'year'] | ||
} | ||
|
||
const text = this.getFormattedDate() | ||
if (text) { | ||
this.textContent = text | ||
} | ||
} | ||
// Internal: Refresh the time element's formatted date when an attribute changes. | ||
// | ||
// Returns nothing. | ||
attributeChangedCallback(attrName, oldValue, newValue) { | ||
if (attrName === 'datetime') { | ||
const millis = Date.parse(newValue) | ||
this._date = isNaN(millis) ? null : new Date(millis) | ||
} | ||
|
||
// Internal: Format the ISO 8601 timestamp according to the user agent's | ||
// locale-aware formatting rules. The element's existing `title` attribute | ||
// value takes precedence over this custom format. | ||
// | ||
// Returns a formatted time String. | ||
ExtendedTimePrototype.getFormattedTitle = function() { | ||
if (!this._date) { | ||
return | ||
} | ||
const title = this.getFormattedTitle() | ||
if (title && !this.hasAttribute('title')) { | ||
this.setAttribute('title', title) | ||
} | ||
|
||
if (this.hasAttribute('title')) { | ||
return this.getAttribute('title') | ||
const text = this.getFormattedDate() | ||
if (text) { | ||
this.textContent = text | ||
} | ||
} | ||
|
||
const formatter = makeFormatter({ | ||
day: 'numeric', | ||
month: 'short', | ||
year: 'numeric', | ||
hour: 'numeric', | ||
minute: '2-digit', | ||
timeZoneName: 'short' | ||
}) | ||
// Internal: Format the ISO 8601 timestamp according to the user agent's | ||
// locale-aware formatting rules. The element's existing `title` attribute | ||
// value takes precedence over this custom format. | ||
// | ||
// Returns a formatted time String. | ||
getFormattedTitle() { | ||
if (!this._date) { | ||
return | ||
} | ||
|
||
if (formatter) { | ||
return formatter.format(this._date) | ||
} else { | ||
try { | ||
return this._date.toLocaleString() | ||
} catch (e) { | ||
if (e instanceof RangeError) { | ||
return this._date.toString() | ||
} else { | ||
throw e | ||
const formatter = makeFormatter({ | ||
day: 'numeric', | ||
month: 'short', | ||
year: 'numeric', | ||
hour: 'numeric', | ||
minute: '2-digit', | ||
timeZoneName: 'short' | ||
}) | ||
|
||
if (formatter) { | ||
return formatter.format(this._date) | ||
} else { | ||
try { | ||
return this._date.toLocaleString() | ||
} catch (e) { | ||
if (e instanceof RangeError) { | ||
return this._date.toString() | ||
} else { | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default ExtendedTimePrototype |
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 |
---|---|---|
@@ -1,19 +1,18 @@ | ||
import RelativeTime from './relative-time' | ||
import RelativeTimePrototype from './relative-time-element' | ||
import RelativeTimeElement from './relative-time-element' | ||
|
||
const TimeAgoPrototype = Object.create(RelativeTimePrototype) | ||
|
||
TimeAgoPrototype.getFormattedDate = function() { | ||
if (this._date) { | ||
const format = this.getAttribute('format') | ||
if (format === 'micro') { | ||
return new RelativeTime(this._date).microTimeAgo() | ||
} else { | ||
return new RelativeTime(this._date).timeAgo() | ||
export default class TimeAgoElement extends RelativeTimeElement { | ||
getFormattedDate() { | ||
if (this._date) { | ||
const format = this.getAttribute('format') | ||
if (format === 'micro') { | ||
return new RelativeTime(this._date).microTimeAgo() | ||
} else { | ||
return new RelativeTime(this._date).timeAgo() | ||
} | ||
} | ||
} | ||
} | ||
|
||
window.TimeAgoElement = document.registerElement('time-ago', { | ||
prototype: TimeAgoPrototype | ||
}) | ||
window.TimeAgoElement = TimeAgoElement | ||
window.customElements.define('time-ago', TimeAgoElement) |
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 |
---|---|---|
@@ -1,19 +1,18 @@ | ||
import RelativeTime from './relative-time' | ||
import RelativeTimePrototype from './relative-time-element' | ||
import RelativeTimeElement from './relative-time-element' | ||
|
||
const TimeUntilPrototype = Object.create(RelativeTimePrototype) | ||
|
||
TimeUntilPrototype.getFormattedDate = function() { | ||
if (this._date) { | ||
const format = this.getAttribute('format') | ||
if (format === 'micro') { | ||
return new RelativeTime(this._date).microTimeUntil() | ||
} else { | ||
return new RelativeTime(this._date).timeUntil() | ||
export default class TimeUntilElement extends RelativeTimeElement { | ||
getFormattedDate() { | ||
if (this._date) { | ||
const format = this.getAttribute('format') | ||
if (format === 'micro') { | ||
return new RelativeTime(this._date).microTimeUntil() | ||
} else { | ||
return new RelativeTime(this._date).timeUntil() | ||
} | ||
} | ||
} | ||
} | ||
|
||
window.TimeUntilElement = document.registerElement('time-until', { | ||
prototype: TimeUntilPrototype | ||
}) | ||
window.TimeUntilElement = TimeUntilElement | ||
window.customElements.define('time-until', TimeUntilElement) |