PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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