From fc49f3bf2bbef255497b4c918b1706ca9afa56b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sun, 4 Sep 2022 23:05:16 +0200 Subject: [PATCH] Split server mods list into two pane layout --- public/js/app/collections/server_mods.js | 10 ++++ public/js/app/models/server_mod.js | 11 ++++ .../app/views/servers/mods/available/list.js | 16 ++++++ .../views/servers/mods/available/list_item.js | 33 ++++++++++++ public/js/app/views/servers/mods/index.js | 54 +++++++++++++++++-- public/js/app/views/servers/mods/list.js | 46 ---------------- public/js/app/views/servers/mods/list_item.js | 41 -------------- .../app/views/servers/mods/selected/list.js | 21 ++++++++ .../views/servers/mods/selected/list_item.js | 27 ++++++++++ .../servers/mods/{ => available}/list.html | 3 +- .../tpl/servers/mods/available/list_item.html | 18 +++++++ public/js/tpl/servers/mods/index.html | 20 ++++--- public/js/tpl/servers/mods/list_item.html | 15 ------ public/js/tpl/servers/mods/selected/list.html | 18 +++++++ .../tpl/servers/mods/selected/list_item.html | 19 +++++++ 15 files changed, 236 insertions(+), 116 deletions(-) create mode 100644 public/js/app/collections/server_mods.js create mode 100644 public/js/app/models/server_mod.js create mode 100644 public/js/app/views/servers/mods/available/list.js create mode 100644 public/js/app/views/servers/mods/available/list_item.js delete mode 100644 public/js/app/views/servers/mods/list.js delete mode 100644 public/js/app/views/servers/mods/list_item.js create mode 100644 public/js/app/views/servers/mods/selected/list.js create mode 100644 public/js/app/views/servers/mods/selected/list_item.js rename public/js/tpl/servers/mods/{ => available}/list.html (58%) create mode 100644 public/js/tpl/servers/mods/available/list_item.html delete mode 100644 public/js/tpl/servers/mods/list_item.html create mode 100644 public/js/tpl/servers/mods/selected/list.html create mode 100644 public/js/tpl/servers/mods/selected/list_item.html diff --git a/public/js/app/collections/server_mods.js b/public/js/app/collections/server_mods.js new file mode 100644 index 0000000..abd3f32 --- /dev/null +++ b/public/js/app/collections/server_mods.js @@ -0,0 +1,10 @@ +var Backbone = require('backbone') + +var ServerMod = require('app/models/server_mod') + +module.exports = Backbone.Collection.extend({ + comparator: function (a, b) { + return a.get('name').toLowerCase().localeCompare(b.get('name').toLowerCase()) + }, + model: ServerMod +}) diff --git a/public/js/app/models/server_mod.js b/public/js/app/models/server_mod.js new file mode 100644 index 0000000..95e0eab --- /dev/null +++ b/public/js/app/models/server_mod.js @@ -0,0 +1,11 @@ +var Backbone = require('backbone') + +module.exports = Backbone.Model.extend({ + defaults: { + name: '' + }, + idAttribute: 'name', + isNew: function () { + return true + } +}) diff --git a/public/js/app/views/servers/mods/available/list.js b/public/js/app/views/servers/mods/available/list.js new file mode 100644 index 0000000..def668f --- /dev/null +++ b/public/js/app/views/servers/mods/available/list.js @@ -0,0 +1,16 @@ +var _ = require('underscore') + +var ModsListView = require('app/views/mods/list') +var ListItemView = require('app/views/servers/mods/available/list_item') +var tpl = require('tpl/servers/mods/available/list.html') + +module.exports = ModsListView.extend({ + childView: ListItemView, + template: _.template(tpl), + + buildChildView: function (item, ChildViewType, childViewOptions) { + var options = _.extend({ model: item, selectedModsCollection: this.options.selectedModsCollection }, childViewOptions) + var view = new ChildViewType(options) + return view + } +}) diff --git a/public/js/app/views/servers/mods/available/list_item.js b/public/js/app/views/servers/mods/available/list_item.js new file mode 100644 index 0000000..36d1b54 --- /dev/null +++ b/public/js/app/views/servers/mods/available/list_item.js @@ -0,0 +1,33 @@ +var _ = require('underscore') + +var ServerMod = require('app/models/server_mod') +var ModListItemView = require('app/views/mods/list_item') +var tpl = require('tpl/servers/mods/available/list_item.html') + +var template = _.template(tpl) + +module.exports = ModListItemView.extend({ + template: template, + templateHelpers: function () { + var superTemplateHelpers = ModListItemView.prototype.templateHelpers.call(this) + var name = this.model.get('name') + var modSelected = this.options.selectedModsCollection.get(name) + + return Object.assign({}, superTemplateHelpers, { + isDisabledButton: function () { + return modSelected ? 'disabled' : '' + } + }) + }, + + events: { + 'click .add-mod': 'addMod' + }, + + addMod: function (e) { + e.preventDefault() + this.options.selectedModsCollection.add(new ServerMod({ + name: this.model.get('name') + })) + } +}) diff --git a/public/js/app/views/servers/mods/index.js b/public/js/app/views/servers/mods/index.js index 6b4482d..07bfaa4 100644 --- a/public/js/app/views/servers/mods/index.js +++ b/public/js/app/views/servers/mods/index.js @@ -1,7 +1,9 @@ var _ = require('underscore') +var ServerMods = require('app/collections/server_mods') var ModsView = require('app/views/mods/index') -var ListView = require('app/views/servers/mods/list') +var AvailableModsListView = require('app/views/servers/mods/available/list') +var SelectedModsListView = require('app/views/servers/mods/selected/list') var tpl = require('tpl/servers/mods/index.html') var template = _.template(tpl) @@ -9,24 +11,66 @@ var template = _.template(tpl) module.exports = ModsView.extend({ template: template, + regions: { + availableView: '#available', + selectedView: '#selected' + }, + modelEvents: { change: 'serverUpdated' }, initialize: function (options) { ModsView.prototype.initialize.call(this, options) - this.modsListView = new ListView({ + + this.selectedModsCollection = new ServerMods(this.serverMods()) + + this.availableModsListView = new AvailableModsListView({ collection: this.options.mods, - server: this.options.server, + selectedModsCollection: this.selectedModsCollection, filterValue: this.filterValue }) + this.selectedModsListView = new SelectedModsListView({ + collection: this.selectedModsCollection + }) + + this.listenTo(this.selectedModsCollection, 'update', this.availableModsListView.render) + }, + + updateFilter: function (event) { + this.filterValue = event.target.value + + this.availableModsListView.filterValue = this.filterValue + this.availableModsListView.render() + this.selectedModsListView.filterValue = this.filterValue + this.selectedModsListView.render() + }, + + onRender: function () { + this.availableView.show(this.availableModsListView) + this.selectedView.show(this.selectedModsListView) + }, + + serverMods: function () { + return this.model.get('mods') + .map(function (mod) { + return { + name: mod + } + }) }, serverUpdated: function () { - this.modsListView.render() + this.selectedModsCollection.set(this.serverMods()) }, serialize: function () { - return this.modsListView.serialize() + this.selectedModsCollection.sort() + return { + mods: this.selectedModsCollection.toJSON() + .map(function (mod) { + return mod.name + }) + } } }) diff --git a/public/js/app/views/servers/mods/list.js b/public/js/app/views/servers/mods/list.js deleted file mode 100644 index 9e261a7..0000000 --- a/public/js/app/views/servers/mods/list.js +++ /dev/null @@ -1,46 +0,0 @@ -var $ = require('jquery') -var _ = require('underscore') - -var ModsListView = require('app/views/mods/list') -var ListItemView = require('app/views/servers/mods/list_item') -var tpl = require('tpl/servers/mods/list.html') - -module.exports = ModsListView.extend({ - childView: ListItemView, - template: _.template(tpl), - - events: { - 'click .check-all': 'checkAll', - 'click .uncheck-all': 'uncheckAll' - }, - - buildChildView: function (item, ChildViewType, childViewOptions) { - var options = _.extend({ model: item, server: this.options.server }, childViewOptions) - var view = new ChildViewType(options) - return view - }, - - changeAllCheckbox: function (checked) { - this.$('input:checkbox').map(function (idx, el) { - return $(el).prop('checked', checked) - }) - }, - - checkAll: function (e) { - e.preventDefault() - this.changeAllCheckbox(true) - }, - - uncheckAll: function (e) { - e.preventDefault() - this.changeAllCheckbox(false) - }, - - serialize: function () { - return { - mods: this.$('input:checkbox:checked').map(function (idx, el) { - return $(el).val() - }).get() - } - } -}) diff --git a/public/js/app/views/servers/mods/list_item.js b/public/js/app/views/servers/mods/list_item.js deleted file mode 100644 index 944b4ba..0000000 --- a/public/js/app/views/servers/mods/list_item.js +++ /dev/null @@ -1,41 +0,0 @@ -var _ = require('underscore') - -var ModListItemView = require('app/views/mods/list_item') -var tpl = require('tpl/servers/mods/list_item.html') - -var template = _.template(tpl) - -module.exports = ModListItemView.extend({ - tagName: 'tr', - template: template, - - templateHelpers: function () { - var modFile = this.model.get('modFile') - var name = this.model.get('name') - var steamMeta = this.model.get('steamMeta') - - var enabled = this.options.server.get('mods').indexOf(name) > -1 - var link = null - var title = null - - if (steamMeta && steamMeta.id) { - if (steamMeta.id) { - link = 'https://steamcommunity.com/sharedfiles/filedetails/?id=' + steamMeta.id - } - - if (steamMeta.name) { - title = steamMeta.name - } - } - - if (modFile && modFile.name) { - title = modFile.name - } - - return { - enabled: enabled, - link: link, - title: title - } - } -}) diff --git a/public/js/app/views/servers/mods/selected/list.js b/public/js/app/views/servers/mods/selected/list.js new file mode 100644 index 0000000..4a8bc5c --- /dev/null +++ b/public/js/app/views/servers/mods/selected/list.js @@ -0,0 +1,21 @@ +var _ = require('underscore') +var Marionette = require('marionette') + +var ServerMod = require('app/models/server_mod') +var ListItemView = require('app/views/servers/mods/selected/list_item') +var tpl = require('tpl/servers/mods/selected/list.html') + +module.exports = Marionette.CompositeView.extend({ + childView: ListItemView, + childViewContainer: 'tbody', + template: _.template(tpl), + + events: { + 'click .add-mod': 'addMod' + }, + + addMod: function (e) { + e.preventDefault() + this.collection.add(new ServerMod({ name: 'Unlisted' })) + } +}) diff --git a/public/js/app/views/servers/mods/selected/list_item.js b/public/js/app/views/servers/mods/selected/list_item.js new file mode 100644 index 0000000..9e96fa1 --- /dev/null +++ b/public/js/app/views/servers/mods/selected/list_item.js @@ -0,0 +1,27 @@ +var $ = require('jquery') +var _ = require('underscore') + +var ModListItemView = require('app/views/mods/list_item') +var tpl = require('tpl/servers/mods/selected/list_item.html') + +var template = _.template(tpl) + +module.exports = ModListItemView.extend({ + template: template, + + events: { + 'click button.delete': 'delete', + 'change select#difficulty': 'changed', + 'change input#name': 'changed' + }, + + changed: function (e) { + var val = $(e.target).val() + this.model.set(e.target.id, val) + }, + + delete: function (e) { + e.preventDefault() + this.model.destroy() + } +}) diff --git a/public/js/tpl/servers/mods/list.html b/public/js/tpl/servers/mods/available/list.html similarity index 58% rename from public/js/tpl/servers/mods/list.html rename to public/js/tpl/servers/mods/available/list.html index f87f3bb..526edb4 100644 --- a/public/js/tpl/servers/mods/list.html +++ b/public/js/tpl/servers/mods/available/list.html @@ -1,10 +1,9 @@
- - + diff --git a/public/js/tpl/servers/mods/available/list_item.html b/public/js/tpl/servers/mods/available/list_item.html new file mode 100644 index 0000000..8d1c6f0 --- /dev/null +++ b/public/js/tpl/servers/mods/available/list_item.html @@ -0,0 +1,18 @@ + + diff --git a/public/js/tpl/servers/mods/index.html b/public/js/tpl/servers/mods/index.html index 6a5524e..49c1c81 100644 --- a/public/js/tpl/servers/mods/index.html +++ b/public/js/tpl/servers/mods/index.html @@ -1,8 +1,14 @@ - -
- - -
- +
+
+
+
+ + +
+ -
+
+
+
+
+
diff --git a/public/js/tpl/servers/mods/list_item.html b/public/js/tpl/servers/mods/list_item.html deleted file mode 100644 index 9f17815..0000000 --- a/public/js/tpl/servers/mods/list_item.html +++ /dev/null @@ -1,15 +0,0 @@ -
diff --git a/public/js/tpl/servers/mods/selected/list.html b/public/js/tpl/servers/mods/selected/list.html new file mode 100644 index 0000000..a565b0b --- /dev/null +++ b/public/js/tpl/servers/mods/selected/list.html @@ -0,0 +1,18 @@ + +
Mod
+ <%-name%> + <% if (link) { %> + + <%-title%> + + <% } else if (title) { %> + <%-title%> + <% } %> + + > + + Add + + +
+
-
- -
-
+ + + + + + + + + +
Selected Mods
+ + + + + Add unlisted mod + diff --git a/public/js/tpl/servers/mods/selected/list_item.html b/public/js/tpl/servers/mods/selected/list_item.html new file mode 100644 index 0000000..2e7234a --- /dev/null +++ b/public/js/tpl/servers/mods/selected/list_item.html @@ -0,0 +1,19 @@ + +
+
+ +
+ +
+
+ +
+
+ +
+
+
+