Class: Object
- Inherits:
- BasicObject
- Includes:
- Molecule, ObjectExtension
- Defined in:
- lib/atome/extensions/atome.rb,
lib/molecules/init.rb
Overview
atome extensions
Instance Method Summary collapse
- #above(item, margin = 6, ref = grab(:view)) ⇒ Object
- #after(item, margin = 6, ref = grab(:view)) ⇒ Object
- #allow_copy(allow) ⇒ Object
- #allow_right_touch(allow) ⇒ Object
-
#atome_method_for_object(element) ⇒ Object
the method below generate Atome method creation at Object level.
- #atomizer(params) ⇒ Object
- #before(item, margin = 6, ref = grab(:view)) ⇒ Object
- #below(item, margin = 6, ref = grab(:view)) ⇒ Object
- #calculate_total_size(objet_atome, axis) ⇒ Object
- #console(debug) ⇒ Object
- #deep_copy(obj) ⇒ Object
- #delete(atomes) ⇒ Object
- #dig ⇒ Object
- #exception_import(atome_id, &proc) ⇒ Object
- #filter_keys_to_keep(hash, keys_to_keep) ⇒ Object
-
#fit(params) ⇒ Object
def fit(params) unless params.instance_of?(Hash) params = { value: params } end target_size = params axis = params objet_atome = self atomes_found = objet_atome.dig total_width = found_area_used(atomes_found)[:x] - found_area_used(atomes_found)[:x] total_height = found_area_used(atomes_found)[:y] - found_area_used(atomes_found)[:y] if axis == :x ratio = target_size / total_width # now we resize and re-position all atomes atomes_found.each do |atome_id| current_atome = grab(atome_id) current_atome.left(current_atome.left * ratio) current_atome.top(current_atome.top * ratio) new_width = current_atome.to_px(:width) * ratio new_height = current_atome.to_px(:height) * ratio current_atome.width(new_width) current_atome.height(new_height) end else ratio = target_size / total_height # now we resize and re-position all atomes.
- #flash(msg) ⇒ Object
-
#found_area_used(ids, &block) ⇒ Object
def found_area_used(ids) min_x, min_y = Float::INFINITY, Float::INFINITY max_x, max_y = 0, 0 ids.each do |id| atome = grab(id) x = atome.compute({ particle: :left })[:value] y = atome.compute({ particle: :top })[:value] width = atome.to_px(:width) height = atome.to_px(:height) min_x = [min_x, x].min min_y = [min_y, y].min max_x = [max_x, x + width].max max_y = [max_y, y + height].max end { min: { x: min_x, y: min_y }, max: { x: max_x, y: max_y } } end.
- #grab(id_to_get) ⇒ Object
- #hook(a_id) ⇒ Object
- #identity_generator ⇒ Object
- #importer(target = :all, &proc) ⇒ Object
-
#importer_all(&proc) ⇒ Object
import methode below.
- #infos ⇒ Object
- #js_class(class_name) ⇒ Object
- #js_func(function_name, *params) ⇒ Object
- #refresh ⇒ Object
-
#relaunch_all_tasks ⇒ Object
Method to relaunch all tasks from localStorage.
- #remove_key_pair_but(hash, keys_to_keep) ⇒ Object
- #reorder_particles(hash_to_reorder) ⇒ Object
- #repeat(delay = 1, repeat = 0, &proc) ⇒ Object
- #repeat_callback(params, counter) ⇒ Object
- #resize_and_reposition(objet_atome, scale_factor, axis, max_other_axis_size) ⇒ Object
- #resize_object(objet, scale_factor, axis, max_other_axis_size) ⇒ Object
-
#retrieve_all_tasks ⇒ Object
Helper method to retrieve all tasks from localStorage.
-
#retrieve_task(name) ⇒ Object
Helper method to retrieve task configuration from localStorage.
- #schedule_recurrence(name, target_time, recurrence, &block) ⇒ Object
-
#schedule_task(name, years, month, day, hours, minutes, seconds, recurrence: nil, &block) ⇒ Object
Helper method to schedule a task.
- #shortcut(key:, option: nil, affect: :all, exclude: [], &block) ⇒ Object
- #stop(params) ⇒ Object
-
#stop_task(name) ⇒ Object
Helper method to stop a scheduled task.
-
#store_task(name, config) ⇒ Object
Helper method to store task configuration in localStorage.
- #tagged(params) ⇒ Object
- #truncate_string(string, max_length) ⇒ Object
- #wait(time, id = nil, &proc) ⇒ Object
Methods included from ObjectExtension
Instance Method Details
#above(item, margin = 6, ref = grab(:view)) ⇒ Object
691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'lib/atome/extensions/atome.rb', line 691 def above(item, margin = 6, ref = grab(:view)) # FIXME above is broken when using % in margin unless margin.is_a?(Numeric) item_height = ref.to_px(:height) margin = (margin.to_f * item_height) / 100 end pos = item.to_px(:bottom) + item.height + margin item.top(:auto) if item.display == :none 33 else pos end end |
#after(item, margin = 6, ref = grab(:view)) ⇒ Object
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 |
# File 'lib/atome/extensions/atome.rb', line 720 def after(item, margin = 6, ref = grab(:view)) unless margin.is_a?(Numeric) item_width = ref.to_px(:width) margin = (margin.to_f * item_width) / 100 end left_f = if item.left.instance_of?(Integer) item.left else item.to_px(:left) end width_f = if item.width.instance_of?(Integer) item.width else item.to_px(:width) end pos = left_f + width_f + margin if item.display == :none 0 else pos end end |
#allow_copy(allow) ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 |
# File 'lib/atome/extensions/atome.rb', line 675 def allow_copy(allow) if allow # allow selection and text copy JS.eval(<<~JS) document.body.style.userSelect = 'auto'; // allow text slectiion document.removeEventListener('copy', preventDefaultAction); // allow copy JS else # lock selection and text copy JS.eval(<<~JS) document.body.style.userSelect = 'none'; // prevent text selection document.addEventListener('copy', preventDefaultAction); // prevent copy JS end end |
#allow_right_touch(allow) ⇒ Object
667 668 669 670 671 672 673 |
# File 'lib/atome/extensions/atome.rb', line 667 def allow_right_touch(allow) if allow JS.eval('document.removeEventListener("contextmenu", window.preventDefaultAction);') else JS.eval('document.addEventListener("contextmenu", window.preventDefaultAction);') end end |
#atome_method_for_object(element) ⇒ Object
the method below generate Atome method creation at Object level
184 185 186 187 188 189 190 |
# File 'lib/atome/extensions/atome.rb', line 184 def atome_method_for_object(element) Object.define_method element do |params = nil, &user_proc| default_parent = Essentials.default_params[element][:attach] || :view grab(default_parent).send(element, params, &user_proc) end end |
#atomizer(params) ⇒ Object
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/atome/extensions/atome.rb', line 651 def atomizer(params) unless params.instance_of? Hash params = { target: params } end id = params[:id] id_wanted = if id { id: id } else {} end basis = { alien: params[:target], renderers: [:html], type: :atomized }.merge(id_wanted) a = Atome.new(basis) return a # convert any foreign object (think HTML) to a pseudo atome objet , that embed foreign objet end |
#before(item, margin = 6, ref = grab(:view)) ⇒ Object
744 745 746 747 748 749 750 751 752 753 754 755 |
# File 'lib/atome/extensions/atome.rb', line 744 def before(item, margin = 6, ref = grab(:view)) unless margin.is_a?(Numeric) item_width = ref.to_px(:width) margin = (margin.to_f * item_width) / 100 end pos = item.to_px(:right) + item.width + margin if item.display == :none 0 else pos end end |
#below(item, margin = 6, ref = grab(:view)) ⇒ Object
706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/atome/extensions/atome.rb', line 706 def below(item, margin = 6, ref = grab(:view)) unless margin.is_a?(Numeric) item_height = ref.to_px(:height) margin = (margin.to_f * item_height) / 100 end pos = item.to_px(:top) + item.to_px(:height) + margin if item.display == :none 0 else pos end end |
#calculate_total_size(objet_atome, axis) ⇒ Object
611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/atome/extensions/atome.rb', line 611 def calculate_total_size(objet_atome, axis) total_size = (axis == :x) ? objet_atome.to_px(:width) : objet_atome.to_px(:height) max_other_axis_size = (axis == :x) ? objet_atome.to_px(:height) : objet_atome.to_px(:width) objet_atome.fasten.each do |child_id| child = grab(child_id) child_size = (axis == :x) ? child.to_px(:width) : child.to_px(:height) other_axis_size = (axis == :x) ? child.to_px(:height) : child.to_px(:width) total_size += child_size max_other_axis_size = [max_other_axis_size, other_axis_size].max end [total_size, max_other_axis_size] end |
#console(debug) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/atome/extensions/atome.rb', line 325 def console(debug) if debug console = box({ id: :console, width: :auto, height: 225, bottom: 0, top: :auto, left: 0, right: 0, depth: 30, color: { alpha: 0, red: 0, green: 0, blue: 0 } }) console_back = console.box({ id: :console_back, blur: { value: 5, affect: :back }, overflow: :auto, width: :auto, height: :auto, top: 25, bottom: 0, left: 0, right: 0, depth: 30, color: { alpha: 0.5, red: 0, green: 0, blue: 0 } }) console_top = console.box({ id: :console_top, overflow: :auto, width: :auto, height: 25, top: 0, bottom: 0, left: 0, right: 0, depth: 30, color: { alpha: 1, red: 0.3, green: 0.3, blue: 0.3 } }) console_top.touch(:double) do console.height(25) console.bottom(0) console.top(:auto) end console_top.shadow({ id: :s1, left: 0, top: 3, blur: 9, invert: false, red: 0, green: 0, blue: 0, alpha: 1 }) console.drag(:locked) do |event| dy = event[:dy] y = console.to_px(:top) + dy.to_f console.top(y) console.height(:auto) end console_output = console_back.text({ data: '', id: :console_output, component: { size: 12 } }) JS.eval <<~JS (function() { var oldLog = console.log; var consoleDiv = document.getElementById("console_output"); console.log = function(message) { if (consoleDiv) { consoleDiv.innerHTML += '<p>' + message + '</p>'; } oldLog.apply(console, arguments); }; }()); JS console_clear = console_top.circle({ id: :console_clear, color: :red, top: 3, left: 3, width: 19, height: 19 }) console_clear.touch(true) do console_output.data("") end JS.global[:document].addEventListener("contextmenu") do |event| end else grab(:console_back).delete(true) JS.global[:document].addEventListener("contextmenu") do |native_event| event = Native(native_event) event.preventDefault end end end |
#deep_copy(obj) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/atome/extensions/atome.rb', line 115 def deep_copy(obj) # utility for buttons case obj when Hash obj.each_with_object({}) do |(k, v), h| unless k == :code # exception to avoid Proc accumulation h[deep_copy(k)] = deep_copy(v) end end when Array obj.map { |e| deep_copy(e) } else obj.dup rescue obj end end |
#delete(atomes) ⇒ Object
149 150 151 |
# File 'lib/atome/extensions/atome.rb', line 149 def delete (atomes) grab(:view).delete(atomes) end |
#dig ⇒ Object
472 473 474 475 476 477 478 479 480 |
# File 'lib/atome/extensions/atome.rb', line 472 def dig ids = [] dig_recursive = lambda do |atome| ids << atome.id atome.fasten.each { |fasten_atome| dig_recursive.call(grab(fasten_atome)) } end dig_recursive.call(self) ids end |
#exception_import(atome_id, &proc) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/atome/extensions/atome.rb', line 405 def exception_import(atome_id, &proc) if Universe.user_atomes.include?(atome_id.to_sym) special_div = JS.global[:document].getElementById(atome_id) special_div.addEventListener('dragover') do |native_event| event = Native(native_event) special_div[:style][:backgroundColor] = 'red' event.preventDefault event.stopPropagation end special_div.addEventListener('dragleave') do |native_event| event = Native(native_event) special_div[:style][:backgroundColor] = 'yellow' event.stopPropagation end special_div.addEventListener('drop') do |native_event| event = Native(native_event) event.preventDefault event.stopPropagation files = event[:dataTransfer][:files] if files[:length].to_i > 0 (0...files[:length].to_i).each do |i| file = files[i] reader = JS.eval('let a= new FileReader(); return a') reader.readAsText(file) reader.addEventListener('load') do proc.call({ content: reader[:result].to_s, name: file[:name].to_s, type: file[:type].to_s, size: file[:size].to_s }) end reader.addEventListener('error') do puts 'Error: ' + file[:name].to_s end end end end JS.global[:document][:body].addEventListener('drop') do |native_event| event = Native(native_event) event.preventDefault event.stopPropagation end end end |
#filter_keys_to_keep(hash, keys_to_keep) ⇒ Object
111 112 113 |
# File 'lib/atome/extensions/atome.rb', line 111 def filter_keys_to_keep(hash, keys_to_keep) hash.select { |key, value| keys_to_keep.include?(key) } end |
#fit(params) ⇒ Object
def fit(params)
unless params.instance_of?(Hash)
params = { value: params }
end
target_size = params[:value]
axis = params[:axis]
objet_atome = self
atomes_found = objet_atome.dig
total_width = found_area_used(atomes_found)[:max][:x] - found_area_used(atomes_found)[:min][:x]
total_height = found_area_used(atomes_found)[:max][:y] - found_area_used(atomes_found)[:min][:y]
if axis == :x
ratio = target_size / total_width
# now we resize and re-position all atomes
atomes_found.each do |atome_id|
current_atome = grab(atome_id)
current_atome.left(current_atome.left * ratio)
current_atome.top(current_atome.top * ratio)
new_width = current_atome.to_px(:width) * ratio
new_height = current_atome.to_px(:height) * ratio
current_atome.width(new_width)
current_atome.height(new_height)
end
else
ratio = target_size / total_height
# now we resize and re-position all atomes
atomes_found.each do |atome_id|
current_atome = grab(atome_id)
# puts "#{current_atome}, #{atome_id}, left: #{current_atome.left}, ratio: #{ratio}"
current_atome.left(current_atome.left * ratio)
current_atome.top(current_atome.top * ratio)
current_atome.width(current_atome.to_px(:width) * ratio)
current_atome.height(current_atome.to_px(:height) * ratio)
end
end
end
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/atome/extensions/atome.rb', line 520 def fit(params) unless params.instance_of?(Hash) params = { value: params } end target_size = params[:value] axis = params[:axis] objet_atome = self atomes_found = objet_atome.dig found_area_used(atomes_found) do |result| total_width = result[:max][:x] - result[:min][:x] total_height = result[:max][:y] - result[:min][:y] ratio = if axis == :x target_size / total_width else target_size / total_height end atomes_found.each do |atome_id| current_atome = grab(atome_id) left = current_atome.left || 0 top = current_atome.top || 0 current_atome.left(left * ratio) current_atome.top(top * ratio) new_width = current_atome.to_px(:width) * ratio new_height = current_atome.to_px(:height) * ratio current_atome.width(new_width) current_atome.height(new_height) end end end |
#flash(msg) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/atome/extensions/atome.rb', line 131 def flash(msg) flash_box = box({ width: 235, height: 112 }) flash_box.text(msg) flash_box.touch(true) do flash_box.delete({ recursive: true }) end end |
#found_area_used(ids, &block) ⇒ Object
def found_area_used(ids)
min_x, min_y = Float::INFINITY, Float::INFINITY
max_x, max_y = 0, 0
ids.each do |id|
atome = grab(id)
x = atome.compute({ particle: :left })[:value]
y = atome.compute({ particle: :top })[:value]
width = atome.to_px(:width)
height = atome.to_px(:height)
min_x = [min_x, x].min
min_y = [min_y, y].min
max_x = [max_x, x + width].max
max_y = [max_y, y + height].max
end
{ min: { x: min_x, y: min_y }, max: { x: max_x, y: max_y } }
end
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/atome/extensions/atome.rb', line 573 def found_area_used(ids, &block) check_interval = 0.1 wait(check_interval) do all_ready = ids.all? do |id| atome = grab(id) width = atome.to_px(:width).to_f rescue 0 height = atome.to_px(:height).to_f rescue 0 width > 0 && height > 0 end if all_ready min_x, min_y = Float::INFINITY, Float::INFINITY max_x, max_y = 0, 0 ids.each do |id| atome = grab(id) x = atome.compute({ particle: :left })[:value].to_f rescue 0 y = atome.compute({ particle: :top })[:value].to_f rescue 0 width = atome.to_px(:width).to_f rescue 0 height = atome.to_px(:height).to_f rescue 0 min_x = [min_x, x].min min_y = [min_y, y].min max_x = [max_x, x + width].max max_y = [max_y, y + height].max end result = { min: { x: min_x, y: min_y }, max: { x: max_x, y: max_y } } block.call(result) if block_given? else found_area_used(ids, &block) end end end |
#grab(id_to_get) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/atome/extensions/atome.rb', line 175 def grab(id_to_get) id_to_get = id_to_get.to_sym return if id_to_get == false aid_to_get = Universe.atomes_ids[id_to_get] aid_to_get = '' if aid_to_get.instance_of? Array Universe.atomes[aid_to_get] end |
#hook(a_id) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/atome/extensions/atome.rb', line 157 def hook(a_id) a_id = a_id.to_sym # Universe.atomes[a_id] atome_get = '' Universe.atomes.each do |aid_f, atome| # alert "1 #{atome}" if atome.id == a_id atome_get = atome # alert "2 #{atome}" end # alert "3 #{atome_get}" end # alert atome_get atome_get end |
#identity_generator ⇒ Object
153 154 155 |
# File 'lib/atome/extensions/atome.rb', line 153 def identity_generator "a_#{Universe.app_identity}_#{Universe.counter}".to_sym end |
#importer(target = :all, &proc) ⇒ Object
450 451 452 453 454 455 456 |
# File 'lib/atome/extensions/atome.rb', line 450 def importer(target = :all, &proc) if target == :all importer_all(&proc) else exception_import(target, &proc) end end |
#importer_all(&proc) ⇒ Object
import methode below
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/atome/extensions/atome.rb', line 378 def importer_all(&proc) JS.global[:document][:body].addEventListener('dragover') do |native_event| event = Native(native_event) event.preventDefault end JS.global[:document][:body].addEventListener('drop') do |native_event| event = Native(native_event) event.preventDefault files = event[:dataTransfer][:files] if files[:length].to_i > 0 (0...files[:length].to_i).each do |i| file = files[i] reader = JS.eval('let a= new FileReader(); return a') reader.readAsText(file) reader.addEventListener('load') do proc.call({ content: reader[:result].to_s, name: file[:name].to_s, type: file[:type].to_s, size: file[:size].to_s }) end reader.addEventListener('error') do puts 'Error: ' + file[:name].to_s end end end end end |
#infos ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/atome/extensions/atome.rb', line 458 def infos particle_list = Universe.particle_list.dup particle_list.delete(:password) particle_list.delete(:selection) infos = {} particle_list[:css] = :poil particle_list.each do |particle_found| infos[particle_found[0]] = send(particle_found[0]) unless send(particle_found[0]).nil? end # we convert CssProxy object to hash below infos[:css] = eval(infos[:css].to_s) infos end |
#js_class(class_name) ⇒ Object
87 88 89 |
# File 'lib/atome/extensions/atome.rb', line 87 def js_class(class_name) JS.eval("return new #{class_name}()") end |
#js_func(function_name, *params) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/atome/extensions/atome.rb', line 73 def js_func(function_name, *params) args = params.map do |param| case param when String "'#{param}'" when TrueClass, FalseClass, Numeric param.to_s else raise ArgumentError, "Unsupported parameter type: #{param.class}" end end.join(", ") JS.eval("#{function_name}(#{args})") end |
#refresh ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/atome/extensions/atome.rb', line 91 def refresh grab(:view).retrieve do |child| child.refresh end # atomes_to_treat=grab(:view).fasten.dup # atomes_to_treat.each do |atome_found| # grab(atome_found).refresh # end end |
#relaunch_all_tasks ⇒ Object
Method to relaunch all tasks from localStorage
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
# File 'lib/atome/extensions/atome.rb', line 865 def relaunch_all_tasks tasks = retrieve_all_tasks tasks.each do |task| name = task[:name] config = task[:config] target_time_found = config['target_time'] target_time = Time.parse(target_time_found) recurrence_found = config['recurrence'] next unless recurrence_found recurrence = config['recurrence'].is_a?(Hash) ? config['recurrence'].transform_keys(&:to_sym) : config['recurrence'].to_sym schedule_task(name, target_time.year, target_time.month, target_time.day, target_time.hour, target_time.min, target_time.sec, recurrence: recurrence) do puts "Relaunched task #{name}(add proc here)" end end end |
#remove_key_pair_but(hash, keys_to_keep) ⇒ Object
105 106 107 108 109 |
# File 'lib/atome/extensions/atome.rb', line 105 def remove_key_pair_but(hash, keys_to_keep) hash.dup.delete_if do |key, value| key.is_a?(Integer) && key.even? && !keys_to_keep.include?(key) end.reject { |key, value| keys_to_keep.include?(key) } end |
#reorder_particles(hash_to_reorder) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/atome/extensions/atome.rb', line 139 def reorder_particles(hash_to_reorder) # we reorder the hash ordered_keys = %i[renderers id alien type attach int8 unit] ordered_part = ordered_keys.map { |k| [k, hash_to_reorder[k]] }.to_h other_part = hash_to_reorder.reject { |k, _| ordered_keys.include?(k) } # merge the parts to obtain an re-ordered hash ordered_part.merge(other_part) end |
#repeat(delay = 1, repeat = 0, &proc) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/atome/extensions/atome.rb', line 213 def repeat(delay = 1, repeat = 0, &proc) @repeat ||= [] @repeat << proc repeat_id = @repeat.length - 1 JS.eval(<<~JS) function repeat(action, interval, repetitions) { let count = 0; let intervalId = null; function executeAction() { if (count < repetitions) { action(count); count++; } else { clearInterval(intervalId); } } executeAction(); // execute immediatly intervalId = setInterval(executeAction, interval); return intervalId; } function myAction(counter) { atomeJsToRuby("repeat_callback(#{repeat_id}, "+counter+")") } const intervalId = repeat(myAction, #{delay} * 1000, #{repeat}); return intervalId; JS repeat_id + 1 end |
#repeat_callback(params, counter) ⇒ Object
209 210 211 |
# File 'lib/atome/extensions/atome.rb', line 209 def repeat_callback(params, counter) @repeat[params].call(counter) end |
#resize_and_reposition(objet_atome, scale_factor, axis, max_other_axis_size) ⇒ Object
624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/atome/extensions/atome.rb', line 624 def resize_and_reposition(objet_atome, scale_factor, axis, max_other_axis_size) current_position = 0 resize_object(objet_atome, scale_factor, axis, max_other_axis_size) current_position += (axis == :x) ? objet_atome.to_px(:width) : objet_atome.to_px(:height) objet_atome.fasten.each do |child_id| child = grab(child_id) resize_object(child, scale_factor, axis, max_other_axis_size) child.top(child.top * scale_factor) child.left(child.left * scale_factor) current_position += child.to_px(:height) end end |
#resize_object(objet, scale_factor, axis, max_other_axis_size) ⇒ Object
637 638 639 640 641 642 643 644 645 646 647 648 649 |
# File 'lib/atome/extensions/atome.rb', line 637 def resize_object(objet, scale_factor, axis, max_other_axis_size) if axis == :x new_width = objet.width * scale_factor new_height = new_width / (objet.width.to_f / objet.height) objet.width(new_width) objet.height([new_height, max_other_axis_size].min) else new_height = objet.height * scale_factor new_width = new_height / (objet.height.to_f / objet.width) objet.height(new_height) objet.width([new_width, max_other_axis_size].min) end end |
#retrieve_all_tasks ⇒ Object
Helper method to retrieve all tasks from localStorage
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'lib/atome/extensions/atome.rb', line 769 def retrieve_all_tasks tasks = [] local_storage = JS.global[:localStorage] if Atome::host == "web-opal" local_storage.each do |key| value = local_storage.getItem(key) if value value = JSON.parse(value) tasks << { name: key, config: value } end end else length = local_storage[:length].to_i length.times do |i| key = local_storage.call(:key, i) value = local_storage.call(:getItem, key) tasks << { name: key, config: JSON.parse(value.to_s) } if value end end tasks end |
#retrieve_task(name) ⇒ Object
Helper method to retrieve task configuration from localStorage
763 764 765 766 |
# File 'lib/atome/extensions/atome.rb', line 763 def retrieve_task(name) config = JS.global[:localStorage].getItem(name) config.nil? ? nil : JSON.parse(config) end |
#schedule_recurrence(name, target_time, recurrence, &block) ⇒ Object
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 |
# File 'lib/atome/extensions/atome.rb', line 808 def schedule_recurrence(name, target_time, recurrence, &block) now = Time.now next_time = target_time case recurrence when :yearly next_time += 365 * 24 * 60 * 60 while next_time <= now when :monthly next_time = next_time >> 1 while next_time <= now when :weekly next_time += 7 * 24 * 60 * 60 while next_time <= now when :daily next_time += 24 * 60 * 60 while next_time <= now when :hourly next_time += 60 * 60 while next_time <= now when :minutely next_time += 60 while next_time <= now when :secondly next_time += 1 while next_time <= now when Hash if recurrence[:weekly] wday = recurrence[:weekly] next_time += 7 * 24 * 60 * 60 while next_time <= now next_time += 24 * 60 * 60 until next_time.wday == wday elsif recurrence[:monthly] week_of_month = recurrence[:monthly][:week] wday = recurrence[:monthly][:wday] while next_time <= now next_month = next_time >> 1 next_time = Time.new(next_month.year, next_month.month, 1, target_time.hour, target_time.min, target_time.sec) next_time += 24 * 60 * 60 while next_time.wday != wday next_time += (week_of_month - 1) * 7 * 24 * 60 * 60 end end else puts "Invalid recurrence option" return end seconds_until_next = next_time - Time.now wait_task = wait(seconds_until_next) do block.call schedule_recurrence(name, next_time, recurrence, &block) end store_task(name, { wait: wait_task, target_time: next_time, recurrence: recurrence }) end |
#schedule_task(name, years, month, day, hours, minutes, seconds, recurrence: nil, &block) ⇒ Object
Helper method to schedule a task
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'lib/atome/extensions/atome.rb', line 792 def schedule_task(name, years, month, day, hours, minutes, seconds, recurrence: nil, &block) target_time = Time.new(years, month, day, hours, minutes, seconds) now = Time.now if target_time < now schedule_recurrence(name, target_time, recurrence, &block) else seconds_until_target = target_time - now wait_task = wait(seconds_until_target) do block.call schedule_recurrence(name, target_time, recurrence, &block) if recurrence end store_task(name, { wait: wait_task, target_time: target_time, recurrence: recurrence }) end end |
#shortcut(key:, option: nil, affect: :all, exclude: [], &block) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/atome/extensions/atome.rb', line 291 def shortcut(key:, option: nil, affect: :all, exclude: [], &block) element_ids = (Array(affect) + Array(exclude)).uniq element_ids.each do |element_id| element = JS.global[:document].querySelector("##{element_id}") unless element_id.to_sym == :all element.addEventListener("mouseenter") { $current_hovered_element = element_id } element.addEventListener("mouseleave") { $current_hovered_element = nil } end end JS.global[:document].addEventListener("keydown") do |native_event| event = Native(native_event) key_pressed = event[:key].to_s.downcase ctrl_pressed = event[:ctrlKey].to_s alt_pressed = event[:altKey].to_s = event[:metaKey].to_s modifier_matched = case option when :ctrl then ctrl_pressed when :alt then alt_pressed when :meta then else true end affect_condition = affect == :all || Array(affect).include?($current_hovered_element) exclude_condition = !Array(exclude).include?($current_hovered_element) $current_hovered_element = :view if $current_hovered_element.nil? if key_pressed == key.to_s.downcase && modifier_matched && affect_condition && exclude_condition block.call(key_pressed, $current_hovered_element) end end end |
#stop(params) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/atome/extensions/atome.rb', line 247 def stop(params) case params when Hash if params.key?(:repeat) repeater_to_stop = params[:repeat] JS.eval(<<~JS) clearInterval(#{repeater_to_stop}); JS elsif params.key?(:wait) waiter_to_stop = params[:wait] JS.eval(<<~JS) clearTimeout(window.timeoutIds['#{waiter_to_stop}']) JS else puts "msg from stop method: the :repeat key doesn't exist" end else puts "msg from stop method, this params is not a Hash" end end |
#stop_task(name) ⇒ Object
Helper method to stop a scheduled task
856 857 858 859 860 861 862 |
# File 'lib/atome/extensions/atome.rb', line 856 def stop_task(name) task_config = retrieve_task(name) return unless task_config stop({ wait: task_config['wait'] }) JS.global[:localStorage].removeItem(name) end |
#store_task(name, config) ⇒ Object
Helper method to store task configuration in localStorage
758 759 760 |
# File 'lib/atome/extensions/atome.rb', line 758 def store_task(name, config) JS.global[:localStorage].setItem(name, config.to_json) end |
#tagged(params) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/atome/extensions/atome.rb', line 268 def tagged(params) atome_get = [] if params.instance_of? Hash params.each do |tag_name, tag_value| Universe.atomes.each do |atomes_id_found, atomes_found| if atomes_found.tag&.instance_of?(Hash) && (atomes_found.tag[tag_name] == tag_value) atome_get << atomes_id_found end end end else Universe.atomes.each do |atomes_id_found, atomes_found| atome_get << atomes_id_found if atomes_found.tag&.instance_of?(Hash) && atomes_found.tag[params] end end atome_get end |
#truncate_string(string, max_length) ⇒ Object
101 102 103 |
# File 'lib/atome/extensions/atome.rb', line 101 def truncate_string(string, max_length) string.length > max_length ? string.slice(0, max_length) + '.' : string end |
#wait(time, id = nil, &proc) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/atome/extensions/atome.rb', line 192 def wait(time, id = nil, &proc) # we generate an uniq id if [:kill, 'kill'].include?(time) JS.eval("clearTimeout(window.timeoutIds['#{id}']);") else obj = Object.new unique_id = obj.object_id id ||= unique_id time *= 1000 callback_id = "myRubyCallback_#{id}" JS.global[callback_id.to_sym] = proc JS.eval("if (!window.timeoutIds) { window.timeoutIds = {}; } window.timeoutIds['#{id}'] = setTimeout(function() { #{callback_id}(); }, #{time});") end id end |