Skip to content

Commit

Permalink
Merge pull request ManageIQ#2302 from mzazrivec/get_rid_of_static_ui_…
Browse files Browse the repository at this point in the history
…lookup_in_helpers

app helpers: get rid of ui_lookup() with static string argument
  • Loading branch information
martinpovolny authored and AparnaKarve committed Oct 9, 2017
2 parents 6a0ec02 + 9309a84 commit 2766dd7
Show file tree
Hide file tree
Showing 37 changed files with 367 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
ManageIQ.angular.app.component('customImageComponent', {
bindings: {
picture: '=',
pictureUploaded: '=',
pictureUrlPath: '@',
pictureReset: '<',
newRecord: '<',
angularForm: '<',
},
controllerAs: 'vm',
controller: customImageComponentController,
templateUrl: '/static/generic_object/custom-image-component.html.haml',
});

customImageComponentController.$inject = ['$timeout'];

function customImageComponentController($timeout) {
var vm = this;

vm.$onInit = function() {
vm.changeImage = false;
vm.imageUploadStatus = "";
};

vm.$onChanges = function() {
vm.changeImage = false;
vm.imageUploadStatus = "";
vm.angularForm.generic_object_definition_image_file_status.$setValidity("incompatibleFileType", true);
angular.element(":file").filestyle('clear');
};

vm.uploadClicked = function() {
var reader = new FileReader();
var imageFile = angular.element('#generic_object_definition_image_file')[0].files[0];
vm.imageUploadStatus = "";

if (imageFile === undefined) {
return;
}

if (imageFile.type === 'image/png') {
vm.picture.extension = 'png';
} else if (imageFile.type === 'image/jpg') {
vm.picture.extension = 'jpg';
} else if (imageFile.type === 'image/jpeg') {
vm.picture.extension = 'jpeg';
} else {
vm.angularForm.generic_object_definition_image_file_status.$setValidity("incompatibleFileType", false);
vm.imageUploadStatus = __("Incompatible image type");
return;
}

reader.onload = function(event) {
vm.picture.content = btoa(event.target.result);

$timeout(function(){
vm.angularForm.generic_object_definition_image_file_status.$setValidity("incompatibleFileType", true);
vm.imageUploadStatus = __("Image upload complete");
vm.pictureUploaded = true;
});
};

if (imageFile) {
reader.readAsBinaryString(imageFile);
}
};

vm.changeImageSelected = function() {
if (!vm.changeImage) {
vm.angularForm.generic_object_definition_image_file_status.$setValidity("incompatibleFileType", true);
vm.imageUploadStatus = "";
$(":file").filestyle('clear');
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ ManageIQ.angular.app.component('genericObjectDefinitionComponent', {
templateUrl: '/static/generic_object/generic_object_definition.html.haml',
});

genericObjectDefinitionFormController.$inject = ['API', 'miqService', '$q'];
genericObjectDefinitionFormController.$inject = ['API', 'miqService', '$q', '$timeout'];

function genericObjectDefinitionFormController(API, miqService, $q) {
function genericObjectDefinitionFormController(API, miqService, $q, $timeout) {
var vm = this;

vm.$onInit = function() {
vm.entity = __('Generic Object Class');
vm.saveable = miqService.saveable;
vm.afterGet = false;

vm.imageUploadStatus = "";
vm.imageUploaded = false;
vm.changeImage = false;

vm.pictureReset = false;

vm.attributeTableHeaders = [__("Name"), __("Type")];
vm.associationTableHeaders = [__("Name"), __("Class")];
vm.methodTableHeaders = [__("Name")];
Expand All @@ -28,6 +34,9 @@ function genericObjectDefinitionFormController(API, miqService, $q) {
vm.genericObjectDefinitionModel = {
name: '',
description: '',
picture_url_path: '',
picture: {},
pictureUpladed: false,
properties: {},
attribute_names: [],
attribute_types: [],
Expand All @@ -48,7 +57,7 @@ function genericObjectDefinitionFormController(API, miqService, $q) {
if (vm.recordId) {
vm.newRecord = false;
miqService.sparkleOn();
var dataPromise = API.get('/api/generic_object_definitions/' + vm.recordId)
var dataPromise = API.get('/api/generic_object_definitions/' + vm.recordId + '?attributes=picture_url_path')
.then(getGenericObjectDefinitionFormData)
.catch(miqService.handleFailure);

Expand Down Expand Up @@ -76,6 +85,8 @@ function genericObjectDefinitionFormController(API, miqService, $q) {

assignAllObjectsToKeyValueArrays(true);

vm.pictureReset = ! vm.pictureReset;

angularForm.$setUntouched(true);
angularForm.$setPristine(true);

Expand All @@ -92,6 +103,45 @@ function genericObjectDefinitionFormController(API, miqService, $q) {
vm.saveWithAPI('post', '/api/generic_object_definitions/', vm.prepSaveObject(), saveMsg);
};

vm.uploadClicked = function() {
var reader = new FileReader();
var imageFile = angularForm.generic_object_definition_image_file.files[0];

vm.imageUploadStatus = "";

if (imageFile.type === 'image/png') {
vm.genericObjectDefinitionModel.picture.extension = 'png';
} else if (imageFile.type === 'image/jpg') {
vm.genericObjectDefinitionModel.picture.extension = 'jpg';
} else if (imageFile.type === 'image/jpeg') {
vm.genericObjectDefinitionModel.picture.extension = 'jpeg';
} else {
// $timeout(function(){
vm.imageUploadStatus = __("Incompatible image type");
angular.element('generic_object_definition_image_file').$setValidity("incompatibleFileType", false);
return;
// });
// vm.imageUploadStatus = __("Incompatible image type");
// angularForm['generic_object_definition_image_file'].$setValidity("incompatibleFileType", false);
// return;
}

reader.onload = function(event) {
vm.genericObjectDefinitionModel.picture.content = btoa(event.target.result);

$timeout(function(){
vm.imageUploadStatus = __("Image upload complete");
});
};

if (imageFile) {
// reader.readAsDataURL(imageFile); readAsBinaryString
reader.readAsBinaryString(imageFile);
} else {
vm.imageUploadStatus = __("No file chosen");
}
};

vm.prepSaveObject = function() {
vm.genericObjectDefinitionModel.properties.attributes = {};
vm.genericObjectDefinitionModel.properties.associations = {};
Expand All @@ -109,11 +159,17 @@ function genericObjectDefinitionFormController(API, miqService, $q) {
vm.genericObjectDefinitionModel.properties.methods = vm.genericObjectDefinitionModel.method_names;
}

return {
var preppedObject = {
name: vm.genericObjectDefinitionModel.name,
description: vm.genericObjectDefinitionModel.description,
properties: vm.genericObjectDefinitionModel.properties,
};

if (vm.genericObjectDefinitionModel.picture !== {}) {
preppedObject.picture = vm.genericObjectDefinitionModel.picture;
}

return preppedObject;
};

vm.saveWithAPI = function(method, url, saveObject, saveMsg) {
Expand Down
6 changes: 2 additions & 4 deletions app/helpers/application_helper/button/collect_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ class ApplicationHelper::Button::CollectLogs < ApplicationHelper::Button::LogDep

def disabled?
@error_message = if !@record.started?
_('Cannot collect current logs unless the %{server} is started') %
{:server => ui_lookup(:table => 'miq_server')}
_('Cannot collect current logs unless the Server is started')
elsif @record.log_collection_active_recently?
_('Log collection is already in progress for this %{server}') %
{:server => ui_lookup(:table => 'miq_server')}
_('Log collection is already in progress for this Server')
elsif !@record.log_file_depot
_('Log collection requires the Log Depot settings to be configured')
end
Expand Down
3 changes: 1 addition & 2 deletions app/helpers/application_helper/button/iso_datastore_new.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class ApplicationHelper::Button::IsoDatastoreNew < ApplicationHelper::Button::ButtonNewDiscover
def disabled?
if no_ems_without_iso_datastores?
@error_message = _('No %{providers} are available to create an ISO Datastore on') %
{:providers => ui_lookup(:tables => 'ext_management_system')}
@error_message = _('No Providers are available to create an ISO Datastore on')
end
@error_message.present?
end
Expand Down
3 changes: 1 addition & 2 deletions app/helpers/application_helper/button/storage_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class ApplicationHelper::Button::StorageDelete < ApplicationHelper::Button::Basi

def disabled?
unless @record.vms_and_templates.empty? && @record.hosts.empty?
@error_message = _('Only %{storage} without VMs and Hosts can be removed') %
{:storage => ui_lookup(:table => 'storage')}
@error_message = _('Only Datastore without VMs and Hosts can be removed')
end
@error_message.present?
end
Expand Down
3 changes: 1 addition & 2 deletions app/helpers/application_helper/button/storage_perf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class ApplicationHelper::Button::StoragePerf < ApplicationHelper::Button::Basic

def disabled?
unless @record.has_perf_data?
@error_message = _('No Capacity & Utilization data has been collected for this %{storage}') %
{:storage => ui_lookup(:table => 'storage')}
@error_message = _('No Capacity & Utilization data has been collected for this Datastore')
end
@error_message.present?
end
Expand Down
6 changes: 1 addition & 5 deletions app/helpers/application_helper/button/volume_detach.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ def disabled?
if !@record.is_available?(:detach_volume)
@error_message = @record.is_available_now_error_message(:detach_volume)
elsif @record.number_of(:vms) == 0
@error_message = _("%{model} \"%{name}\" is not attached to any %{instances}") % {
:model => ui_lookup(:table => 'cloud_volume'),
:name => @record.name,
:instances => ui_lookup(:tables => 'vm_cloud')
}
@error_message = _("Cloud Volume \"%{name}\" is not attached to any Instances") % {:name => @record.name}
end
@error_message.present?
end
Expand Down
6 changes: 2 additions & 4 deletions app/helpers/application_helper/button/zone_collect_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ class ApplicationHelper::Button::ZoneCollectLogs < ApplicationHelper::Button::Lo

def disabled?
@error_message = if !@record.any_started_miq_servers?
_('Cannot collect current logs unless there are started %{servers} in the Zone') %
{:servers => ui_lookup(:tables => 'miq_servers')}
_('Cannot collect current logs unless there are started Servers in the Zone')
elsif !@record.log_file_depot
_('This Zone do not have Log Depot settings configured, collection not allowed')
elsif @record.log_collection_active_recently?
_('Log collection is already in progress for one or more %{servers} in this Zone') %
{:servers => ui_lookup(:tables => 'miq_servers')}
_('Log collection is already in progress for one or more Servers in this Zone')
end
@error_message.present?
end
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/auth_key_pair_cloud_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ def textual_fingerprint
end

def textual_vms
label = ui_lookup(:tables => "vm_cloud")
num = @record.number_of(:vms)
h = {:label => label, :icon => "pficon pficon-virtual-machine", :value => num}
h = {:label => _("Instances"), :icon => "pficon pficon-virtual-machine", :value => num}
if num > 0 && role_allows?(:feature => "vm_show_list")
h[:link] = url_for_only_path(:action => 'show', :id => @record, :display => 'instances')
h[:title] = _("Show all %{label}") % {:label => label}
h[:title] = _("Show all Instances")
end
h
end
Expand Down
10 changes: 4 additions & 6 deletions app/helpers/availability_zone_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ def textual_group_availability_zone_totals
#

def textual_cloud_volumes
label = ui_lookup(:tables => "cloud_volume")
num = @record.number_of(:cloud_volumes)
h = {:label => label, :icon => "pficon pficon-volume", :value => num}
h = {:label => _('Cloud Volumes'), :icon => "pficon pficon-volume", :value => num}
if num > 0 && role_allows?(:feature => "cloud_volume_show_list")
h[:link] = url_for_only_path(:action => 'show', :id => @record, :display => 'cloud_volumes')
h[:title] = _("Show all %{label}") % {:label => label}
h[:title] = _("Show all Cloud Volumes")
end
h
end

def textual_instances
label = ui_lookup(:tables => "vm_cloud")
num = @record.number_of(:vms)
h = {:label => label, :icon => "pficon pficon-virtual-machine", :value => num}
h = {:label => _('Instances'), :icon => "pficon pficon-virtual-machine", :value => num}
if num > 0 && role_allows?(:feature => "vm_show_list")
h[:link] = url_for_only_path(:action => 'show', :id => @record, :display => 'instances')
h[:title] = _("Show all %{label}") % {:label => label}
h[:title] = _("Show all Instances")
end
h
end
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/cloud_network_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ def textual_parent_ems_cloud
end

def textual_instances
label = ui_lookup(:tables => "vm_cloud")
num = @record.number_of(:vms)
h = {:label => label, :icon => "pficon pficon-virtual-machine", :value => num}
h = {:label => _('Instances'), :icon => "pficon pficon-virtual-machine", :value => num}
if num > 0 && role_allows?(:feature => "vm_show_list")
h[:link] = url_for_only_path(:action => 'show', :id => @record, :display => 'instances')
h[:title] = _("Show all %{label}") % {:label => label}
h[:title] = _('Show all Instances')
end
h
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ def textual_ems

def textual_cloud_tenant
cloud_tenant = @record.cloud_tenant if @record.respond_to?(:cloud_tenant)
label = ui_lookup(:table => "cloud_tenant")
h = {:label => label, :icon => "pficon pficon-cloud-tenant", :value => (cloud_tenant.nil? ? "None" : cloud_tenant.name)}
h = {:label => _('Cloud Tenant'), :icon => "pficon pficon-cloud-tenant", :value => (cloud_tenant.nil? ? "None" : cloud_tenant.name)}
if cloud_tenant && role_allows?(:feature => "cloud_tenant_show")
h[:title] = _("Show this Cloud Object Store's parent %{parent}") % {:parent => label}
h[:title] = _("Show this Cloud Object Store's parent Cloud Tenant")
h[:link] = url_for_only_path(:controller => 'cloud_tenant', :action => 'show', :id => cloud_tenant)
end
h
end

def textual_cloud_object_store_objects
label = ui_lookup(:tables => "cloud_object_store_object")
num = @record.number_of(:cloud_object_store_objects)
h = {:label => label, :icon => "ff ff-cloud-object-store", :value => num}
h = {:label => _('Cloud Object Store Objects'), :icon => "ff ff-cloud-object-store", :value => num}
if num > 0 && role_allows?(:feature => "cloud_object_store_object_show_list")
h[:title] = _("Show this Cloud Object Store's child %{children}") % {:children => label}
h[:title] = _("Show this Cloud Object Store's child Cloud Object Store Objects")
h[:link] = url_for_only_path(:action => 'show', :id => @record, :display => 'cloud_object_store_objects')
end
h
Expand Down
10 changes: 4 additions & 6 deletions app/helpers/cloud_object_store_object_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,23 @@ def textual_etag

def textual_cloud_tenant
cloud_tenant = @record.cloud_tenant if @record.respond_to?(:cloud_tenant)
label = ui_lookup(:table => "cloud_tenant")
h = {:label => label, :icon => "pficon pficon-cloud-tenant", :value => (cloud_tenant.nil? ? "None" : cloud_tenant.name)}
h = {:label => _('Cloud Tenant'), :icon => "pficon pficon-cloud-tenant", :value => (cloud_tenant.nil? ? "None" : cloud_tenant.name)}
if cloud_tenant && role_allows?(:feature => "cloud_tenant_show")
h[:title] = _("Show this Cloud Object's parent %{parent}") % {:parent => label}
h[:title] = _("Show this Cloud Object's parent Cloud Tenant")
h[:link] = url_for_only_path(:controller => 'cloud_tenant', :action => 'show', :id => cloud_tenant)
end
h
end

def textual_cloud_object_store_container
object_store_container = @record.cloud_object_store_container if @record.respond_to?(:cloud_object_store_container)
label = ui_lookup(:table => "cloud_object_store_container")
h = {
:label => label,
:label => _('Cloud Object Store Container'),
:icon => "ff ff-cloud-object-store",
:value => (object_store_container.nil? ? "None" : object_store_container.key)
}
if object_store_container && role_allows?(:feature => "cloud_object_store_container_show")
h[:title] = _("Show this Cloud Object's parent %{parent}") % {:parent => label}
h[:title] = _("Show this Cloud Object's parent Cloud Object Store Container")
h[:link] = url_for_only_path(:controller => 'cloud_object_store_container',
:action => 'show',
:id => object_store_container)
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/cloud_subnet_helper/textual_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ def textual_parent_ems_cloud
end

def textual_instances
label = ui_lookup(:tables => "vm_cloud")
num = @record.number_of(:vms)
h = {:label => label, :icon => "pficon pficon-virtual-machine", :value => num}
h = {:label => _('Instances'), :icon => "pficon pficon-virtual-machine", :value => num}
if num > 0 && role_allows?(:feature => "vm_show_list")
h[:link] = url_for_only_path(:action => 'show', :id => @record, :display => 'instances')
h[:title] = _("Show all %{label}") % {:label => label}
h[:title] = _("Show all Instances")
end
h
end
Expand Down
Loading

0 comments on commit 2766dd7

Please sign in to comment.