/modules/mod_base/scomps/scomp_base_sorter.erl

https://code.google.com/p/zotonic/ · Erlang · 129 lines · 79 code · 28 blank · 22 comment · 6 complexity · 026d51f175e62a5951d423b2f001f7ff 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 Mark an element as a sorter. A sorter is a container for sortables.
  7. %% Copyright 2009 Marc Worrell
  8. %%
  9. %% Licensed under the Apache License, Version 2.0 (the "License");
  10. %% you may not use this file except in compliance with the License.
  11. %% You may obtain a copy of the License at
  12. %%
  13. %% http://www.apache.org/licenses/LICENSE-2.0
  14. %%
  15. %% Unless required by applicable law or agreed to in writing, software
  16. %% distributed under the License is distributed on an "AS IS" BASIS,
  17. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. %% See the License for the specific language governing permissions and
  19. %% limitations under the License.
  20. -module(scomp_base_sorter).
  21. -behaviour(gen_scomp).
  22. -export([vary/2, render/3, event/2]).
  23. -include("zotonic.hrl").
  24. vary(_Params, _Context) -> nocache.
  25. % -record(sortblock, {?ELEMENT_BASE(element_sortblock), tag, items=[], group, connect_with_groups=none, handle }).
  26. render(Params, _Vars, Context) ->
  27. Id = proplists:get_value(id, Params),
  28. Tag = proplists:get_value(tag, Params),
  29. Class = proplists:get_value(class, Params, []),
  30. Handle = proplists:get_value(handle, Params),
  31. ConnectGroups= proplists:get_all_values(connect_group, Params),
  32. Groups = proplists:get_all_values(group, Params),
  33. Delegate = proplists:get_value(delegate, Params),
  34. Axis = proplists:get_value(axis, Params),
  35. Containment = proplists:get_value(containment, Params),
  36. Opacity = proplists:get_value(opacity, Params),
  37. Placeholder = proplists:get_value(placeholder, Params),
  38. case Id of
  39. undefined ->
  40. {error, "sorter scomp, please give the id of the sorter container"};
  41. _ ->
  42. Delegate1 = case Delegate of
  43. undefined -> z_context:get_resource_module(Context);
  44. _ -> z_convert:to_atom(Delegate)
  45. end,
  46. PickledPostbackInfo = z_render:make_postback_info({Tag,Delegate1}, sort, Id, Id, ?MODULE, Context),
  47. Handle1 = case Handle of
  48. undefined -> "null";
  49. _ -> [$', Handle, $']
  50. end,
  51. ConnectWithGroups = case ConnectGroups of
  52. [All] when All == "all" orelse All == <<"all">> -> ["*"];
  53. [None] when None == "none" orelse None == <<"none">> -> [];
  54. _ -> groups_to_connect_with(lists:usort(ConnectGroups ++ Groups))
  55. end,
  56. GroupClasses = groups_to_classes(Groups),
  57. Axis1 = case Axis of
  58. undefined -> "null";
  59. _ -> [$', Axis, $']
  60. end,
  61. Containment1 = case Containment of
  62. undefined -> "null";
  63. _ -> [$', Containment, $']
  64. end,
  65. Opacity1 = case Opacity of
  66. undefined -> "null";
  67. _ -> [$', Opacity, $']
  68. end,
  69. Placeholder1 = case Placeholder of
  70. undefined -> "null";
  71. _ -> [$', Placeholder, $']
  72. end,
  73. % Emit the javascript...
  74. Script = io_lib:format( "z_sorter($('#~s'), { handle: ~s, connectWith: [~s], axis: ~s, containment: ~s, opacity: ~s, placeholder: ~s }, '~s');",
  75. [Id, Handle1, string:join(ConnectWithGroups, ", "), Axis1, Containment1, Opacity1, Placeholder1, PickledPostbackInfo]),
  76. Actions = [
  77. {script, [{script, Script}]},
  78. {add_class, [{class, "sorter " ++ string:join(GroupClasses, " ") ++ " " ++ z_convert:to_list(Class)}]}
  79. ],
  80. {ok, z_render:wire(Id, Actions, Context)}
  81. end.
  82. %% @doc Handle the drop of a sortable in a sorter
  83. event({postback, {SortTag,SortDelegate}, TriggerId, _TargetId}, Context) ->
  84. SortItems = z_context:get_q("sort_items", Context),
  85. UnpickleF = fun(X) ->
  86. {DragTag,DragDelegate,DragId} = z_utils:depickle(X, Context),
  87. #dragdrop{tag=DragTag, delegate=DragDelegate, id=DragId}
  88. end,
  89. Sorted = lists:map(UnpickleF, string:tokens(SortItems, ",")),
  90. Drop = #dragdrop{tag=SortTag, delegate=SortDelegate, id=TriggerId},
  91. try
  92. SortDelegate:event({sort, Sorted, Drop}, Context)
  93. catch
  94. _M:E ->
  95. Error = io_lib:format("Error in routing sort to \"~s:event/2\"; error: \"~p\"", [SortDelegate,E]),
  96. z_render:wire({growl, [{text,Error}, {stay,1}, {type, error}]}, Context)
  97. end.
  98. groups_to_classes(Groups) ->
  99. ["drag_group_" ++ z_convert:to_list(X) || X <- Groups].
  100. groups_to_connect_with(Groups) ->
  101. ["'.drag_group_" ++ z_convert:to_list(X) ++ "'" || X <- Groups].