Skip to content

Commit

Permalink
#62 - add three-state boolean configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Oct 28, 2015
1 parent 2a90325 commit 705b115
Show file tree
Hide file tree
Showing 20 changed files with 1,739 additions and 27 deletions.
5 changes: 4 additions & 1 deletion lib_exclude
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
/numeral/languages/**
/numeral/tests/**
/numeral/*.js
/numeral/min/languages/**
/numeral/min/languages/**
/nz-toggle/*
/nz-toggle/example/**
/nz-toggle/src/**
63 changes: 43 additions & 20 deletions src/main/java/alfio/manager/system/ConfigurationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ConfigurationManager {
private static final Map<ConfigurationKeys.SettingCategory, List<Configuration>> EVENT_CONFIGURATION = collectConfigurationKeysByCategory(ConfigurationPathLevel.EVENT);
private static final Map<ConfigurationKeys.SettingCategory, List<Configuration>> CATEGORY_CONFIGURATION = collectConfigurationKeysByCategory(ConfigurationPathLevel.TICKET_CATEGORY);

private static final Predicate<ConfigurationModification> TO_BE_SAVED = c -> c.getId() > -1 || !StringUtils.isBlank(c.getValue()) || ConfigurationKeys.fromString(c.getKey()).getComponentType() == ComponentType.BOOLEAN;
private static final Predicate<ConfigurationModification> TO_BE_SAVED = c -> c.getId() > -1 || !StringUtils.isBlank(c.getValue());


private final ConfigurationRepository configurationRepository;
Expand Down Expand Up @@ -155,12 +155,14 @@ public void saveAllOrganizationConfiguration(int organizationId, List<Configurat
list.stream()
.filter(TO_BE_SAVED)
.forEach(c -> {
String value = evaluateValue(c.getKey(), c.getValue());
Optional<String> value = evaluateValue(c.getKey(), c.getValue());
Optional<Configuration> existing = configurationRepository.findByKeyAtOrganizationLevel(organizationId, c.getKey());
if (existing.isPresent()) {
configurationRepository.updateOrganizationLevel(organizationId, c.getKey(), value);
if(!value.isPresent()) {
configurationRepository.deleteOrganizationLevelByKey(c.getKey(), organizationId);
} else if (existing.isPresent()) {
configurationRepository.updateOrganizationLevel(organizationId, c.getKey(), value.get());
} else {
configurationRepository.insertOrganizationLevel(organizationId, c.getKey(), value, ConfigurationKeys.fromString(c.getKey()).getDescription());
configurationRepository.insertOrganizationLevel(organizationId, c.getKey(), value.get(), ConfigurationKeys.fromString(c.getKey()).getDescription());
}
});
}
Expand All @@ -177,11 +179,13 @@ public void saveEventConfiguration(int eventId, int organizationId, List<Configu
.filter(TO_BE_SAVED)
.forEach(c -> {
Optional<Configuration> existing = configurationRepository.findByKeyAtEventLevel(eventId, organizationId, c.getKey());
String value = evaluateValue(c.getKey(), c.getValue());
if (existing.isPresent()) {
configurationRepository.updateEventLevel(eventId, organizationId, c.getKey(), value);
Optional<String> value = evaluateValue(c.getKey(), c.getValue());
if(!value.isPresent()) {
configurationRepository.deleteEventLevelByKey(c.getKey(), eventId);
} else if (existing.isPresent()) {
configurationRepository.updateEventLevel(eventId, organizationId, c.getKey(), value.get());
} else {
configurationRepository.insertEventLevel(organizationId, eventId, c.getKey(), value, ConfigurationKeys.fromString(c.getKey()).getDescription());
configurationRepository.insertEventLevel(organizationId, eventId, c.getKey(), value.get(), ConfigurationKeys.fromString(c.getKey()).getDescription());
}
});
}
Expand All @@ -195,29 +199,48 @@ public void saveCategoryConfiguration(int categoryId, int eventId, List<Configur
.filter(TO_BE_SAVED)
.forEach(c -> {
Optional<Configuration> existing = configurationRepository.findByKeyAtCategoryLevel(eventId, event.getOrganizationId(), categoryId, c.getKey());
String value = evaluateValue(c.getKey(), c.getValue());
if (existing.isPresent()) {
configurationRepository.updateCategoryLevel(eventId, event.getOrganizationId(), categoryId, c.getKey(), value);
Optional<String> value = evaluateValue(c.getKey(), c.getValue());
if(!value.isPresent()) {
configurationRepository.deleteCategoryLevelByKey(c.getKey(), eventId, categoryId);
} else if (existing.isPresent()) {
configurationRepository.updateCategoryLevel(eventId, event.getOrganizationId(), categoryId, c.getKey(), value.get());
} else {
configurationRepository.insertTicketCategoryLevel(event.getOrganizationId(), eventId, categoryId, c.getKey(), value, ConfigurationKeys.fromString(c.getKey()).getDescription());
configurationRepository.insertTicketCategoryLevel(event.getOrganizationId(), eventId, categoryId, c.getKey(), value.get(), ConfigurationKeys.fromString(c.getKey()).getDescription());
}
});
}

private String evaluateValue(String key, String value) {
private Optional<String> evaluateValue(String key, String value) {
if(ConfigurationKeys.fromString(key).isBooleanComponentType()) {
return StringUtils.defaultString(value, "false");
return Optional.ofNullable(StringUtils.trimToNull(value));
}
return value;
return Optional.of(Objects.requireNonNull(value));
}

private Optional<Boolean> getThreeStateValue(String value) {
return Optional.ofNullable(StringUtils.trimToNull(value)).map(Boolean::parseBoolean);
}

public void saveSystemConfiguration(ConfigurationKeys key, String value) {
Optional<Configuration> conf = optionally(() -> findByConfigurationPathAndKey(Configuration.system(), key));
Optional<String> valueOpt = Optional.ofNullable(value);
if(!conf.isPresent()) {
valueOpt.ifPresent(v -> configurationRepository.insert(key.getValue(), v, key.getDescription()));
if(key.isBooleanComponentType()) {
Optional<Boolean> state = getThreeStateValue(value);
if(conf.isPresent()) {
if(state.isPresent()) {
configurationRepository.update(key.getValue(), value);
} else {
configurationRepository.deleteByKey(key.getValue());
}
} else {
state.ifPresent(v -> configurationRepository.insert(key.getValue(), v.toString(), key.getDescription()));
}
} else {
configurationRepository.update(key.getValue(), value);
Optional<String> valueOpt = Optional.ofNullable(value);
if(!conf.isPresent()) {
valueOpt.ifPresent(v -> configurationRepository.insert(key.getValue(), v, key.getDescription()));
} else {
configurationRepository.update(key.getValue(), value);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/webapp/WEB-INF/templates/admin/index.ms
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<link rel="stylesheet" href="{{request.contextPath}}/resources/bower_components/components-font-awesome/css/font-awesome.min.css" />
<link rel="stylesheet" href="{{request.contextPath}}/resources/bower_components/bootstrap-daterangepicker/daterangepicker-bs3.css" />
<link rel="stylesheet" href="{{request.contextPath}}/resources/bower_components/angular-bootstrap/ui-bootstrap-csp.css"/>
<link rel="stylesheet" href="{{request.contextPath}}/resources/bower_components/nz-toggle/dist/nz-toggle.min.css"/>
<script src="{{request.contextPath}}/resources/bower_components/jquery/dist/jquery.min.js"></script>
<script src="{{request.contextPath}}/resources/bower_components/lodash/lodash.min.js"></script>
<script src="{{request.contextPath}}/resources/bower_components/moment/min/moment-with-locales.min.js"></script>
Expand All @@ -23,6 +24,7 @@
<script src="{{request.contextPath}}/resources/bower_components/ng-file-upload/ng-file-upload.js"></script>
<script src="{{request.contextPath}}/resources/bower_components/Chart.js/Chart.min.js"></script>
<script src="{{request.contextPath}}/resources/bower_components/angular-chart.js/angular-chart.js"></script>
<script src="{{request.contextPath}}/resources/bower_components/nz-toggle/dist/nz-toggle.min.js"></script>
<script src="{{request.contextPath}}/resources/js/jsqrcode/jsqrcode.min.js"></script>
<script src="{{request.contextPath}}/resources/js/admin/ng-app/admin-application.js"></script>
<script src="{{request.contextPath}}/resources/js/admin/directive/admin-directive.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<label for="{{setting.key}}">{{setting.description}}</label>
<div data-ng-class="{'input-group': displayDelete && setting.componentType === 'TEXT' && setting.id > -1}" data-ng-switch="setting.componentType">
<input data-ng-switch-when="TEXT" class="form-control" data-ng-model="setting.value" name="{{setting.key}}" id="{{setting.key}}">
<input type="checkbox" data-ng-switch-when="BOOLEAN" data-ng-model="setting.value" data-ng-true-value="'true'" data-ng-false-value="'false'" name="{{setting.key}}" id="{{setting.key}}">
<nz-toggle tri-toggle ng-model="setting.value" val-true="'true'" val-false="'false'" val-null="null" id="{{setting.key}}" data-ng-switch-when="BOOLEAN"></nz-toggle>
<div class="input-group-btn" data-ng-if="displayDelete && setting.componentType === 'TEXT' && setting.id > -1">
<button class="btn btn-danger" data-ng-click="removeConfiguration(setting)" type="button"><i class="fa fa-trash" data-tooltip="Delete"></i></button>
</div>
Expand Down
40 changes: 40 additions & 0 deletions src/main/webapp/resources/bower_components/nz-toggle/.bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "nz-toggle",
"main": [
"dist/nz-toggle.js",
"dist/nz-toggle.css"
],
"version": "2.1.0",
"homepage": "https://github.com/nozzle/nz-toggle",
"authors": [
"tannerlinsley"
],
"description": "Double and Triple-State Toggle for AngularJS",
"keywords": [
"toggle",
"angularJS",
"indeterminate",
"checkbox",
"double",
"triple",
"state"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"_release": "2.1.0",
"_resolution": {
"type": "version",
"tag": "2.1.0",
"commit": "867979a9be91b83dad4c6aec9a975eb9917c8ca9"
},
"_source": "git://github.com/tannerlinsley/nz-toggle.git",
"_target": "~2.1.0",
"_originalSource": "nz-toggle",
"_direct": true
}
74 changes: 74 additions & 0 deletions src/main/webapp/resources/bower_components/nz-toggle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# nz-toggle

__Double and Triple-State Toggle for Angular__

## Features
- Native checkboxes are ugly, inconsistent, and involve insane CSS tweaks to style. nz-toggle is a simple directive that relieves all of the pain of styling `<input type="checkbox">`
- Indeterminate checkboxes are great for nested checklists, but emit a `false` value in that state. nz-toggle's `tri-toggle` mode fixes this by truthfully providing 3 separate value states.
- Tooltips for the tri-toggle impaired.

[Demo](http://codepen.io/anon/pen/yNjyME)

## Get Started

Install via NPM or Bower

`npm install --save nz-toggle`
`bower install --save nz-toggle`

Include Files

```html
<link rel="stylesheet" type="text/css" href=".../nz-toggle/dist/nz-toggle.min.css" />
<script type="text/javascript" src=".../nz-toggle/dist/nz-toggle.min.js"></script>
```

## Using the directive

```html
<nz-toggle
tri-toggle
on-toggle="myFunction()"
ng-model="value">
</nz-toggle>

<!-- Default Values : false-val = 0, null-val = null, true-val = true; -->
```

```html
<nz-toggle
tri-toggle
on-toggle="myFunction()"
ng-model="value"
val-true="'myString'"
val-false="0"
val-null="-1">
</nz-toggle>

```

Visit the [demo](http://codepen.io/anon/pen/yNjyME) for more usage information

## License

The MIT License (MIT)

Copyright (c) 2014 Tanner Linsley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
27 changes: 27 additions & 0 deletions src/main/webapp/resources/bower_components/nz-toggle/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "nz-toggle",
"main": ["dist/nz-toggle.js", "dist/nz-toggle.css"],
"version": "2.1.0",
"homepage": "https://github.com/nozzle/nz-toggle",
"authors": [
"tannerlinsley"
],
"description": "Double and Triple-State Toggle for AngularJS",
"keywords": [
"toggle",
"angularJS",
"indeterminate",
"checkbox",
"double",
"triple",
"state"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
Loading

0 comments on commit 705b115

Please sign in to comment.