Skip to content
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

feat(timeline): add custom events for markers #132

Merged
merged 4 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/timeline/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,34 @@ <h2 id="Events">Events</h2>
</td>
</tr>

<tr>
<td>markerinput</td>
<td>
<ul>
<li><code>id</code> (Number or String): custom time bar id which the marker is attached to.</li>
<li><code>title</code> (Date): the marker title.</li>
<li><code>event</code> (Object): original event triggering the markerinput.</li>
</ul>
</td>
<td>Fired when the marker title has been changed.
Only available when the marker is editable.
</td>
</tr>

<tr>
<td>markerchange</td>
<td>
<ul>
<li><code>id</code> (Number or String): custom time bar id which the marker is attached to.</li>
<li><code>title</code> (Date): the marker title.</li>
<li><code>event</code> (Object): original event triggering the markerchange.</li>
</ul>
</td>
<td>Fired when an alteration to the marker title is committed.
Only available when the marker is editable.
</td>
</tr>

</table>

<h2 id="Editing_Items">Editing Items</h2>
Expand Down
41 changes: 41 additions & 0 deletions lib/timeline/component/CustomTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,23 @@ class CustomTime extends Component {
marker.className = `vis-custom-time-marker`;
marker.innerHTML = title;
marker.style.position = 'absolute';

if (editable) {
marker.setAttribute('contenteditable', 'true');
marker.addEventListener('pointerdown', function () {
marker.focus();
});
marker.addEventListener('input', this._onMarkerInput.bind(this));
// The editable div element has no onchange event, so here emulates the onchange event.
marker.title = title;
marker.addEventListener('blur', function (event) {
if (this.title != event.target.innerHTML) {
this._onMarkerChange(event);
this.title = event.target.innerHTML;
};
}.bind(this));
}

this.bar.appendChild(marker);
}

Expand Down Expand Up @@ -267,6 +278,36 @@ class CustomTime extends Component {
event.stopPropagation();
}

/**
* Perform input operating.
* @param {Event} event
* @private
*/
_onMarkerInput(event) {
this.body.emitter.emit('markerinput', {
id: this.options.id,
title: event.target.innerHTML,
event
});

event.stopPropagation();
}

/**
* Perform change operating.
* @param {Event} event
* @private
*/
_onMarkerChange(event) {
this.body.emitter.emit('markerchange', {
id: this.options.id,
title: event.target.innerHTML,
event
});

event.stopPropagation();
}

/**
* Find a custom time from an event target:
* searches for the attribute 'custom-time' in the event target's element tree
Expand Down