-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Js shakapacker and Boostrap confirm (#140)
* Manage js in app/javascript/app_views * Pull most javascript from html to compile with webpacker * Override default confirm dialog with a bootstrap modal * Remove useless fields for confirm dialog
- Loading branch information
Showing
18 changed files
with
312 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import $ from 'jquery'; | ||
|
||
class AnalysesIndex { | ||
constructor(relRoot, apiKey, userId) { | ||
this.relRoot = relRoot; | ||
this.apiKey = apiKey; | ||
this.userId = userId; | ||
} | ||
|
||
start() { | ||
const { relRoot, apiKey, userId } = this; | ||
const SPINNER_TIMEOUT = 1500; // ms | ||
function setAnalysesListSettings() { | ||
const query = $(this).data('analyses-query'); | ||
const order = $(this).data('analyses-order'); | ||
let timeout; | ||
$.ajax({ | ||
type: 'PATCH', | ||
xhrFields: { withCredentials: true }, | ||
headers: { Authorization: `Token ${apiKey}` }, | ||
url: `${relRoot}/api/v1/users/${userId}`, | ||
data: { user: { settings: { analyses_query: query, analyses_order: order } } }, | ||
beforeSend() { | ||
timeout = setTimeout(() => { | ||
$('.spinner').show(); | ||
}, SPINNER_TIMEOUT); | ||
}, | ||
success() { | ||
$.getScript(this.href, () => { | ||
if (timeout) { clearTimeout(timeout); } | ||
$('.spinner').hide(); | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
$('input[data-analyses-query]').on('click', setAnalysesListSettings); | ||
$('input[data-analyses-order]').on('click', setAnalysesListSettings); | ||
|
||
let current_design_project_id = '<%= current_user.analyses_scope_design_project_id %>'; | ||
|
||
$('#designProjectScope').on('click', function (e) { | ||
const design_project_id = this.value; | ||
let timeout; | ||
if (design_project_id !== current_design_project_id) { | ||
$.ajax({ | ||
type: 'PATCH', | ||
xhrFields: { withCredentials: true }, | ||
headers: { Authorization: `Token ${apiKey}` }, | ||
url: `${relRoot}/api/v1/users/${userId}`, | ||
data: { user: { settings: { analyses_scope_design_project_id: design_project_id } } }, | ||
beforeSend() { | ||
timeout = setTimeout(() => { | ||
$('.spinner').show(); | ||
}, SPINNER_TIMEOUT); | ||
}, | ||
success() { | ||
$.getScript(this.href, () => { | ||
if (timeout) { clearTimeout(timeout); console.log('CLEAR'); } | ||
$('.spinner').hide(); | ||
}); | ||
}, | ||
}).always(() => { | ||
current_design_project_id = design_project_id; | ||
}); | ||
} | ||
}); | ||
|
||
// hide spinner | ||
$('.spinner').hide(); | ||
} | ||
} | ||
|
||
export default AnalysesIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import $ from 'jquery'; | ||
|
||
class OperationsIndex { | ||
constructor(relRoot) { | ||
this.relRoot = relRoot; | ||
} | ||
|
||
start() { | ||
const relativeUrlRoot = this.relRoot; | ||
const opeEdit = {}; | ||
const $modal = $('#editModal'); | ||
const $input = $("input[name='operation[name]']"); | ||
|
||
$modal.on('show.bs.modal', (e) => { | ||
opeEdit.invoker = $(e.relatedTarget); | ||
opeEdit.id = opeEdit.invoker.data('ope-id'); | ||
opeEdit.name = opeEdit.invoker.data('ope-name'); | ||
opeEdit.apiKey = opeEdit.invoker.data('api-key'); | ||
$input.val(opeEdit.name); | ||
$input.on('focus'); | ||
}); | ||
|
||
$('button[data-save="true"]').on( | ||
'click', | ||
() => { | ||
const newName = $input.val(); | ||
$.ajax({ | ||
type: 'PATCH', | ||
xhrFields: { withCredentials: true }, | ||
headers: { Authorization: `Token ${opeEdit.apiKey}` }, | ||
url: `${relativeUrlRoot}/api/v1/operations/${opeEdit.id}`, | ||
data: { operation: { name: newName } }, | ||
success() { | ||
$(`a[id=${opeEdit.id}]`).text(newName); | ||
opeEdit.invoker.data('ope-name', newName); | ||
$modal.modal('hide'); | ||
}, | ||
error(xhr) { | ||
console.log(xhr.responseJSON.message); | ||
$('#errorPlaceHolder').html( | ||
`<div class="alert bg-warning" role="alert"><a href="#" data-dismiss="alert" class="close">×</a>${ | ||
xhr.responseJSON.message | ||
}</div>`, | ||
); | ||
$modal.modal('hide'); | ||
}, | ||
}); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
export default OperationsIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import $ from 'jquery'; | ||
import { Tooltip } from 'bootstrap'; | ||
|
||
class OptimizationsIndex { | ||
start() { | ||
$('#need_selected').hide(); | ||
|
||
// Initialize tooltips BS5 | ||
const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')); | ||
const tooltipList = tooltipTriggerList.map((triggerEl) => new Tooltip(triggerEl)); | ||
|
||
$('#selectAll').change(function () { | ||
checked = this.checked; | ||
|
||
$('.checkbox_child').map(function () { | ||
$(this).prop('checked', checked); | ||
}); | ||
|
||
$('#selectStatus').prop('checked', checked); | ||
update_checkbox_count(); | ||
}); | ||
|
||
$('#selectStatus').change(function () { | ||
checked = this.checked; | ||
status = $('#status-select').val(); | ||
|
||
$('.checkbox_child').map(function () { | ||
if (~$(this).attr('class').indexOf(status)) { | ||
$(this).prop('checked', checked); | ||
} | ||
}); | ||
update_checkbox_count(); | ||
}); | ||
|
||
$('#status-select').change(() => { | ||
$('#selectStatus').prop('checked', false); | ||
}); | ||
|
||
$('.checkbox_child').change(() => { update_checkbox_count(); }); | ||
|
||
function update_checkbox_count() { | ||
$('#display_checkbox_count').text(($('.checkbox_child:checked').length === 0) ? `0/${$('.checkbox_child').length}` : `${$('.checkbox_child:checked').length}/${$('.checkbox_child').length}`); | ||
|
||
$('.checkbox_child').map(function () { | ||
$(this).parent().parent().removeClass((this.checked) ? '' : 'table-primary') | ||
.addClass((this.checked) ? 'table-primary' : ''); | ||
}); | ||
|
||
if ($('.checkbox_child:checked').length == $('.checkbox_child').length) { | ||
$('#selectAll').prop('checked', true); | ||
} else { | ||
$('#selectAll').prop('checked', false); | ||
} | ||
|
||
if ($('.checkbox_child:checked').length > 0) { | ||
$('#need_selected').show(); | ||
} else { | ||
$('#need_selected').hide(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default OptimizationsIndex; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import $ from 'jquery'; | ||
|
||
class UserShow { | ||
constructor(relRoot, userId) { | ||
this.relRoot = relRoot; | ||
this.userId = userId; | ||
} | ||
|
||
start() { | ||
const { relRoot, userId } = this; | ||
$('#reset-api-key').on('click', () => { | ||
const api_key = $('#user-api-key').text(); | ||
$.ajax({ | ||
type: 'PATCH', | ||
xhrFields: { | ||
withCredentials: true, | ||
}, | ||
headers: { | ||
Authorization: `Token ${api_key}`, | ||
}, | ||
url: `${relRoot}/api/v1/users/${userId}/api_key`, | ||
success() { | ||
$.getScript(this.href); | ||
return false; | ||
}, | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default UserShow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
import Rails from '@rails/ujs'; | ||
import AnalysesIndex from '../application_views/analyses_index'; | ||
import OperationsIndex from '../application_views/operations_index'; | ||
import OptimizationsIndex from '../application_views/optimizations_index'; | ||
import UserShow from '../application_views/users_show'; | ||
|
||
// Views are used in corresponding views/**/*.html.erb files | ||
// in document_ready section. | ||
// Calling start() allow to attach js callbacks to DOM element | ||
// specially for each views. | ||
window.App = { | ||
AnalysesIndex, | ||
OperationsIndex, | ||
OptimizationsIndex, | ||
UserShow, | ||
}; | ||
|
||
// Override default browser confirm dialog by bootstrap version | ||
Rails.confirm = (message, element) => { | ||
const $element = $(element); | ||
const $dialog = $('#confirmModal'); | ||
const $content = $dialog.find('#modal-content'); | ||
const $ok = $dialog.find('#ok-button'); | ||
$content.text(message); | ||
$ok.on('click', (event) => { | ||
event.preventDefault(); | ||
$dialog.modal('hide'); | ||
const old = Rails.confirm; | ||
Rails.confirm = () => true; | ||
$element.get(0).click(); | ||
Rails.confirm = old; | ||
}); | ||
$dialog.modal('show'); | ||
return false; | ||
}; | ||
|
||
Rails.start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.