|
1 | | -(-> |
2 | | - ldloader = (opt={}) -> |
3 | | - @opt = {active-class: \running, base-z: 4000, auto-z: false, class-name: '', atomic: true} <<< opt |
4 | | - <[root container]>.map (n) ~> if opt[n] => |
5 | | - @[n] = (if Array.isArray(opt[n]) => opt[n] else [opt[n]]).map -> |
6 | | - ret = if typeof(it) == \string => document.querySelector(it) else it |
7 | | - if !ret => console.warn "[ldloader] warning: no node found for #it" |
8 | | - ret |
9 | | - if !@container => @container = if @root => @root.map(-> it.parentNode) else [document.body] |
10 | | - if !@root => @root = @container.map -> |
11 | | - node = document.createElement("div") |
12 | | - it.appendChild node |
13 | | - return node |
14 | | - @root.map ~> |
15 | | - it.classList.add.apply it.classList, (@opt.class-name or '').split(' ').filter(->it) |
16 | | - it.classList.remove opt.active-class |
17 | | - if opt.inactive-class => it.classList.add opt.inactive-class |
18 | | - @ <<< running: false, count: 0 |
19 | | - @ |
| 1 | +ldloader = (opt={}) -> |
| 2 | + @opt = {active-class: \running, base-z: 4000, auto-z: false, class-name: '', atomic: true} <<< opt |
| 3 | + <[root container]>.map (n) ~> if opt[n] => |
| 4 | + @[n] = (if Array.isArray(opt[n]) => opt[n] else [opt[n]]).map -> |
| 5 | + ret = if typeof(it) == \string => document.querySelector(it) else it |
| 6 | + if !ret => console.warn "[ldloader] warning: no node found for #it" |
| 7 | + ret |
| 8 | + if !@container => @container = if @root => @root.map(-> it.parentNode) else [document.body] |
| 9 | + if !@root => @root = @container.map -> |
| 10 | + node = document.createElement("div") |
| 11 | + it.appendChild node |
| 12 | + return node |
| 13 | + @root.map ~> |
| 14 | + it.classList.add.apply it.classList, (@opt.class-name or '').split(' ').filter(->it) |
| 15 | + it.classList.remove opt.active-class |
| 16 | + if opt.inactive-class => it.classList.add opt.inactive-class |
| 17 | + @ <<< running: false, count: 0 |
| 18 | + @ |
20 | 19 |
|
21 | | - ldloader.prototype = Object.create(Object.prototype) <<< do |
22 | | - is-on: -> return @running |
23 | | - on: (delay=0) -> @toggle true, delay |
24 | | - off: (delay=0, force = false) -> @toggle false, delay, force |
25 | | - # if v defined, force state to v. |
26 | | - cancel: (v) -> clearTimeout @handle; if v? => @toggle v |
27 | | - flash: (dur=1000, delay=0) -> @toggle(true, delay).then ~> @toggle false, dur + delay |
28 | | - render: -> |
29 | | - if !(@running and @opt.ctrl and @opt.ctrl.step) => return @render.runid = -1 |
30 | | - @render.runid = runid = Math.random! |
31 | | - @render.start = 0 |
32 | | - if @opt.ctrl.init => @root.map ~> @opt.ctrl.init.call it |
33 | | - _ = (t) ~> |
34 | | - if !@render.start => @render.start = t |
35 | | - @root.map ~> @opt.ctrl.step.call it, (t - @render.start) |
36 | | - if @render.runid == runid => requestAnimationFrame -> _ it |
37 | | - else if @opt.ctrl.done => @root.map ~> @opt.ctrl.done.call it, (t - @render.start) |
38 | | - ret = requestAnimationFrame -> _ it |
39 | | - toggle: (v, delay=0, force = false) -> |
40 | | - d = (if !(v?) => (if @root.0.classList.contains @opt.active-class => -1 else 1) else if v => 1 else -1) |
41 | | - if @handle => @cancel! |
42 | | - if delay => return new Promise (res, rej) ~> |
43 | | - # new approach - treat delay directly as a simple delay before action. |
44 | | - @handle = setTimeout (~> @toggle(v)then ~> res!), delay |
45 | | - # old approach |
46 | | - # if is on: resolve after on |
47 | | - #if d > 0 => @toggle(v)then ~> setTimeout (~> res!), delay |
48 | | - # if is off: off after resolve |
49 | | - #else => setTimeout (~> @toggle(v)then ~> res!), delay |
50 | | - new Promise (res, rej) ~> |
51 | | - @count = (@count + d >? 0) |
52 | | - if !force and !@opt.atomic and ( @count >= 2 or (@count == 1 and d < 0)) => return res! |
53 | | - @root.map ~> |
54 | | - it.classList.toggle @opt.active-class, (d > 0) |
55 | | - if @opt.inactive-class => it.classList.toggle @opt.inactive-class, (d < 0) |
56 | | - @running = running = @root.0.classList.contains(@opt.active-class) |
57 | | - if @opt.ctrl => @render! |
58 | | - if !@opt.auto-z => return res! |
59 | | - if running => |
60 | | - if ldloader.zmgr => @z = ldloader.zmgr.add @opt.base-z |
61 | | - else |
62 | | - @z = (ldloader.zstack[* - 1] or @opt.base-z) + 1 |
63 | | - ldloader.zstack.push z |
64 | | - @root.map ~> it.style.zIndex = @z |
| 20 | +ldloader.prototype = Object.create(Object.prototype) <<< do |
| 21 | + is-on: -> return @running |
| 22 | + on: (delay=0) -> @toggle true, delay |
| 23 | + off: (delay=0, force = false) -> @toggle false, delay, force |
| 24 | + # if v defined, force state to v. |
| 25 | + cancel: (v) -> clearTimeout @handle; if v? => @toggle v |
| 26 | + flash: (dur=1000, delay=0) -> @toggle(true, delay).then ~> @toggle false, dur + delay |
| 27 | + render: -> |
| 28 | + if !(@running and @opt.ctrl and @opt.ctrl.step) => return @render.runid = -1 |
| 29 | + @render.runid = runid = Math.random! |
| 30 | + @render.start = 0 |
| 31 | + if @opt.ctrl.init => @root.map ~> @opt.ctrl.init.call it |
| 32 | + _ = (t) ~> |
| 33 | + if !@render.start => @render.start = t |
| 34 | + @root.map ~> @opt.ctrl.step.call it, (t - @render.start) |
| 35 | + if @render.runid == runid => requestAnimationFrame -> _ it |
| 36 | + else if @opt.ctrl.done => @root.map ~> @opt.ctrl.done.call it, (t - @render.start) |
| 37 | + ret = requestAnimationFrame -> _ it |
| 38 | + toggle: (v, delay=0, force = false) -> |
| 39 | + d = (if !(v?) => (if @root.0.classList.contains @opt.active-class => -1 else 1) else if v => 1 else -1) |
| 40 | + if @handle => @cancel! |
| 41 | + if delay => return new Promise (res, rej) ~> |
| 42 | + # new approach - treat delay directly as a simple delay before action. |
| 43 | + @handle = setTimeout (~> @toggle(v)then ~> res!), delay |
| 44 | + # old approach |
| 45 | + # if is on: resolve after on |
| 46 | + #if d > 0 => @toggle(v)then ~> setTimeout (~> res!), delay |
| 47 | + # if is off: off after resolve |
| 48 | + #else => setTimeout (~> @toggle(v)then ~> res!), delay |
| 49 | + new Promise (res, rej) ~> |
| 50 | + @count = (@count + d >? 0) |
| 51 | + if !force and !@opt.atomic and ( @count >= 2 or (@count == 1 and d < 0)) => return res! |
| 52 | + @root.map ~> |
| 53 | + it.classList.toggle @opt.active-class, (d > 0) |
| 54 | + if @opt.inactive-class => it.classList.toggle @opt.inactive-class, (d < 0) |
| 55 | + @running = running = @root.0.classList.contains(@opt.active-class) |
| 56 | + if @opt.ctrl => @render! |
| 57 | + if !@opt.auto-z => return res! |
| 58 | + if running => |
| 59 | + if ldloader.zmgr => @z = ldloader.zmgr.add @opt.base-z |
65 | 60 | else |
66 | | - if ldloader.zmgr => ldloader.zmgr.remove @z |
67 | | - else |
68 | | - if (idx = ldloader.zstack.indexOf(@z)) < 0 => return res! |
69 | | - ldloader.zstack.splice(idx, 1) |
70 | | - @root.map ~> it.style.zIndex = "" |
71 | | - res! |
| 61 | + @z = (ldloader.zstack[* - 1] or @opt.base-z) + 1 |
| 62 | + ldloader.zstack.push z |
| 63 | + @root.map ~> it.style.zIndex = @z |
| 64 | + else |
| 65 | + if ldloader.zmgr => ldloader.zmgr.remove @z |
| 66 | + else |
| 67 | + if (idx = ldloader.zstack.indexOf(@z)) < 0 => return res! |
| 68 | + ldloader.zstack.splice(idx, 1) |
| 69 | + @root.map ~> it.style.zIndex = "" |
| 70 | + res! |
72 | 71 |
|
73 | | - ldloader <<< do |
74 | | - zstack: [] |
75 | | - set-zmgr: -> @zmgr = it |
| 72 | +ldloader <<< do |
| 73 | + zstack: [] |
| 74 | + set-zmgr: -> @zmgr = it |
76 | 75 |
|
77 | | - if module? => module.exports = ldloader |
78 | | - else window.ldloader = window.ldLoader = ldloader |
79 | | -)! |
| 76 | +if module? => module.exports = ldloader |
| 77 | +else window.ldloader = window.ldLoader = ldloader |
0 commit comments