Revision 1d7dcde3
Added by Tomer Brisker over 7 years ago
app/assets/javascripts/trends.js | ||
---|---|---|
$(function() {
|
||
trendTypeSelected($("[id$='trend_trendable_type']"))
|
||
});
|
||
|
||
|
||
function trendTypeSelected(item){
|
||
var is_fact = ($(item).val() == "FactName");
|
||
var edit_mode = $(item).attr('disabled');
|
||
$("[id$='trend_trendable_id']").attr('disabled', (is_fact && !edit_mode) ? null : 'disabled');
|
||
$("[id$='trend_name']").attr('disabled', (is_fact && !edit_mode) ? null : 'disabled');
|
||
if (!is_fact || !edit_mode) {
|
||
$("[id$='trend_trendable_id']").val('');
|
||
$("[id$='trend_name']").val('');
|
||
}
|
||
}
|
app/helpers/trends_helper.rb | ||
---|---|---|
module TrendsHelper
|
||
include CommonParametersHelper
|
||
|
||
def trendable_types(new_record)
|
||
def trendable_types
|
||
options = {_('Environment') => 'Environment', _('Operating system') => 'Operatingsystem',
|
||
_('Model') => 'Model', _('Facts') =>'FactName',_('Host group') => 'Hostgroup', _('Compute resource') => 'ComputeResource'}
|
||
if new_record
|
||
existing = ForemanTrend.includes(:trendable).types.map(&:to_s)
|
||
options.delete_if{ |k,v| existing.include?(v) }
|
||
end
|
||
options
|
||
existing = ForemanTrend.types.pluck(:trendable_type)
|
||
options.delete_if{ |k,v| existing.include?(v) }
|
||
end
|
||
|
||
def trend_days_filter
|
app/views/trends/_form.html.erb | ||
---|---|---|
<%= javascript 'trends' %>
|
||
<%= form_for @trend do |f| %>
|
||
<%= base_errors_for @trend %>
|
||
<% new_record = @trend.new_record? %>
|
||
<%= selectable_f f, :trendable_type, trendable_types(new_record), { }, :place_holder => _('Please Select'), :onchange => 'trendTypeSelected(this)', :disabled => !new_record %>
|
||
<%= select_f(f, :trendable_id, FactName.no_timestamp_fact, :id, :name, { :include_blank => true }, :disabled => true) %>
|
||
<%= text_f f, :name %>
|
||
<% types = trendable_types %>
|
||
<%= selectable_f f, :trendable_type, types, { }, :place_holder => _('Please Select'), :onchange => 'tfm.trends.trendTypeSelected(this)' %>
|
||
<%= select_f(f, :trendable_id, FactName.no_timestamp_fact, :id, :name, { :include_blank => true }, :disabled => types.first[1] != 'FactName') %>
|
||
<%= text_f f, :name, :disabled => types.first[1] != 'FactName' %>
|
||
<%= submit_or_cancel f %>
|
||
<% end %>
|
app/views/trends/edit.html.erb | ||
---|---|---|
<%= javascript 'trends' %>
|
||
<% title _("Edit Trend %s") % @trend.to_label %>
|
||
<div class="col-md-6">
|
||
<%= form_tag trend_path, :method => :put do %>
|
webpack/assets/javascripts/bundle.js | ||
---|---|---|
{
|
||
tools: require('./foreman_tools'),
|
||
users: require('./foreman_users'),
|
||
trends: require('./foreman_trends'),
|
||
numFields: require('./jquery.ui.custom_spinners'),
|
||
reactMounter: require('./react_app/common/MountingService')
|
||
}
|
webpack/assets/javascripts/foreman_trends.js | ||
---|---|---|
import $ from 'jquery';
|
||
|
||
export function trendTypeSelected({value}) {
|
||
$('#trend_trendable_id, #trend_name').attr('disabled', value !== 'FactName').val('');
|
||
}
|
webpack/assets/javascripts/foreman_trends.test.js | ||
---|---|---|
jest.unmock('./foreman_trends');
|
||
window.trends = require('./foreman_trends');
|
||
const $ = require('jquery');
|
||
|
||
describe('selecting trend type', () => {
|
||
it('should disable fields on non-fact trend', () => {
|
||
document.body.innerHTML =
|
||
`<select id="trendable_type" onchange="trends.trendTypeSelected(this)">
|
||
<option value="FactName">Facts</option>
|
||
<option value="Hostgroup">Host group</option>
|
||
</select>
|
||
<select id="trend_trendable_id">
|
||
<option value=""></option>
|
||
<option value="27">architecture</option>
|
||
</select>
|
||
<input id="trend_name">`;
|
||
$('#trendable_type').val('Hostgroup').change();
|
||
expect($('#trend_trendable_id').is(':disabled')).toBeTruthy();
|
||
expect($('#trend_name').is(':disabled')).toBeTruthy();
|
||
});
|
||
|
||
it('should enable fields on non-fact trend', () => {
|
||
document.body.innerHTML =
|
||
`<select id="trendable_type" onchange="trends.trendTypeSelected(this)">
|
||
<option value="Evironment">Environment</option>
|
||
<option value="FactName">Facts</option>
|
||
</select>
|
||
<select id="trend_trendable_id" disabled>
|
||
<option value=""></option>
|
||
<option value="27">architecture</option>
|
||
</select>
|
||
<input id="trend_name" disabled>`;
|
||
$('#trendable_type').val('FactName').change();
|
||
expect($('#trend_trendable_id').is(':disabled')).toBeFalsy();
|
||
expect($('#trend_name').is(':disabled')).toBeFalsy();
|
||
});
|
||
|
||
});
|
Also available in: Unified diff
Fixes #17972 - Simplify new trend form js and helpers
Migrated the js to webpack as well.