Skip to content

Commit

Permalink
Update default database settings to include an "all" option
Browse files Browse the repository at this point in the history
  • Loading branch information
thostetler committed Jan 24, 2024
1 parent 1eebe6a commit a7dd6ab
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 32 deletions.
24 changes: 22 additions & 2 deletions src/js/widgets/preferences/templates/application.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,30 @@
</div>
</div>
</p>
<p>
<label class="label">
<input type="checkbox" name="database" id="database_all" value="all" {{#if this.databaseALLSelected}}checked{{/if}}>
All Collections
</label>
</p>

<div class="btn-group" data-toggle="buttons" role="group" aria-labelledby="defaultDatabaseLabel">
{{#each databaseSelected}}
<label class="database-select btn btn-default {{#if this.value}} active {{/if}}">
<input type="checkbox" name="database" checked="{{this.value}}" id="database_{{this.name}}"> {{this.name}}
<label
class="database-select btn btn-default{{#if (and this.value (not ../databaseALLSelected))}} active{{/if}}"
{{#if ../databaseALLSelected}}disabled{{/if}}
>

{{#if (not ../databaseALLSelected)}}
<input
type="checkbox"
name="database"
id="database_{{this.name}}"
value="{{this.name}}"
{{#if this.value}}checked{{/if}}
/>
{{/if}}
{{this.name}}
</label> {{/each}}
</div>
</div>
Expand Down
90 changes: 60 additions & 30 deletions src/js/widgets/preferences/views/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ define([
'marionette',
'hbs!js/widgets/preferences/templates/application',
'js/widgets/config',
], function(_, Marionette, ApplicationTemplate, config) {
], function (_, Marionette, ApplicationTemplate, config) {
var DEFAULTS = {
numAuthors: {
initialOptions: _.range(1, 11).concat(['all']),
Expand Down Expand Up @@ -43,6 +43,22 @@ define([
},
};

const mergeDatabases = (databases) => {
if (
!Array.isArray(databases) ||
(Array.isArray(databases) && databases.length === 0)
) {
return DEFAULTS.database.initialValue;
}

const merged = [];
DEFAULTS.database.initialValue.forEach((database) => {
const found = databases.find((d) => d.name === database.name);
merged.push(found || database);
});
return merged;
};

const watchedProps = [
'numAuthorsSelected',
'externalLinksSelected',
Expand All @@ -52,7 +68,7 @@ define([
];

var ApplicationView = Marionette.ItemView.extend({
initialize: function() {
initialize: function () {
// Get the latest value from the incoming model, or just take the default
var numAuthors =
this.model.get('minAuthorsPerResult') ||
Expand All @@ -62,12 +78,12 @@ define([
DEFAULTS.externalLinks.initialValue;
var homePage =
this.model.get('homePage') || DEFAULTS.homePage.initialValue;
var database =
this.model.get('defaultDatabase') || DEFAULTS.database.initialValue;
var database = mergeDatabases(this.model.get('defaultDatabase'));
var hideSidebars =
this.model.get('defaultHideSidebars') ||
DEFAULTS.hideSidebars.initialValue;


// must clone the props that will get mutated
this.model.set({
numAuthorsOptions: DEFAULTS.numAuthors.initialOptions,
Expand All @@ -83,65 +99,79 @@ define([
hideSideBarsDefault: DEFAULTS.hideSidebars.initialValue,
hideSideBarsOptions: DEFAULTS.hideSidebars.initialOptions,
hideSideBarsSelected: _.clone(hideSidebars),
databaseALLSelected: data,
});
this.model.trigger('change');

this.render = _.debounce(_.bind(this.render), 280);
this.render = _.debounce(_.bind(this.render), 60);
},

template: ApplicationTemplate,

className: 'panel panel-default s-form-container',

events: {
'click .database-select': 'onDatabaseSelect',
'change .database-select': 'onDatabaseSelect',
'change #database_all': 'onDatabaseALLSelect',
'change select': 'syncModel',
},

modelEvents: {
change: 'render',
},

onDatabaseSelect: function(e) {
var data = this.model.get('databaseSelected');
onDatabaseALLSelect: function (e) {
const checked = $(e.currentTarget).prop('checked');
this.model.set('databaseALLSelected', checked);
if (checked) {
this.model.set('databaseSelected', DEFAULTS.database.initialValue);
}
this.model.trigger('change');
},

onDatabaseSelect: function (e) {
if (this.model.get('databaseALLSelected')) {
return;
}
const data = this.model.get('databaseSelected');

// find the current index of the element
var idx = $('.database-select', this.el).index(e.currentTarget);
const idx = $('.database-select', this.el).index(e.currentTarget);

// grab the object at [idx] and make our change
var newVal = _.assign({}, data[idx], {
const newVal = _.assign({}, data[idx], {
value: !data[idx].value,
});

// place our new value in the array
var newData = data
.slice(0, idx)
.concat(newVal)
.concat(data.slice(idx + 1));
const newData = data
.slice(0, idx)
.concat(newVal)
.concat(data.slice(idx + 1));
this.model.set('databaseSelected', newData);
this.model.trigger('change');
},

_convertToNumber: function(val) {
_convertToNumber: function (val) {
try {
return _.isNaN(Number(val)) ? val : Number(val);
} catch (e) {
return val;
}
},

_convertToString: function(val) {
_convertToString: function (val) {
try {
return String(val) !== '[object Object]' ? String(val) : val;
} catch (e) {
return val;
}
},

syncModel: function() {
syncModel: function () {
var update = {};
var convert = this._convertToNumber;
$('.form-control', this.el).each(function() {
$('.form-control', this.el).each(function () {
var $el = $(this);
var val = $el.val();

Expand All @@ -153,7 +183,7 @@ define([
this.model.set(update);
},

onSubmit: function() {
onSubmit: function () {
this.model.set({
updateSucceeded: false,
updateFailed: false,
Expand All @@ -162,7 +192,7 @@ define([
this.syncModel();
this.trigger('change:applicationSettings', {
minAuthorsPerResult: this._convertToString(
this.model.get('numAuthorsSelected')
this.model.get('numAuthorsSelected'),
),
externalLinkAction: this.model.get('externalLinksSelected'),
defaultDatabase: this.model.get('databaseSelected'),
Expand All @@ -172,12 +202,12 @@ define([
return false;
},

onCancel: function(e) {
onCancel: function (e) {
this.initialize();
return false;
},

onResetToDefaults: function() {
onResetToDefaults: function () {
// clear the model
this.model.set(
{
Expand All @@ -188,13 +218,13 @@ define([
},
{
unset: true,
}
},
);

this.onCancel.apply(this, arguments);
},

onError: function() {
onError: function () {
var model = this.model;
model.set({
updateFailed: true,
Expand All @@ -208,7 +238,7 @@ define([
}, 5000);
},

onSuccess: function() {
onSuccess: function () {
var model = this.model;
model.set({
updateSucceeded: true,
Expand All @@ -222,13 +252,13 @@ define([
}, 3000);
},

hideMessage: function() {
$('#app-settings-msg').fadeOut(500, function() {
hideMessage: function () {
$('#app-settings-msg').fadeOut(500, function () {
$(this).empty();
});
},

onSortChange: function(e, ui) {
onSortChange: function (e, ui) {
var items = _.clone(this.model.get('addCustomFormatOptions'));
var index = this.$('#addCustomFormat .list-group-item').index(ui.item);
var id = this.$(ui.item).data('id');
Expand All @@ -244,13 +274,13 @@ define([
this.model.set('addCustomFormatOptions', items);
},

isEditing: function() {
isEditing: function () {
return _.any(this.model.get('addCustomFormatOptions'), {
editing: true,
});
},

onRender: function() {
onRender: function () {
var onSortChange = _.bind(this.onSortChange, this);
setTimeout(() => {
$('#addCustomFormat').sortable({
Expand Down

0 comments on commit a7dd6ab

Please sign in to comment.