lib/molecules/intuition/utilities.rb in atome-0.5.7.3.9 vs lib/molecules/intuition/utilities.rb in atome-0.5.7.4.2
- old
+ new
@@ -1,21 +1,47 @@
# frozen_string_literal: true
class Atome
+ def reorder_menu
+ disposition = data[:inactive][:disposition]
+ margin = data[:inactive][:margin]
+ spacing = data[:inactive][:spacing]
+ inactive_style = data[:inactive]
+ keys_to_exclude = [:margin, :spacing, :disposition, :text]
+ inactive_style = inactive_style.reject { |key, _| keys_to_exclude.include?(key) }
+ fasten.each_with_index do |atome_f, index|
+ menu_item = grab(atome_f)
+ if disposition == :horizontal
+ menu_item.left = margin[:left] + (inactive_style[:width] + spacing) * index
+ menu_item.top = margin[:top]
+ else
+ menu_item.top = margin[:top] + (inactive_style[:height] + spacing) * index
+ menu_item.left = margin[:left]
+ end
+ end
+ end
+ def remove_menu_item(item_to_remove)
+ grab(item_to_remove).delete(recursive: true)
+ reorder_menu
+ end
+
def create_new_button(button_id, position_in_menu, label, code)
essential_keys = [:inactive, :active]
- buttons_style = data.select { |key, value| essential_keys.include?(key) }
+ buttons_style = data.select { |key, _value| essential_keys.include?(key) }
menu_item = box({ id: button_id })
+ actor({ button_id => :button })
+ menu_item.role(:button)
menu_item.text({ data: label, id: "#{button_id}_label" })
menu_item.code({ button_code: code })
inactive_style = buttons_style[:inactive]
active_style = buttons_style[:active]
if active_style
active_state_text = active_style[:text]
keys_to_exclude = [:margin, :spacing, :disposition, :text]
+
active_style = active_style.reject { |key, _| keys_to_exclude.include?(key) }
end
if inactive_style
inactive_state_text = inactive_style[:text]
@@ -24,10 +50,11 @@
disposition = inactive_style[:disposition]
keys_to_exclude = [:margin, :spacing, :disposition, :text]
inactive_style = inactive_style.reject { |key, _| keys_to_exclude.include?(key) }
menu_item.set(inactive_style)
+ # reorder_menu
if disposition == :horizontal
menu_item.left = margin[:left] + (inactive_style[:width] + spacing) * position_in_menu
menu_item.top = margin[:top]
else
menu_item.top = margin[:top] + (inactive_style[:height] + spacing) * position_in_menu
@@ -40,41 +67,44 @@
end
menu_item.touch(:down) do
unless @active_item == menu_item.id
- menu_item.set(active_style)
+
menu_item.text.each do |text_f|
- grab(text_f).set(active_state_text)
- end
- fasten.each do |item_id|
- unless button_id == item_id
- grab(item_id).remove({ all: :shadow })
- grab(item_id).set(inactive_style)
- grab("#{item_id}_label").remove({ all: :shadow })
- grab("#{item_id}_label").set(inactive_state_text)
+ # below we unset any active style
+ fasten.each do |item_id|
+ unless button_id == item_id
+ grab(item_id).remove({ all: :shadow })
+ grab(item_id).set(inactive_style)
+ grab("#{item_id}_label").remove({ all: :shadow })
+ grab("#{item_id}_label").set(inactive_state_text)
+ end
+ grab(text_f).set(active_state_text)
end
+
end
- code.call if code
+ menu_item.set(active_style)
+ code&.call
+ @active_item = menu_item.id
end
- @active_item = menu_item.id
+
end
+
end
def add_button(params)
params.each do |button_id, params|
label = params[:text]
code = params[:code]
- # {"new_button"=>{"text"=>"button1", "code"=>#<Proc:0xafe>}}
index = fasten.length
create_new_button(button_id, index, label, code)
end
false
end
def resize_matrix(params)
-
width(params[:width])
height(params[:height])
current_matrix = self
real_width = current_matrix.to_px(:width)
real_height = current_matrix.to_px(:height)
@@ -104,30 +134,26 @@
box.left((box_width + spacing) * (index % matrix_cells.collect.length ** (0.5)) + spacing)
box.top((box_height + spacing) * (index / matrix_cells.collect.length ** (0.5)).floor + spacing)
end
end
-
end
new(molecule: :input) do |params, bloc|
params[:height] ||= 15
params[:width] ||= 222
new_id = params.delete(:id) || identity_generator
-
trigger = params.delete(:trigger)
trigger ||= :return
limit = params.delete(:limit)
limit ||= 15
back_col = params.delete(:back)
back_col ||= :grey
text_params = params.delete(:text)
text_params ||= {}
default_text = params.delete(:default)
component = params.delete(:component)
- # component ||= {}
-
default_text ||= :input
default_parent = if self.instance_of?(Atome)
id
else
:view
@@ -144,64 +170,51 @@
{ renderers: [:html], type: :text, component: component,
data: default_text, left: params[:height] * 20 / 100, top: 0, edit: true, attach: input_back.id, height: params[:height],
position: :absolute
}.merge(text_params)
)
- # text_input.set()
text_input.touch(:down) do
input_back.tick(:input)
text_input.edit(true)
end
input_back.touch(:up) do
- if input_back.tick[:input] == 1
- text_input.component({ selected: true })
- end
+ text_input.component({ selected: true }) if input_back.tick[:input] == 1
end
text_input.keyboard(:down) do |native_event|
- # text_input.component({ selected: { color: :red, text: :red } })
-
event = Native(native_event)
if event[:keyCode].to_s == '8' || event[:keyCode].to_s == '46'
# always allow
elsif event[:keyCode].to_s == '13'
# we prevent the input
- if trigger == :return
- bloc.call(text_input.data)
- end
+ bloc.call(text_input.data) if trigger == :return
text_input.edit(false)
event.preventDefault()
elsif text_input.data.length > limit
event.preventDefault()
end
- if trigger == :down
- bloc.call(text_input.data)
- end
+ bloc.call(text_input.data) if trigger == :down
end
- text_input.keyboard(:up) do |native_event|
+ text_input.keyboard(:up) do |_native_event|
input_back.data = text_input.data
- if trigger == :up
- bloc.call(text_input.data)
- end
+ bloc.call(text_input.data) if trigger == :up
end
params.each do |part_f, val_f|
input_back.send(part_f, val_f)
end
input_back.holder(text_input)
input_back
end
new(molecule: :list) do |params, _bloc|
-
styles_found = params.delete(:styles)
element = params.delete(:element)
listing = params.delete(:listing)
action = params.delete(:action)
new_id = params.delete(:id) || identity_generator
-
styles_found ||= {
width: 99,
height: 33,
margin: 6,
shadow: { blur: 9, left: 3, top: 3, id: :cell_shadow, red: 0, green: 0, blue: 0, alpha: 0.6 },
@@ -212,16 +225,12 @@
height: 33,
left: :center,
top: :center,
color: :orange,
type: :text }
- unless params[:width]
- params[:width] = styles_found[:width]
- end
- unless element[:width]
- element[:width] = styles_found[width]
- end
+ params[:width] = styles_found[:width] unless params[:width]
+ element[:width] = styles_found[width] unless element[:width]
margin = styles_found[:margin]
height_found = styles_found[:height]
# lets create the listing container
default_parent = if self.instance_of?(Atome)
@@ -473,26 +482,63 @@
id_f = if params[:id]
params.delete(:id)
else
identity_generator
end
+
main_app = box({ id: id_f, width: :auto, height: :auto, top: 0, bottom: 0, left: 0, right: 0, apply: :app_color,
category: :application })
main_app.remove(:box_color)
main_app.instance_variable_set('@pages', {})
+ main_app.role(:application)
- buttons({
- id: "#{id_f}_menu",
- attach: id_f,
- inactive: { text: { color: :gray }, width: 66, height: 12, spacing: 3, disposition: :horizontal,
- color: :orange, margin: { left: 33, top: 12 } },
- active: { text: { color: :white, shadow: {} }, color: :blue, shadow: {} },
- })
+ menu = buttons({
+ id: "#{id_f}_menu",
+ attach: id_f,
+ inactive: { text: { color: :gray }, width: 66, height: 12, spacing: 3, disposition: :horizontal,
+ color: :orange, margin: { left: 33, top: 12 } },
+ active: { text: { color: :white, shadow: {} }, color: :blue, shadow: {} },
+ })
+ main_app.define_singleton_method(:menu) do
+ menu
+ end
+ main_app.define_singleton_method(:pages) do
+ @pages
+ end
+
+ main_app.define_singleton_method(:insert) do |bloc_to_add|
+ bloc_to_add.each do |page_id, params_f|
+ params_f.each do |block_id, block_content|
+ @pages[page_id][:blocks] ||= {}
+ @pages[page_id][:blocks][block_id.to_sym] = block_content
+ end
+ @blocks ||= {}
+ @blocks[page_id] = @pages[page_id][:blocks]
+ end
+
+ end
+ main_app.define_singleton_method(:extract) do |bloc_to_extract|
+ bloc_to_extract.each do |page_id, block_id|
+ @blocks[page_id].delete(block_id)
+ end
+
+ end
+
+ main_app.define_singleton_method(:blocks) do
+ @blocks
+ end
+ main_app.define_singleton_method(:margin) do
+ @margin = params[:margin]
+ end
+ main_app.define_singleton_method(:spacing) do
+ @spacing = params[:spacing]
+ end
+
main_app
end
-new(molecule: :page) do |params, &bloc|
+new(molecule: :page) do |params = nil, &bloc|
allow_menu = params.delete(:menu)
if params[:id]
id_f = params.delete(:id)
page_name = params.delete(:name)
@pages[id_f.to_sym] = params
@@ -508,39 +554,48 @@
menu_f.add_button({ "#{@id}_menu_item_#{page_name}" => {
text: page_name,
code: item_code
} })
actor({ "#{@id}_menu_item_#{page_name}" => :buttons })
- menu_f.role([:button])
end
end
new(molecule: :show) do |page_id, &bloc|
+
params = @pages[page_id.to_sym]
params ||= {}
footer = params.delete(:footer)
header = params.delete(:header)
left_side_bar = params.delete(:left_side_bar)
right_side_bar = params.delete(:right_side_bar)
- # modules = params.delete(:modules)
basic_size = 30
fasten.each do |page_id_found|
page_found = grab(page_id_found)
- page_found.delete({ recursive: true }) if page_found && page_found.category.include?(:page)
+ page_found.delete({ recursive: true }) if page_found&.category&.include?(:page)
end
color({ id: :page_color, red: 0.1, green: 0.1, blue: 0.1 })
- # TODO : remove the patch below when possible
- id_f = if params[:id]
- params.delete(:id)
- else
- "#{id_f}_#{identity_generator}"
- end
+
+ id_f = "#{id}_content"
main_page = box({ width: :auto, depth: -1, height: :auto, id: id_f, top: 0, bottom: 0, left: 0, right: 0, apply: :page_color, category: :page })
main_page.remove(:box_color)
- main_page.set(params)
+ new_page = main_page.box({ width: '100%', height: '100%', top: 0, left: 0, id: page_id })
+ # now looking for associated blocks
+ blocks_found = params[:blocks]
+ @prev_bloc_height = 0
+ blocks_found&.each_with_index do |(bloc_id, bloc_content), index|
+
+ new_bloc = new_page.box({ id: bloc_id, width: '100%', height: 99, top: spacing + @prev_bloc_height, bottom: 0, left: 0, right: 0 })
+ new_bloc.set(bloc_content)
+ @prev_bloc_height = @prev_bloc_height + new_bloc.height + spacing
+ end
+
+ keys_to_exclude = [:blocks]
+ particles_to_apply = params.reject { |key, _| keys_to_exclude.include?(key) }
+
+ new_page.set(particles_to_apply)
if footer
new_footer = box({ left: 0, depth: -1, right: 0, width: :auto, top: :auto, bottom: 0, height: basic_size, category: :footer, id: "#{id_f}_footer" })
new_footer.remove(:box_color)
new_footer.set(footer)
end
@@ -575,62 +630,51 @@
end
if item_found&.category&.include?(:right_side_bar)
main_page.width(:auto)
main_page.left(item_found.width)
- if footer
- grab("#{id_f}_footer").right(basic_size)
- end
- if header
- grab("#{id_f}_header").right(basic_size)
- end
+ grab("#{id_f}_footer").right(basic_size) if footer
+ grab("#{id_f}_header").right(basic_size) if header
end
if item_found&.category&.include?(:left_side_bar)
main_page.width(:auto)
main_page.right(item_found.width)
- if footer
- grab("#{id_f}_footer").left(basic_size)
- end
- if header
- grab("#{id_f}_header").left(basic_size)
- end
+ grab("#{id_f}_footer").left(basic_size) if footer
+ grab("#{id_f}_header").left(basic_size) if header
end
end
main_page
end
new(molecule: :buttons) do |params, &bloc|
role_f = params.delete(:role)
actor_f = params.delete(:actor)
params_saf = deep_copy(params)
context = params.delete(:attach) || :view
- id = params.delete(:id) || identity_generator
- main = grab(context).box({ id: id })
+ id_f = params.delete(:id) || identity_generator
+ main = grab(context).box({ id: id_f })
main.role(role_f) || main.role(:buttons)
main.actor(actor_f) if actor_f
main.color({ blue: 0.5, red: 1, green: 1, alpha: 0 })
main.data(params_saf)
default = params.delete(:inactive) || {}
main.data[:default] = default
default_text = default.delete(:text)
main.data[:default_text] = default_text
active = params.delete(:active) || {}
- active_text = active.delete(:text)
inactive = {}
active.each_key do |part_f|
inactive[part_f] = default[part_f]
end
inactive_text = {}
active.each_key do |part_f|
inactive_text[part_f] = default_text[part_f]
end
-
params.each_with_index do |(item_id, part_f), index|
label = part_f[:text]
code = part_f[:code]
main.create_new_button(item_id, index, label, code)
-
end
main
end
\ No newline at end of file