comboSearch.ex 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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: "stop")) do
  37. case :nitro_pi.pid(:async, "comboSearch#{uid}") do
  38. [] -> []
  39. pid ->
  40. send pid, {:stop, uid, self()}
  41. end
  42. end
  43. def keyUp(NITRO.comboKey(uid: uid, value: value, dom: field, feed: feed, delegate: module)) do
  44. opts = [index: NITRO.Combo.index(module), field: field, delegate: module]
  45. comboContainer = :nitro.atom([:comboContainer, :nitro.to_list(field)])
  46. :nitro.display(:nitro.atom([:comboContainer, field]), :block)
  47. :nitro.clear(comboContainer)
  48. :nitro.wire("comboCloseFormById('#{:nitro.atom([:nitro.to_list(field), 'form'])}');")
  49. :nitro.wire("comboLookupChange('#{field}');")
  50. :nitro.wire(NITRO.bind(target: :nitro.to_binary(comboContainer), type: :scroll, postback: onscroll(uid, field, module)))
  51. send(self(), {:direct, NITRO.comboLoader(dom: field, delegate: module, status: :finished)})
  52. start(uid, value, field, feed, opts)
  53. end
  54. def onscroll(uid, field, delegate), do:
  55. :erlang.iolist_to_binary([
  56. "if (event.target && (event.target.scrollTop + event.target.offsetHeight + 10 >= event.target.scrollHeight)) {",
  57. "ws.send(enc(tuple(atom('direct'),
  58. tuple(atom('comboScroll'),",
  59. "bin('#{uid}'),",
  60. "bin('#{field}'),",
  61. "atom('#{delegate}'))",
  62. ")));",
  63. "}"
  64. ])
  65. def proc(:init, NITRO.pi(state: state(value: v, opts: opts) = st) = pi) do
  66. v = :string.lowercase(v)
  67. index = Keyword.get(opts, :index, [])
  68. |> Enum.flat_map(
  69. fn (i) when is_function(i) -> [i];
  70. (i) -> [fn o -> :erlang.apply(:kvs, :field, [o,i]) end]
  71. end)
  72. cps = :binary.split(v, [" "], [:global,:trim_all])
  73. |> Enum.map(&:binary.compile_pattern(&1))
  74. opts = Keyword.put(opts, :index, index)
  75. opts = Keyword.put(opts, :cps, cps)
  76. send self(), {:filterComboValues, :init, v}
  77. {:ok, NITRO.pi(pi, state: state(st, opts: opts, lastMsg: :erlang.timestamp(), timer: ping(100)))}
  78. end
  79. def proc({:check}, NITRO.pi(state: state(timer: t, lastMsg: {_, sec, _}) = st) = pi) do
  80. :erlang.cancel_timer(t)
  81. case :erlang.timestamp() do
  82. {_, x, _} when x - sec >= 60 -> {:stop, :normal, NITRO.pi(pi, state: state(st, timer: []))}
  83. _ -> {:noreply, NITRO.pi(pi, state: state(st, timer: ping(10000)))}
  84. end
  85. end
  86. def proc({:stop, uid, ref}, NITRO.pi(state: state(opts: opts)) = pi) do
  87. m = Keyword.get(opts, :delegate, [])
  88. send(ref, {:direct, NITRO.comboLoader(dom: uid, delegate: m, status: :finished)})
  89. {:stop, :normal, pi}
  90. end
  91. def proc({:filterComboValues, cmd, value0}, NITRO.pi(state: state(chunks: chunks, value: prev) = st) = pi) do
  92. state(uid: uid, feed: feed, reader: r, pid: pid, opts: opts) = st
  93. m = Keyword.get(opts, :delegate, [])
  94. field = Keyword.get(opts, :field, [])
  95. index = Keyword.get(opts, :index, [])
  96. cps = Keyword.get(opts, :cps, [])
  97. value = case cmd do :init -> value0; _ -> prev end
  98. cmd in [:init, :append] and
  99. send(pid, {:direct, NITRO.comboLoader(dom: field, delegate: m)})
  100. r1 = :erlang.apply(:kvs, :take, [:erlang.apply(:kvs, :setfield, [r, :args, 10])])
  101. case :erlang.apply(:kvs, :field, [r1, :args]) do
  102. [] ->
  103. send(pid, {:direct, NITRO.comboInsert(uid: uid, dom: field, delegate: m, chunks: chunks, status: :finished)})
  104. send(pid, {:direct, NITRO.comboLoader(dom: field, delegate: m, status: :finished)})
  105. {:stop, :normal, NITRO.pi(pi, state: state(st, reader: :erlang.apply(:kvs, :setfield, [r1, :args, []])))}
  106. rows when value == "all" -> rows
  107. rows ->
  108. filtered = rows |>
  109. Enum.flat_map(fn row ->
  110. if index
  111. |> Enum.map(&(:string.lowercase(&1.(row))))
  112. |> Enum.filter(fn r -> cps |> Enum.all?(&:binary.match(r,&1,[]) != :nomatch) end)
  113. |> Enum.empty?, do: [], else: [row]
  114. end)
  115. newChunks = chunks + length(filtered)
  116. send(pid, {:direct, NITRO.comboInsert(uid: uid, dom: field, delegate: m, chunks: newChunks, feed: feed, rows: filtered)})
  117. if chunks < 50, do: send(self(), {:filterComboValues, :continue, value0}),
  118. else: send(pid, {:direct, NITRO.comboLoader(dom: field, delegate: m, status: :finished)})
  119. {:noreply, NITRO.pi(pi, state: state(st, lastMsg: :erlang.timestamp(), value: value, chunks: newChunks, reader: :erlang.apply(:kvs, :setfield, [r1, :args, []])))}
  120. end
  121. end
  122. def proc(_, pi), do: {:noreply, pi}
  123. def ping(milliseconds), do: :erlang.send_after(milliseconds, self(), {:check})
  124. end