Skip to content

Change Tracker

shared.createChangeTracker(opts) compares a serialized snapshot with the current state and drives the Unsaved Indicator. Passed to page.guardUnsaved, it also asks before the user leaves with unsaved edits.

Decision

When Use
Any page or dialog where a save is explicit Change Tracker.
A field that saves on change Nothing. The field saves itself.

Usage

var tracker = shared.createChangeTracker({
    serialize: function () { return JSON.stringify(readForm()); },
    indicatorId: 'unsavedIndicator',
    savedText: 'Saved'
});
view.addEventListener('input', function () { tracker.check(); });
page.guardUnsaved(tracker, 'Leave without saving your settings?');
// after a load: tracker.markClean();   after a save: tracker.flashSaved();

States and Modifiers

  • isDirty() says whether the state differs from the snapshot.
  • check() does the same and shows or hides the indicator. Call it after edits.
  • markClean() takes a new snapshot and hides the indicator. Call it after a load.
  • flashSaved() takes a new snapshot and pulses the indicator in the success color for 1.5 seconds. With savedText, the indicator shows that text for the pulse, then the original label returns.
  • The options are { serialize, indicatorId, indicator, savedText }. The indicator gets role="status" when it has no role.

Gotchas

  • serialize() must return a stable string of the tracked state. The snapshot is taken when the tracker is created.
  • flashSaved without savedText pulses the indicator with its own label.

Example

Example Code

<!-- title: Change Tracker -->
<div class="inputContainer">
    <label for="txtTrack">Name <span class="stolon-unsaved" id="unsavedIndicator"><span class="stolon-unsaved-dot"></span>Unsaved changes</span></label>
    <input is="emby-input" type="text" id="txtTrack" value="Living room" />
</div>
<div style="display: flex; flex-wrap: wrap; gap: 10px; align-items: center;">
    <button type="button" id="btnSave" class="raised stolon-button stolon-primary stolon-small"><span>Save</span></button>
    <span id="dirtyOut" class="stolon-mono">isDirty: false</span>
</div>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
const input = document.getElementById('txtTrack');
const tracker = shared.createChangeTracker({
    serialize: () => input.value,
    indicatorId: 'unsavedIndicator',
    savedText: 'Saved'
});
const report = () => { document.getElementById('dirtyOut').textContent = 'isDirty: ' + tracker.isDirty(); };
input.addEventListener('input', () => { tracker.check(); report(); });
shared.bindClick('btnSave', () => { tracker.flashSaved(); report(); });
</script>