Home Information - Your home, your data, organized.
Python
[Feature] Visual indicators for unsaved attribute changes (Issue #99) (#102)
cassandra-ai-agent82376bab59fc
diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_boolean.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_boolean.htmlindex f6e2cae3..dd68ce73 100644--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_boolean.html+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_boolean.html@@ -6,7 +6,8 @@<div class="input-group align-items-center" ><input type="checkbox" name="{{ attribute_form.value.html_name }}"id="{{ attribute_form.value.id_for_label }}"- {% if attribute_form.value.value %}checked="on"{% endif %} >+ {% if attribute_form.value.value %}checked="on"{% endif %}+ data-original-value="{% if attribute_form.value.value %}true{% else %}false{% endif %}" ><label for="{{ attribute_form.value.id_for_label }}" class="pl-2 mb-0">{{ attribute_form.name.value }}</label></div>diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_enum.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_enum.htmlindex 27eb65d9..dfaa0466 100644--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_enum.html+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_enum.html@@ -10,7 +10,8 @@<selectid="{{ attribute_form.value.id_for_label }}"{% if attribute_form.instance and not attribute_form.instance.is_editable %}disabled="true"{% endif %}- name="{{ attribute_form.value.html_name }}" >+ name="{{ attribute_form.value.html_name }}"+ data-original-value="{{ attribute_form.value.value|default_if_none:'' }}" >{% for value, label in choices_list %}<option value="{{ value }}" {% if attribute_form.value.value == value %}selected="true"{% endif %}>{{ label }}diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_field_value.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_field_value.htmlindex 8a211459..ccb89732 100644--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_field_value.html+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_field_value.html@@ -2,7 +2,8 @@<textarea rows="1" cols="40"id="{{ attribute_form.value.id_for_label }}"{% if attribute_form.instance and not attribute_form.instance.is_editable %}disabled="true"{% endif %}- name="{{ attribute_form.value.html_name }}">{{ attribute_form.value.value|default_if_none:'' }}</textarea>+ name="{{ attribute_form.value.html_name }}"+ data-original-value="{{ attribute_form.value.value|default_if_none:'' }}">{{ attribute_form.value.value|default_if_none:'' }}</textarea></div><div class="form-error text-danger" >{{ attribute_form.value.errors }}</div><script type="text/javascript">diff --git a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.html b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.htmlindex 4e0f8481..3f1a11b0 100644--- a/src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.html+++ b/src/hi/apps/attribute/templates/attribute/panes/attribute_form_text.html@@ -10,7 +10,8 @@id="{{ attribute_form.value.id_for_label }}"{% if attribute_form.instance and not attribute_form.instance.is_editable %}disabled="true"{% endif %}name="{{ attribute_form.value.html_name }}"- value="{{ attribute_form.value.value|default_if_none:'' }}">+ value="{{ attribute_form.value.value|default_if_none:'' }}"+ data-original-value="{{ attribute_form.value.value|default_if_none:'' }}"><span class="pl-2"><input type="checkbox" name="{{ attribute_form.value.html_name }}-show"id="{{ attribute_form.value.id_for_label }}-show"diff --git a/src/hi/settings/base.py b/src/hi/settings/base.pyindex 5d5287ba..32579f71 100644--- a/src/hi/settings/base.py+++ b/src/hi/settings/base.py@@ -247,6 +247,7 @@ PIPELINE = {'js/main.js','js/settings.js','js/audio.js',+ 'js/attribute-changes.js',),'output_filename': 'js/js_before_content.js',},diff --git a/src/hi/static/css/main.css b/src/hi/static/css/main.cssindex df3d6dc1..73af94ec 100644--- a/src/hi/static/css/main.css+++ b/src/hi/static/css/main.css@@ -726,3 +726,46 @@ g[hover] {font-size: 12px;}}++/* ===== ATTRIBUTE CHANGE INDICATORS ===== */++/* Visual indicator for modified attribute fields */+.attribute-modified {+ border: 2px solid var(--warning-color) !important;+ background-color: rgba(236, 165, 0, 0.05);+ transition: border-color 0.2s ease-in-out, background-color 0.2s ease-in-out;+}++/* Asterisk indicator for modified fields */+.attribute-modified-indicator {+ color: var(--warning-color);+ font-weight: bold;+ font-size: 1.1em;+ margin-left: 4px;+}+++/* Focus states for modified fields */+.attribute-modified:focus {+ border-color: var(--warning-color);+ box-shadow: 0 0 0 0.2rem rgba(236, 165, 0, 0.25);+}++/* Dirty form styling - submit button enhancement */+form[data-dirty] .btn-primary[type="submit"] {+ background-color: var(--warning-color) !important;+ border-color: var(--warning-color) !important;+ color: var(--on-warning-color) !important;+}++form[data-dirty] button[type="submit"]:hover {+ background-color: #d39e00 !important; /* Darker shade of warning color */+ border-color: #d39e00 !important;+}++/* Dirty form styling - subtle form background */+form[data-dirty] {+ background-color: rgba(236, 165, 0, 0.2) !important;+ border-radius: 4px;+ transition: background-color 0.2s ease-in-out;+}diff --git a/src/hi/static/js/attribute-changes.js b/src/hi/static/js/attribute-changes.jsnew file mode 100644index 00000000..26723306--- /dev/null+++ b/src/hi/static/js/attribute-changes.js@@ -0,0 +1,249 @@+// Attribute Changes Tracker+// Provides visual indicators for unsaved attribute changes++(function($) {+ 'use strict';++ // Namespace for attribute change tracking+ const AttributeChanges = {++ // Configuration+ config: {+ modifiedClass: 'attribute-modified',+ indicatorClass: 'attribute-modified-indicator',+ debounceDelay: 300+ },++ // State tracking+ state: {+ originalValues: new Map(),+ modifiedFields: new Set(),+ debounceTimers: new Map()+ },++ // Initialize the change tracking system+ init: function() {+ this.captureOriginalValues();+ this.bindEvents();+ },++ // Capture original values for all attribute fields+ captureOriginalValues: function() {+ const self = this;++ // Find all attribute form fields+ $('.hi-attribute-list').find('input, textarea, select').each(function() {+ const $field = $(this);+ const fieldId = $field.attr('id');++ if (fieldId && !fieldId.endsWith('-show')) { // Skip password show/hide checkboxes+ // Only capture if we don't already have this field tracked+ if (!self.state.originalValues.has(fieldId)) {+ let originalValue = self.getFieldValue($field);+ self.state.originalValues.set(fieldId, originalValue);++ // Add data attribute for reference+ $field.attr('data-original-value', originalValue);+ }+ }+ });+ },++ // Get normalized field value based on field type+ getFieldValue: function($field) {+ const fieldType = $field.prop('type');+ const tagName = $field.prop('tagName').toLowerCase();++ if (fieldType === 'checkbox') {+ return $field.is(':checked') ? 'true' : 'false';+ } else if (tagName === 'select') {+ return $field.val() || '';+ } else {+ // For text fields and textareas, normalize whitespace+ return ($field.val() || '').trim();+ }+ },++ // Check if field has changed from original value+ hasFieldChanged: function($field) {+ const fieldId = $field.attr('id');+ const originalValue = this.state.originalValues.get(fieldId);+ const currentValue = this.getFieldValue($field);++ return originalValue !== currentValue;+ },++ // Bind event listeners using document-level delegation+ bindEvents: function() {+ const self = this;++ // Handle input changes with debouncing for text fields+ // Use document-level delegation to handle dynamically loaded forms+ $(document).on('input keyup', '.hi-attribute-list input[type="text"], .hi-attribute-list input[type="password"], .hi-attribute-list textarea', function() {+ const $field = $(this);+ const fieldId = $field.attr('id');++ // Skip if no ID (shouldn't happen for attribute fields)+ if (!fieldId) return;++ // Clear existing debounce timer+ if (self.state.debounceTimers.has(fieldId)) {+ clearTimeout(self.state.debounceTimers.get(fieldId));+ }++ // Set new debounce timer+ const timer = setTimeout(function() {+ self.handleFieldChange($field);+ self.state.debounceTimers.delete(fieldId);+ }, self.config.debounceDelay);++ self.state.debounceTimers.set(fieldId, timer);+ });++ // Handle immediate changes for select and checkbox+ $(document).on('change', '.hi-attribute-list select, .hi-attribute-list input[type="checkbox"]', function() {+ self.handleFieldChange($(this));+ });++ // Handle form submission to clear indicators+ $(document).on('submit', 'form', function() {+ // Only clear if this form contains attributes+ if ($(this).find('.hi-attribute-list').length > 0) {+ self.clearAllIndicators();+ }+ });++ // Handle successful async form submission (using antinode.js pattern)+ $(document).on('an:success', function(e) {+ // Clear indicators if there are any modified fields+ if (self.state.modifiedFields.size > 0) {+ self.clearAllIndicators();+ }+ });+ },++ // Handle individual field change+ handleFieldChange: function($field) {+ const fieldId = $field.attr('id');++ if (!fieldId || fieldId.endsWith('-show')) {+ return; // Skip password show/hide checkboxes+ }++ const hasChanged = this.hasFieldChanged($field);++ if (hasChanged) {+ this.addFieldIndicator($field);+ this.state.modifiedFields.add(fieldId);+ } else {+ this.removeFieldIndicator($field);+ this.state.modifiedFields.delete(fieldId);+ }++ this.updatePageState();+ },++ // Add visual indicator to field+ addFieldIndicator: function($field) {+ const $container = $field.closest('.input-group, div');++ // Add modified class to field+ $field.addClass(this.config.modifiedClass);++ // Add asterisk indicator if not already present+ if ($container.find('.' + this.config.indicatorClass).length === 0) {+ const $indicator = $('<span class="' + this.config.indicatorClass + '">*</span>');++ // Find the best place to insert the indicator+ const $label = $container.find('.input-group-text').first();+ if ($label.length > 0) {+ $label.append(' ').append($indicator);+ } else {+ $container.append($indicator);+ }+ }+ },++ // Remove visual indicator from field+ removeFieldIndicator: function($field) {+ const $container = $field.closest('.input-group, div');++ // Remove modified class+ $field.removeClass(this.config.modifiedClass);++ // Remove asterisk indicator+ $container.find('.' + this.config.indicatorClass).remove();+ },++ // Update form-level dirty state for submit button and form styling+ updatePageState: function() {+ const hasChanges = this.state.modifiedFields.size > 0;++ // Add/remove dirty attribute on forms containing attribute lists+ $('form').each(function() {+ const $form = $(this);+ if ($form.find('.hi-attribute-list').length > 0) {+ if (hasChanges) {+ $form.attr('data-dirty', 'true');+ } else {+ $form.removeAttr('data-dirty');+ }+ }+ });+ },++++ // Clear all indicators and reset state+ clearAllIndicators: function() {+ const self = this;++ // Clear all visual indicators+ $('.' + this.config.modifiedClass).removeClass(this.config.modifiedClass);+ $('.' + this.config.indicatorClass).remove();++ // Clear form dirty attributes+ $('form[data-dirty]').removeAttr('data-dirty');++ // Clear state+ this.state.modifiedFields.clear();++ // Clear debounce timers+ this.state.debounceTimers.forEach(function(timer) {+ clearTimeout(timer);+ });+ this.state.debounceTimers.clear();+ },++ // Public method to manually refresh original values (for dynamic content)+ refreshOriginalValues: function() {+ this.clearAllIndicators();+ this.state.originalValues.clear();+ this.captureOriginalValues();+ },++ // Initialize tracking for newly loaded attribute forms+ initializeNewForms: function() {+ // Capture original values for any new forms+ this.captureOriginalValues();+ }+ };++ // Initialize when DOM is ready+ $(document).ready(function() {+ // Always initialize - event delegation works without existing elements+ AttributeChanges.init();+ });++ // Hook into antinode async content loading to handle dynamic forms+ $(document).on('an:success', function() {+ // Reinitialize to capture original values for any newly loaded forms+ AttributeChanges.initializeNewForms();+ });++ // Add to Hi namespace for potential external access+ if (window.Hi) {+ window.Hi.AttributeChanges = AttributeChanges;+ }++})(jQuery);\ No newline at end of file