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

Methods included from ObjectExtension

#new

Instance Method Details

#above(item, margin = 6, ref = grab(:view)) ⇒ Object



596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/atome/extensions/atome.rb', line 596

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



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/atome/extensions/atome.rb', line 625

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



580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/atome/extensions/atome.rb', line 580

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



572
573
574
575
576
577
578
# File 'lib/atome/extensions/atome.rb', line 572

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



166
167
168
169
170
171
172
# File 'lib/atome/extensions/atome.rb', line 166

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



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/atome/extensions/atome.rb', line 556

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



649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/atome/extensions/atome.rb', line 649

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



611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/atome/extensions/atome.rb', line 611

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



516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/atome/extensions/atome.rb', line 516

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



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
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
# File 'lib/atome/extensions/atome.rb', line 307

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/atome/extensions/atome.rb', line 97

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



131
132
133
# File 'lib/atome/extensions/atome.rb', line 131

def delete (atomes)
  grab(:view).delete(atomes)
end

#digObject



454
455
456
457
458
459
460
461
462
# File 'lib/atome/extensions/atome.rb', line 454

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



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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
# File 'lib/atome/extensions/atome.rb', line 387

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



93
94
95
# File 'lib/atome/extensions/atome.rb', line 93

def filter_keys_to_keep(hash, keys_to_keep)
  hash.select { |key, value| keys_to_keep.include?(key) }
end

#fit(params) ⇒ Object



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/atome/extensions/atome.rb', line 464

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)
      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

#flash(msg) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/atome/extensions/atome.rb', line 113

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) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/atome/extensions/atome.rb', line 499

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



157
158
159
160
161
162
163
# File 'lib/atome/extensions/atome.rb', line 157

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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/atome/extensions/atome.rb', line 139

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_generatorObject



135
136
137
# File 'lib/atome/extensions/atome.rb', line 135

def identity_generator
  "a_#{Universe.app_identity}_#{Universe.counter}".to_sym
end

#importer(target = :all, &proc) ⇒ Object



432
433
434
435
436
437
438
# File 'lib/atome/extensions/atome.rb', line 432

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



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/atome/extensions/atome.rb', line 360

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

#infosObject



440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/atome/extensions/atome.rb', line 440

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

#refreshObject



72
73
74
75
76
77
78
79
80
# File 'lib/atome/extensions/atome.rb', line 72

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_tasksObject

Method to relaunch all tasks from localStorage



770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/atome/extensions/atome.rb', line 770

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



87
88
89
90
91
# File 'lib/atome/extensions/atome.rb', line 87

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



121
122
123
124
125
126
127
128
129
# File 'lib/atome/extensions/atome.rb', line 121

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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/atome/extensions/atome.rb', line 195

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



191
192
193
# File 'lib/atome/extensions/atome.rb', line 191

def repeat_callback(params, counter)
  @repeat[params].call(counter)
end

#resize_and_reposition(objet_atome, scale_factor, axis, max_other_axis_size) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/atome/extensions/atome.rb', line 529

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



542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/atome/extensions/atome.rb', line 542

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_tasksObject

Helper method to retrieve all tasks from localStorage



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# File 'lib/atome/extensions/atome.rb', line 674

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



668
669
670
671
# File 'lib/atome/extensions/atome.rb', line 668

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



713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'lib/atome/extensions/atome.rb', line 713

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



697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/atome/extensions/atome.rb', line 697

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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/atome/extensions/atome.rb', line 273

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
    meta_pressed = event[:metaKey].to_s

    modifier_matched = case option
                       when :ctrl then ctrl_pressed
                       when :alt then alt_pressed
                       when :meta then meta_pressed
                       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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/atome/extensions/atome.rb', line 229

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



761
762
763
764
765
766
767
# File 'lib/atome/extensions/atome.rb', line 761

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



663
664
665
# File 'lib/atome/extensions/atome.rb', line 663

def store_task(name, config)
  JS.global[:localStorage].setItem(name, config.to_json)
end

#tagged(params) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/atome/extensions/atome.rb', line 250

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



82
83
84
# File 'lib/atome/extensions/atome.rb', line 82

def truncate_string(string, max_length)
  string.length > max_length ? string.slice(0, max_length) + '.' : string
end

#wait(time, id = nil, &proc) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/atome/extensions/atome.rb', line 174

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