From 807596596f2923241f57f1349cafc7d0261aca5a Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 21 Jun 2016 00:18:38 -0700 Subject: [PATCH] fix badging, parsing of operating hours --- .../resources/web/settings/js/settings.js | 12 +++- domain-server/src/DomainMetadata.cpp | 55 +++++++++++++++---- domain-server/src/DomainMetadata.h | 2 + .../src/DomainServerSettingsManager.cpp | 6 +- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/domain-server/resources/web/settings/js/settings.js b/domain-server/resources/web/settings/js/settings.js index 709e0e05ce..c2cb2ecb80 100644 --- a/domain-server/resources/web/settings/js/settings.js +++ b/domain-server/resources/web/settings/js/settings.js @@ -243,6 +243,16 @@ $(document).ready(function(){ } }); + $('#' + Settings.FORM_ID).on('change', 'input.table-time', function() { + // Bootstrap switches in table: set the changed data attribute for all rows in table. + var row = $(this).closest('tr'); + if (row.hasClass("value-row")) { // Don't set attribute on input row switches prior to it being added to table. + row.find('td.' + Settings.DATA_COL_CLASS + ' input').attr('data-changed', true); + updateDataChangedForSiblingRows(row, true); + badgeSidebarForDifferences($(this)); + } + }); + $('.advanced-toggle').click(function(){ Settings.showAdvanced = !Settings.showAdvanced var advancedSelector = $('.' + Settings.ADVANCED_CLASS) @@ -1009,7 +1019,7 @@ function makeTable(setting, keypath, setting_value, isLocked) { + "name='" + colName + "'" + (colValue ? " checked" : "") + " />"; } else if (isArray && col.type === "time" && col.editable) { html += "" - + ""; } else { // Use a hidden input so that the values are posted. diff --git a/domain-server/src/DomainMetadata.cpp b/domain-server/src/DomainMetadata.cpp index c1656af5b7..cfb48e6c49 100644 --- a/domain-server/src/DomainMetadata.cpp +++ b/domain-server/src/DomainMetadata.cpp @@ -39,6 +39,8 @@ const QString DomainMetadata::Descriptors::HOURS = "hours"; const QString DomainMetadata::Descriptors::Hours::WEEKDAY = "weekday"; const QString DomainMetadata::Descriptors::Hours::WEEKEND = "weekend"; const QString DomainMetadata::Descriptors::Hours::UTC_OFFSET = "utc_offset"; +const QString DomainMetadata::Descriptors::Hours::OPEN = "open"; +const QString DomainMetadata::Descriptors::Hours::CLOSE = "close"; // descriptors metadata will appear as (JSON): // { "description": String, // capped description // "capacity": Number, @@ -59,8 +61,13 @@ const QString DomainMetadata::Descriptors::Hours::UTC_OFFSET = "utc_offset"; // it is meant to be sent to and consumed by an external API DomainMetadata::DomainMetadata(QObject* domainServer) : QObject(domainServer) { - _metadata[USERS] = {}; - _metadata[DESCRIPTORS] = {}; + _metadata[USERS] = QVariantMap {}; + _metadata[DESCRIPTORS] = QVariantMap { + { Descriptors::HOURS, QVariantMap { + { Descriptors::Hours::WEEKDAY, QVariantList() }, + { Descriptors::Hours::WEEKEND, QVariantList() } + } } + }; assert(dynamic_cast(domainServer)); DomainServer* server = static_cast(domainServer); @@ -87,10 +94,38 @@ QJsonObject DomainMetadata::get(const QString& group) { return QJsonObject::fromVariantMap(_metadata[group].toMap()); } +QVariant parseHours(QVariant base, QVariant delta) { + using Hours = DomainMetadata::Descriptors::Hours; + + auto& baseList = base.toList(); + auto& deltaList = delta.toList(); + if (baseList.isEmpty() || !baseList.length()) { + return delta; + } else if (deltaList.isEmpty() || !deltaList.length()) { + return base; + } + + auto& baseMap = baseList[0].toMap(); + auto& deltaMap = deltaList[0].toMap(); + if (baseMap.isEmpty()) { + return delta; + } else if (deltaMap.isEmpty()) { + return base; + } + + // merge delta into base + // hours should be of the form [ { open: Time, close: Time } ], so one level is sufficient + foreach(auto key, baseMap.keys()) { + deltaMap[key] = baseMap[key]; + } + + return base; +} + void DomainMetadata::descriptorsChanged() { // get descriptors auto settings = static_cast(parent())->_settingsManager.getSettingsMap(); - auto descriptors = settings[DESCRIPTORS].toMap(); + auto& descriptors = settings[DESCRIPTORS].toMap(); // parse capacity const QString CAPACITY = "security.maximum_user_capacity"; @@ -100,20 +135,21 @@ void DomainMetadata::descriptorsChanged() { // parse operating hours const QString WEEKDAY_HOURS = "weekday_hours"; - const QString WEEKEND_HOURS = "weekday_hours"; + const QString WEEKEND_HOURS = "weekend_hours"; const QString UTC_OFFSET = "utc_offset"; - auto weekdayHours = descriptors[WEEKDAY_HOURS]; - auto weekendHours = descriptors[WEEKEND_HOURS]; + auto& hours = _metadata[DESCRIPTORS].toMap()[Descriptors::HOURS].toMap(); + assert(!hours.isEmpty()); + auto weekdayHours = parseHours(hours[Descriptors::Hours::WEEKDAY], descriptors[WEEKDAY_HOURS]); + auto weekendHours = parseHours(hours[Descriptors::Hours::WEEKEND], descriptors[WEEKEND_HOURS]); auto utcOffset = descriptors[UTC_OFFSET]; descriptors.remove(WEEKDAY_HOURS); descriptors.remove(WEEKEND_HOURS); descriptors.remove(UTC_OFFSET); - QVariantMap hours { + descriptors[Descriptors::HOURS] = QVariantMap { { Descriptors::Hours::UTC_OFFSET, utcOffset }, { Descriptors::Hours::WEEKDAY, weekdayHours }, { Descriptors::Hours::WEEKEND, weekendHours } }; - descriptors[Descriptors::HOURS] = hours; // update metadata _metadata[DESCRIPTORS] = descriptors; @@ -150,9 +186,8 @@ void DomainMetadata::securityChanged(bool send) { restriction = RESTRICTION_ACL; } - auto descriptors = _metadata[DESCRIPTORS].toMap(); + auto& descriptors = _metadata[DESCRIPTORS].toMap(); descriptors[Descriptors::RESTRICTION] = restriction; - _metadata[DESCRIPTORS] = descriptors; #if DEV_BUILD || PR_BUILD qDebug() << "Domain metadata restriction set:" << restriction; diff --git a/domain-server/src/DomainMetadata.h b/domain-server/src/DomainMetadata.h index 6573a9076c..41f3a60832 100644 --- a/domain-server/src/DomainMetadata.h +++ b/domain-server/src/DomainMetadata.h @@ -45,6 +45,8 @@ public: static const QString WEEKDAY; static const QString WEEKEND; static const QString UTC_OFFSET; + static const QString OPEN; + static const QString CLOSE; }; }; diff --git a/domain-server/src/DomainServerSettingsManager.cpp b/domain-server/src/DomainServerSettingsManager.cpp index f5ff479cd3..543e61f485 100644 --- a/domain-server/src/DomainServerSettingsManager.cpp +++ b/domain-server/src/DomainServerSettingsManager.cpp @@ -271,10 +271,8 @@ void DomainServerSettingsManager::setupConfigMap(const QStringList& argumentList QVariant* weekendHours = valueForKeyPath(_configMap.getUserConfig(), WEEKEND_HOURS, true); QVariant* utcOffset = valueForKeyPath(_configMap.getUserConfig(), UTC_OFFSET, true); - - QVariantList allHours { QVariantMap{ { "open", QVariant("00:00") }, { "close", QVariant("23:59") } } }; - *weekdayHours = allHours; - *weekendHours = allHours; + *weekdayHours = QVariantList { QVariantMap{ { "open", QVariant("00:00") }, { "close", QVariant("23:59") } } }; + *weekendHours = QVariantList { QVariantMap{ { "open", QVariant("00:00") }, { "close", QVariant("23:59") } } }; *utcOffset = QVariant(QTimeZone::systemTimeZone().offsetFromUtc(QDateTime::currentDateTime()) / (float)3600); // write the new settings to file