PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  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
 22-module(scomp_base_sorter).
 23-behaviour(gen_scomp).
 24
 25-export([vary/2, render/3, event/2]).
 26
 27-include("zotonic.hrl").
 28
 29vary(_Params, _Context) -> nocache.
 30
 31% -record(sortblock, {?ELEMENT_BASE(element_sortblock), tag, items=[], group, connect_with_groups=none, handle }).
 32
 33render(Params, _Vars, Context) ->
 34    Id           = proplists:get_value(id, Params),
 35    Tag          = proplists:get_value(tag, Params),
 36    Class        = proplists:get_value(class, Params, []),
 37    Handle       = proplists:get_value(handle, Params),
 38    ConnectGroups= proplists:get_all_values(connect_group, Params),
 39    Groups       = proplists:get_all_values(group, Params),
 40    Delegate     = proplists:get_value(delegate, Params),
 41    Axis     	 = proplists:get_value(axis, Params),
 42	Containment	 = proplists:get_value(containment, Params),
 43	Opacity	 	 = proplists:get_value(opacity, Params),
 44	Placeholder	 = proplists:get_value(placeholder, Params),
 45	
 46    
 47    case Id of
 48        undefined ->
 49            {error, "sorter scomp, please give the id of the sorter container"};
 50        _ ->
 51
 52			Delegate1	 = case Delegate of
 53							undefined -> z_context:get_resource_module(Context);
 54							_ -> z_convert:to_atom(Delegate)
 55						   end,
 56
 57			PickledPostbackInfo = z_render:make_postback_info({Tag,Delegate1}, sort, Id, Id, ?MODULE, Context),
 58			Handle1			  = case Handle of
 59									undefined -> "null";
 60									_		  -> [$', Handle, $']
 61								end,
 62								
 63			ConnectWithGroups = case ConnectGroups of
 64									[All] when All == "all" orelse All == <<"all">> -> ["*"];
 65									[None] when None == "none" orelse None == <<"none">> -> [];
 66									_ -> groups_to_connect_with(lists:usort(ConnectGroups ++ Groups))
 67								end,
 68			GroupClasses	  = groups_to_classes(Groups),
 69			
 70			Axis1				= case Axis of
 71									undefined -> "null";
 72									_		  -> [$', Axis, $']
 73								end,
 74		
 75			Containment1	   = case Containment of
 76									undefined -> "null";
 77									_		  -> [$', Containment, $']
 78								end,
 79
 80			Opacity1	   = case Opacity of
 81									undefined -> "null";
 82									_		  -> [$', Opacity, $']
 83								end,
 84
 85			Placeholder1   = case Placeholder of
 86									undefined -> "null";
 87									_		  -> [$', Placeholder, $']
 88								end,
 89
 90		
 91        	% Emit the javascript...
 92        	Script = io_lib:format( "z_sorter($('#~s'), { handle: ~s, connectWith: [~s], axis: ~s, containment: ~s, opacity: ~s, placeholder: ~s }, '~s');", 
 93        	                        [Id, Handle1, string:join(ConnectWithGroups, ", "), Axis1, Containment1, Opacity1, Placeholder1, PickledPostbackInfo]),
 94
 95            Actions = [
 96                        {script,    [{script, Script}]},
 97                        {add_class, [{class, "sorter " ++ string:join(GroupClasses, " ") ++ " " ++ z_convert:to_list(Class)}]}
 98                    ],
 99	
100    	    {ok, z_render:wire(Id, Actions, Context)}
101    end.
102
103
104%% @doc Handle the drop of a sortable in a sorter
105event({postback, {SortTag,SortDelegate}, TriggerId, _TargetId}, Context) ->
106	SortItems = z_context:get_q("sort_items", Context),
107
108    UnpickleF = fun(X) ->
109                    {DragTag,DragDelegate,DragId} = z_utils:depickle(X, Context),
110                    #dragdrop{tag=DragTag, delegate=DragDelegate, id=DragId}
111                end,
112
113    Sorted = lists:map(UnpickleF, string:tokens(SortItems, ",")),
114    Drop   = #dragdrop{tag=SortTag, delegate=SortDelegate, id=TriggerId},
115
116	try
117	    SortDelegate:event({sort, Sorted, Drop}, Context)
118    catch
119        _M:E ->
120            Error = io_lib:format("Error in routing sort to \"~s:event/2\"; error: \"~p\"", [SortDelegate,E]),
121            z_render:wire({growl, [{text,Error}, {stay,1}, {type, error}]}, Context)
122    end.
123
124
125groups_to_classes(Groups) ->
126	["drag_group_" ++ z_convert:to_list(X) || X <- Groups].
127	
128groups_to_connect_with(Groups) ->
129	["'.drag_group_" ++ z_convert:to_list(X) ++ "'" || X <- Groups].