comboSearch.ex 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. defmodule NITRO.Combo.Search do
  2. require NITRO
  3. require Record
  4. Record.defrecord(:state, lastMsg: [], timer: [], uid: [], pid: [], chunks: 0, value: [], reader: [], opts: [], feed: [])
  5. def start(uid, value, field, feed0, opts) do
  6. feed = :nitro.to_binary(feed0)
  7. state = state(uid: uid, pid: self(), value: value, reader: :erlang.apply(:kvs, :reader, [feed]), opts: opts, feed: feed)
  8. stop(uid, field)
  9. pi =
  10. NITRO.pi(
  11. module: NITRO.Combo.Search,
  12. table: :async,
  13. state: state,
  14. name: "comboSearch#{uid}",
  15. timeout: :brutal_kill,
  16. restart: :temporary
  17. )
  18. pid = :erlang.spawn_link(:nitro_pi, :start_link, [pi])
  19. :nitro_pi.cache(:async,{:async,"comboSearch#{uid}"},pid,:infinity)
  20. Process.unlink(pid)
  21. end
  22. def stop(uid, _field) do
  23. case :nitro_pi.pid(:async, "comboSearch#{uid}") do
  24. [] -> :ok
  25. pid ->
  26. :erlang.exit(pid, :kill)
  27. :nitro_pi.cache(:async, {:async, "comboSearch#{uid}"}, :undefined)
  28. end
  29. end
  30. def comboScroll(NITRO.comboScroll(uid: uid)) do
  31. case :nitro_pi.pid(:async, "comboSearch#{uid}") do
  32. [] -> []
  33. pid -> send(pid, {:filterComboValues, :append, []})
  34. end
  35. end
  36. def keyUp(NITRO.comboKey(uid: uid, value: value, dom: field, feed: feed, delegate: module)) do
  37. opts = [index: NITRO.Combo.index(module), field: field, delegate: module]
  38. comboContainer = :nitro.atom([:comboContainer, :nitro.to_list(field)])
  39. :nitro.display(:nitro.atom([:comboContainer, field]), :block)
  40. :nitro.clear(comboContainer)
  41. :nitro.wire("comboCloseFormById('#{:nitro.atom([:nitro.to_list(field), 'form'])}');")
  42. :nitro.wire("comboLookupChange('#{field}');")
  43. :nitro.wire(NITRO.bind(target: :nitro.to_binary(comboContainer), type: :scroll, postback: onscroll(uid, field, module)))
  44. start(uid, value, field, feed, opts)
  45. end
  46. def onscroll(uid, field, delegate), do:
  47. :erlang.iolist_to_binary([
  48. "if (event.target && (event.target.scrollTop + event.target.offsetHeight + 10 >= event.target.scrollHeight)) {",
  49. "ws.send(enc(tuple(atom('direct'),
  50. tuple(atom('comboScroll'),",
  51. "bin('#{uid}'),",
  52. "bin('#{field}'),",
  53. "atom('#{delegate}'))",
  54. ")));",
  55. "}"
  56. ])
  57. def proc(:init, NITRO.pi(state: state(value: v) = st) = pi) do
  58. send(self(), {:filterComboValues, :init, v})
  59. {:ok, NITRO.pi(pi, state: state(st, lastMsg: :erlang.timestamp(), timer: ping(100)))}
  60. end
  61. def proc({:check}, NITRO.pi(state: state(timer: t, lastMsg: {_, sec, _}) = st) = pi) do
  62. :erlang.cancel_timer(t)
  63. case :erlang.timestamp() do
  64. {_, x, _} when x - sec >= 60 -> {:stop, :normal, NITRO.pi(pi, state: state(st, timer: []))}
  65. _ -> {:noreply, NITRO.pi(pi, state: state(st, timer: ping(10000)))}
  66. end
  67. end
  68. def proc({:filterComboValues, cmd, value0}, NITRO.pi(state: state(chunks: chunks) = st) = pi) do
  69. state(uid: uid, feed: feed, reader: r, value: prev, pid: pid, opts: opts) = st
  70. m = Keyword.get(opts, :delegate, [])
  71. field = Keyword.get(opts, :field, [])
  72. value = case cmd do :append -> prev; _ -> :string.lowercase(:unicode.characters_to_list(value0, :unicode)) end
  73. r1 = :erlang.apply(:kvs, :take, [:erlang.apply(:kvs, :setfield, [r, :args, 10])])
  74. case :erlang.apply(:kvs, :field, [r1, :args]) do
  75. [] ->
  76. send(pid, {:direct, NITRO.comboInsert(uid: uid, dom: field, delegate: m, chunks: chunks, status: :finished)})
  77. {:stop, :normal, NITRO.pi(pi, state: state(st, reader: :erlang.apply(:kvs, :setfield, [r1, :args, []])))}
  78. rows ->
  79. filtered =
  80. case value do
  81. 'all' -> rows
  82. _ ->
  83. vals = :string.split(:string.trim(value), " ", :all)
  84. if length(vals) > 1, do:
  85. :lists.filter(fn x -> :lists.all(fn v ->:lists.any(&filter(v, x, &1), Keyword.get(opts, :index, [])) end, vals) end, rows),
  86. else:
  87. :lists.filter(fn x -> :lists.any(&filter(value, x, &1), Keyword.get(opts, :index, [])) end, rows)
  88. end
  89. newChunks = chunks + length(filtered)
  90. send(pid, {:direct, NITRO.comboInsert(uid: uid, dom: field, delegate: m, chunks: newChunks, feed: feed, rows: filtered)})
  91. chunks < 100 and
  92. case :nitro_pi.pid(:async, "comboSearch#{uid}") do [] -> []; pid -> send(pid, {:filterComboValues, cmd, value0}) end
  93. {:noreply, NITRO.pi(pi, state: state(st, lastMsg: :erlang.timestamp(), value: value, chunks: newChunks, reader: :erlang.apply(:kvs, :setfield, [r1, :args, []])))}
  94. end
  95. end
  96. def proc(_, pi), do: {:noreply, pi}
  97. def ping(milliseconds), do: :erlang.send_after(milliseconds, self(), {:check})
  98. defp filter(val, obj, i) do
  99. fld =
  100. if is_function(i) do
  101. i.(obj)
  102. else
  103. fldVal = :erlang.apply(:kvs, :field, [obj, i])
  104. if fldVal == elem(obj, 0), do: :nitro.to_list(fldVal), else: fldVal
  105. end |> NITRO.Combo.to_list()
  106. fld != elem(obj, 0) and :string.rstr(:string.lowercase(:nitro.to_list(fld)), val) > 0
  107. end
  108. end