Skip to content

Commit

Permalink
Added support for reset admin changes back to default value
Browse files Browse the repository at this point in the history
  • Loading branch information
smpallen99 committed Apr 3, 2018
1 parent c5944b9 commit 075eaef
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 124 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# InfinityOne Changelog

## 1.0.0.beta10 (2018-04-02)
## 1.0.0.beta10 (2018-04-03)

### Enhancements

* Add InfinityOnePages package including download desktop app support
* Update the default home page default text
* Support markdown for home page text entry
* Added support for reset admin changes back to default value

### Bug Fixes

Expand Down
4 changes: 4 additions & 0 deletions assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ form.backup {
padding-bottom: 10px;
}
}

.reset-settings {
margin-left: 20px;
}
41 changes: 26 additions & 15 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,40 @@ class Admin {
save.attr('disabled', 'disabled')
$('button.discard').remove()
}
add_reset_setting_button(target, input_line) {
let data_settings = target.getAttribute('name').replace('[', '__').replace(']', '')
let reset = `<button text='Reset' data-setting="${data_settings}"
class="reset-settings button danger" rebel-click="admin_reset_setting_click">${reset_i}</button>`
if (input_line.find('button.reset-settings').length === 0) {
input_line.append(reset);

Rebel.set_event_handlers($(target).selector);
}
}
handle_change(e) {
let target = e.currentTarget;
let input_line = $(target).closest('.input-line');
this.enable_save_button()
input_line.addClass('setting-changed')
this.add_reset_setting_button(target, input_line);
}
register_events(admin) {
$('body')
.on('click', 'button.discard', function() {
.on('click', 'button.discard', () => {
// admin.disable_save_button()
$('a.admin-link[data-id="admin_info"]').click()
})
.on('change', '.admin-settings form input:not(.search)', function(e) {
let target = e.currentTarget
admin.enable_save_button()
let reset = `<button text='Reset' data-setting="${target.getAttribute('name')}" class="reset-setting button danger">${reset_i}</button>`
$(this).closest('.input-line').addClass('setting-changed') //.append(reset)
.on('change', '.admin-settings form input:not(.search)', (e) => {
this.handle_change(e);
})
.on('change', '.admin-settings form select', function(e) {
admin.enable_save_button()
$(this).closest('.input-line').addClass('setting-changed') //.append(reset)
.on('change', '.admin-settings form select', (e) => {
this.handle_change(e);
})
.on('keyup keypress paste', '.admin-settings form input:not(.search)', function(e) {
admin.enable_save_button()
$(this).closest('.input-line').addClass('setting-changed') //.append(reset)
.on('keyup keypress paste', '.admin-settings form input:not(.search)', (e) => {
this.handle_change(e);
})
.on('keyup keypress paste', '.admin-settings form textarea', function(e) {
admin.enable_save_button()
$(this).closest('.input-line').addClass('setting-changed') //.append(reset)
.on('keyup keypress paste', '.admin-settings form textarea', (e) => {
this.handle_change(e);
})
.on('change', '.permissions-manager [type="checkbox"]', function(e, t) {
e.preventDefault()
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule InfinityOne.Mixfile do
def project do
[
app: :infinity_one,
version: "1.0.0-beta9",
version: "1.0.0-beta10",
elixir: "~> 1.5",
elixirc_paths: elixirc_paths(Mix.env()),
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
Expand Down
36 changes: 35 additions & 1 deletion plugins/one_admin/lib/one_admin_web/channels/admin_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ defmodule OneAdminWeb.AdminChannel do

def admin_restart_server(socket, _sender) do
SweetAlert.swal_modal socket, ~g(Are you sure?),
~g(This will disrupt servic for all active users), "warning",
~g(This will disrupt service for all active users), "warning",
[
showCancelButton: true, closeOnConfirm: false, closeOnCancel: true,
confirmButtonColor: "#DD6B55", confirmButtonText: ~g(Yes, restart it)
Expand Down Expand Up @@ -382,6 +382,40 @@ defmodule OneAdminWeb.AdminChannel do
end
end

def admin_reset_setting_click(socket, sender) do
setting_name = sender["dataset"]["setting"]
[mod, field] = String.split(setting_name, "__", trim: true)
module = OneSettings.module_map(mod)
field = String.to_existing_atom(field)
schema = apply(module, :schema, [])
default = Map.get(schema.__struct__, field)

control_type = Rebel.Core.exec_js!(socket,
~s/let e=$('[name="#{mod}[#{field}]"]'); e.attr('type') + ' ' + e.get(0).tagName/)

selector = "#" <> String.replace(setting_name, "__", "_")

case String.split(control_type) do
[_, "TEXTAREA"] ->
Rebel.Query.update(socket, :val, set: default, on: selector)
["radio", _] ->
selector = selector <> "_"
which = if default in [true, "true"], do: "1", else: "0"
Rebel.Query.update(socket, prop: "checked", set: true, on: selector <> which)
[_, "INPUT"] ->
Rebel.Query.update(socket, :val, set: default, on: selector)
[_, "SELECT"] ->
Rebel.Query.update(socket, :val, set: default, on: selector)
other ->
raise "invalid control type: #{inspect other}"
end

socket
|> async_js(~s/OneChat.admin.enable_save_button()/)
|> async_js(~s/$('[data-setting="#{setting_name}"]').closest('.input-line').addClass('setting-changed')/)
|> async_js(~s/$('[data-setting="#{setting_name}"]').remove()/)
end

defp clear_selected_users(socket) do
async_js socket, ~s/$('.-autocomplete-container.users').addClass('hidden')/
end
Expand Down
50 changes: 42 additions & 8 deletions plugins/one_admin/lib/one_admin_web/view/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ defmodule OneAdminWeb.View.Utils do
~g"Reset section settings"
end,
content_tag :div, class: "setting-field" do
content_tag :button, class: "reset-group button danger" do
content_tag :button, class: "reset-group button danger", "rebel-click": :admin_reset_settings_click do
~g"Reset"
end
end
]
end
end

def text_input_line(f, _item, field, title, opts \\ []) do
def text_input_line(f, item, field, title, opts \\ []) do
type = opts[:type] || :text
description = opts[:description]

Expand All @@ -54,12 +54,43 @@ defmodule OneAdminWeb.View.Utils do
f
|> text_input(field, class: "input-monitor", type: type)
|> do_description(description)
end
end,
add_changed(opts, item, field)
]
end
end

def textarea_input_line(f, _item, field, title, opts \\ []) do

def add_changed(opts, item, field) do
module = item.__struct__ |> Module.split() |> List.last() |> Inflex.Underscore.underscore()
changed = opts[:changed]
if changed[field] do
content_tag :button, class: "reset-settings button danger", text: "Reset",
"data-setting": "#{module}__#{field}", "rebel-click": :admin_reset_setting_click do

content_tag :i, [class: "icon-ccw secondary-font-color color-error-contrast"], do: ""
end
else
[]
end
end

def changed_bindings(module, current) do
defaults = apply(module, :new, [])
schema = apply(module, :schema, [])

changed =
Enum.reduce(schema.__schema__(:fields), %{}, fn field, acc ->
if Map.get(defaults, field) != Map.get(current, field) do
Map.put(acc, field, true)
else
acc
end
end)
[defaults: defaults, changed: changed]
end

def textarea_input_line(f, item, field, title, opts \\ []) do
type = opts[:type] || :text
description = opts[:description]

Expand All @@ -72,7 +103,8 @@ defmodule OneAdminWeb.View.Utils do
f
|> textarea(field, class: "input-monitor", type: type)
|> do_description(description)
end
end,
add_changed(opts, item, field)
]
end
end
Expand Down Expand Up @@ -118,12 +150,13 @@ defmodule OneAdminWeb.View.Utils do
end
]
|> do_description(description)
end
end,
add_changed(opts, item, field)
]
end
end

def select_line(f, _item, field, options, title, opts \\ []) do
def select_line(f, item, field, options, title, opts \\ []) do
description = opts[:description]
content_tag :div, class: "input-line double-col" do
[
Expand All @@ -139,7 +172,8 @@ defmodule OneAdminWeb.View.Utils do
select(f, field, options)
]
|> do_description(description)
end
end,
add_changed(opts, item, field)
]
end
end
Expand Down
6 changes: 4 additions & 2 deletions plugins/one_chat/lib/one_chat_web/admin/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule OneChatWeb.Admin.Page.Accounts do
use OneAdmin.Page

alias InfinityOne.{Repo, Hooks, Settings.Accounts}
alias OneAdminWeb.View.Utils

def add_page do
new(
Expand All @@ -17,11 +18,12 @@ defmodule OneChatWeb.Admin.Page.Accounts do
end

def args(page, user, _sender, socket) do
accounts = Accounts.get()
{[
user: Repo.preload(user, Hooks.user_preload([])),
changeset: Accounts.get() |> Accounts.changeset()
changeset: accounts |> Accounts.changeset()
|> Hooks.all_users_post_filter,
], user, page, socket}
] ++ Utils.changed_bindings(Accounts, accounts), user, page, socket}
end

def check_perissions(_page, user) do
Expand Down
6 changes: 4 additions & 2 deletions plugins/one_chat/lib/one_chat_web/admin/chat_general.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule OneChatWeb.Admin.Page.ChatGeneral do

alias InfinityOne.{Repo, Hooks}
alias OneChat.Settings.ChatGeneral
alias OneAdminWeb.View.Utils

@doc """
Callback to add the ChatGeneral page into the administration pages.
Expand All @@ -27,10 +28,11 @@ defmodule OneChatWeb.Admin.Page.ChatGeneral do
Callback to provide the ChatGeneral page rendering bindings.
"""
def args(page, user, _sender, socket) do
general = ChatGeneral.get()
{[
user: Repo.preload(user, Hooks.user_preload([])),
changeset: ChatGeneral.get |> ChatGeneral.changeset,
], user, page, socket}
changeset: general |> ChatGeneral.changeset(),
] ++ Utils.changed_bindings(ChatGeneral, general), user, page, socket}
end

@doc """
Expand Down
6 changes: 4 additions & 2 deletions plugins/one_chat/lib/one_chat_web/admin/general.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule OneChatWeb.Admin.Page.General do
use OneAdmin.Page

alias InfinityOne.{Repo, Hooks, Settings.General}
alias OneAdminWeb.View.Utils

def add_page do
new(
Expand All @@ -17,11 +18,12 @@ defmodule OneChatWeb.Admin.Page.General do
end

def args(page, user, _sender, socket) do
general = General.get()
{[
user: Repo.preload(user, Hooks.user_preload([])),
changeset: General.get() |> General.changeset()
changeset: general |> General.changeset()
|> Hooks.all_users_post_filter,
], user, page, socket}
] ++ Utils.changed_bindings(General, general), user, page, socket}
end

def check_perissions(_page, user) do
Expand Down
6 changes: 4 additions & 2 deletions plugins/one_chat/lib/one_chat_web/admin/layout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule OneChatWeb.Admin.Page.Layout do

alias InfinityOne.{Repo, Hooks}
alias OneChat.Settings.Layout
alias OneAdminWeb.View.Utils

def add_page do
new(
Expand All @@ -18,10 +19,11 @@ defmodule OneChatWeb.Admin.Page.Layout do
end

def args(page, user, _sender, socket) do
layout = Layout.get()
{[
user: Repo.preload(user, Hooks.user_preload([])),
changeset: Layout.get |> Layout.changeset,
], user, page, socket}
changeset: layout |> Layout.changeset,
] ++ Utils.changed_bindings(Layout, layout), user, page, socket}
end

def check_perissions(_page, user) do
Expand Down
6 changes: 4 additions & 2 deletions plugins/one_chat/lib/one_chat_web/admin/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule OneChatWeb.Admin.Page.Message do

alias InfinityOne.{Repo, Hooks}
alias OneChat.Settings.Message
alias OneAdminWeb.View.Utils

def add_page do
new(
Expand All @@ -18,11 +19,12 @@ defmodule OneChatWeb.Admin.Page.Message do
end

def args(page, user, _sender, socket) do
message = Message.get()
{[
user: Repo.preload(user, Hooks.user_preload([])),
changeset: Message.get |> Message.changeset,
changeset: message |> Message.changeset(),
message_opts: OneChatWeb.MessageView.message_opts()
], user, page, socket}
] ++ Utils.changed_bindings(Message, message), user, page, socket}
end

def check_perissions(_page, user) do
Expand Down
Loading

0 comments on commit 075eaef

Please sign in to comment.