/modules/mod_base/scomps/scomp_base_droppable.erl

http://github.com/zotonic/zotonic · Erlang · 95 lines · 52 code · 19 blank · 24 comment · 1 complexity · eb48562d46c9542f310b298f482a4b20 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %%
  4. %% Based on code (c) 2008-2009 Rusty Klophaus
  5. %%
  6. %% @doc Make an element draggable
  7. %% {% @draggable id="xxx" tag="sometag" group="group1" group="group2" handle="selector" %}
  8. %% Copyright 2009 Marc Worrell
  9. %%
  10. %% Licensed under the Apache License, Version 2.0 (the "License");
  11. %% you may not use this file except in compliance with the License.
  12. %% You may obtain a copy of the License at
  13. %%
  14. %% http://www.apache.org/licenses/LICENSE-2.0
  15. %%
  16. %% Unless required by applicable law or agreed to in writing, software
  17. %% distributed under the License is distributed on an "AS IS" BASIS,
  18. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. %% See the License for the specific language governing permissions and
  20. %% limitations under the License.
  21. -module(scomp_base_droppable).
  22. -behaviour(gen_scomp).
  23. -export([vary/2, render/3, event/2]).
  24. -include("zotonic.hrl").
  25. vary(_Params, _Context) -> nocache.
  26. %% -record(droppable, {?ELEMENT_BASE(element_droppable), tag, body=[], accept_groups=all, active_class=active, hover_class=hover}).
  27. render(Params, _Vars, Context) ->
  28. Id = proplists:get_value(id, Params),
  29. Tag = proplists:get_value(tag, Params),
  30. ActiveClass = proplists:get_value(active_class, Params, "active"),
  31. HoverClass = proplists:get_value(active_class, Params, "hover"),
  32. AcceptGroups = proplists:get_all_values(accept, Params),
  33. Delegate = proplists:get_value(delegate, Params),
  34. case Id of
  35. undefined ->
  36. {error, "droppable scomp, please give the id of the droppable"};
  37. _ ->
  38. Delegate1 = case Delegate of
  39. undefined -> z_context:get_controller_module(Context);
  40. _ -> z_convert:to_atom(Delegate)
  41. end,
  42. AcceptGroups1 = groups_to_accept(AcceptGroups),
  43. PickledPostbackInfo = z_render:make_postback_info({Tag,Delegate1}, sort, Id, Id, ?MODULE, Context),
  44. Script = io_lib:format( "z_droppable($('#~s'), { activeClass: '~s', hoverClass: '~s', accept: '~s' }, '~s');",
  45. [Id, ActiveClass, HoverClass, AcceptGroups1, PickledPostbackInfo]),
  46. % Hook the actions to the element
  47. Actions = {script, [{script, Script}]},
  48. {ok, z_render:wire(Id, Actions, Context)}
  49. end.
  50. %% @doc Drops will be delegated to this event handler, which will call the postback resource.
  51. event(#postback{message={DropTag,DropDelegate}, trigger=TriggerId}, Context) ->
  52. DragItem = z_context:get_q("drag_item", Context),
  53. {DragTag,DragDelegate,DragId} = z_utils:depickle(DragItem, Context),
  54. Drop = #dragdrop{tag=DropTag, delegate=DropDelegate, id=TriggerId},
  55. Drag = #dragdrop{tag=DragTag, delegate=DragDelegate, id=DragId},
  56. try
  57. Context1 = DropDelegate:event(#drop{drag=Drag, drop=Drop}, z_context:set_controller_module(DropDelegate, Context)),
  58. % also notify the dragged element that it has been dragged
  59. try
  60. DragDelegate:event(#drag{drag=Drag, drop=Drop}, z_context:set_controller_module(DragDelegate, Context1))
  61. catch
  62. _M1:_E1 -> Context1
  63. end
  64. catch
  65. _M2:E ->
  66. Stack = erlang:get_stacktrace(),
  67. lager:error("Error in drop routing: ~p~n~p", [E, Stack]),
  68. Error = io_lib:format("Error in routing drop to module \"~s\"; error: \"~p\"", [DropDelegate,E]),
  69. z_render:wire({growl, [{text,Error}, {stay,1}]}, Context)
  70. end.
  71. groups_to_accept([]) -> "*";
  72. groups_to_accept(["all"]) -> "*";
  73. groups_to_accept(["none"]) -> "";
  74. groups_to_accept(Groups) ->
  75. Groups1 = [".drag_group_" ++ z_convert:to_list(X) || X <- Groups],
  76. string:join(Groups1, ", ").