Class: Mxrb::Compiler::ImageBundleCompiler
- Inherits:
-
Object
- Object
- Mxrb::Compiler::ImageBundleCompiler
- Includes:
- ModelValues
- Defined in:
- lib/mxrb/compiler/image_bundle_compiler.rb
Overview
Compiles the static-image subset of the official React Image widget. rubocop:disable Metrics
Constant Summary collapse
- WIDGET_ID =
'com.mendix.widget.web.image.Image'
Class Method Summary collapse
-
.expression(value) ⇒ Object
rubocop:enable Metrics/MethodLength.
- .javascript(value) ⇒ Object
- .number(value, fallback) ⇒ Object
- .raw(value) ⇒ Object
-
.render_static(key, css_class, uri, options) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .unit(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(source, page_name, widget, scope: nil, entity: nil, key_prefix: 'p', action_property: nil) ⇒ ImageBundleCompiler
constructor
A new instance of ImageBundleCompiler.
-
#render ⇒ Object
rubocop:disable Metrics/AbcSize.
- #supported? ⇒ Boolean
Constructor Details
#initialize(source, page_name, widget, scope: nil, entity: nil, key_prefix: 'p', action_property: nil) ⇒ ImageBundleCompiler
Returns a new instance of ImageBundleCompiler.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 46 def initialize(source, page_name, , scope: nil, entity: nil, key_prefix: 'p', action_property: nil) @source = source @page_name = page_name @widget = @scope = scope @entity = entity @key_prefix = key_prefix @action_property = action_property @index = document_index @values = property_values(['Object']) end |
Class Method Details
.expression(value) ⇒ Object
rubocop:enable Metrics/MethodLength
28 29 30 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 28 def self.expression(value) "ExpressionProperty({ expression: { expr: { type: \"literal\", value: #{JSON.generate(value)} }, args: {} } })" end |
.javascript(value) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 36 def self.javascript(value) return value['$raw'] if value.is_a?(Hash) && value.key?('$raw') return "[#{value.map { javascript(_1) }.join(', ')}]" if value.is_a?(Array) if value.is_a?(Hash) return "{ #{value.map { |key, item| "#{JSON.generate(key)}: #{javascript(item)}" }.join(', ')} }" end JSON.generate(value) end |
.number(value, fallback) ⇒ Object
34 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 34 def self.number(value, fallback) = Integer(value || fallback) |
.raw(value) ⇒ Object
32 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 32 def self.raw(value) = { '$raw' => value } |
.render_static(key, css_class, uri, options) ⇒ Object
rubocop:disable Metrics/MethodLength
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 14 def self.render_static(key, css_class, uri, ) # rubocop:disable Metrics/MethodLength properties = { key:, '$widgetId': key, datasource: 'image', imageObject: raw("WebStaticImageProperty({ image: { uri: #{JSON.generate(uri)} } })"), imageUrl: raw(expression('')), isBackgroundImage: false, onClickType: 'action', alternativeText: raw(expression('')), widthUnit: unit([:width_unit]), width: number([:width], 100), heightUnit: unit([:height_unit]), height: number([:height], 100), iconSize: 14, displayAs: 'fullImage', responsive: [:responsive], minHeightUnit: 'none', minHeight: 0, maxHeightUnit: 'none', maxHeight: 0, class: css_class } "React.createElement($Image, #{javascript(properties)})" end |
.unit(value) ⇒ Object
33 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 33 def self.unit(value) = value.to_s.downcase.then { _1.empty? ? 'auto' : _1 } |
Instance Method Details
#render ⇒ Object
rubocop:disable Metrics/AbcSize
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 63 def render # rubocop:disable Metrics/AbcSize values = primitive_properties.merge( key: , '$widgetId': , alternativeText: self.class.raw(self.class.expression(text_value('alternativeText'))), class: css_class ) if primitive('datasource') == 'image' values[:imageObject] = self.class.raw( "WebStaticImageProperty({ image: { uri: #{JSON.generate(image_uri)} } })" ) values[:imageUrl] = self.class.raw(self.class.expression(text_value('imageUrl'))) else values[:imageUrl] = self.class.raw(dynamic_image_url) end action = compiled_action values[:onClick] = self.class.raw(action) if action "React.createElement($Image, #{self.class.javascript(values)})" end |
#supported? ⇒ Boolean
58 59 60 61 |
# File 'lib/mxrb/compiler/image_bundle_compiler.rb', line 58 def supported? type = type && type.fetch('WidgetId', nil) == WIDGET_ID && supported_source? && supported_action? end |