Skip to content

Commit

Permalink
Merge branch 'master' of github.com:zotonic/zotonic
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed May 23, 2012
2 parents 5e14370 + 983b2b9 commit 5c21181
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 71 deletions.
2 changes: 1 addition & 1 deletion modules/mod_base/dispatch/dispatch
Expand Up @@ -69,7 +69,7 @@
]}, ]},


%% User Agent handling %% User Agent handling
{ua_probe, ["useragent","probe"], resource_user_agent_probe, []}, {ua_probe, ["useragent","probe.js"], resource_user_agent_probe, []},
{ua_select, ["useragent","select", ua_class], resource_user_agent_select, []}, {ua_select, ["useragent","select", ua_class], resource_user_agent_select, []},
{ua_select, ["useragent","select"], resource_user_agent_select, []}, {ua_select, ["useragent","select"], resource_user_agent_select, []},


Expand Down
20 changes: 0 additions & 20 deletions modules/mod_base/lib/js/apps/zotonic-1.0.js
Expand Up @@ -394,26 +394,6 @@ function z_reload(args)
} }
} }


/* Send a post request to a server
---------------------------------------------------------- */

function z_post(path, args)
{
var form = $(document.createElement('form'))
.attr({action: path, method: "POST", style: "display: none"});
for(var key in args) {
if(args.hasOwnProperty(key)) {
var field = $(document.createElement("input"))
.attr({type: "hidden", name: key, value: args[key]});
form.append(field);
}
}
form.appendTo(document.body);
form.submit();
form.remove();
}


/* translations /* translations
---------------------------------------------------------- */ ---------------------------------------------------------- */


Expand Down
61 changes: 20 additions & 41 deletions modules/mod_base/resources/resource_user_agent_probe.erl
Expand Up @@ -17,16 +17,15 @@
%% limitations under the License. %% limitations under the License.


-module(resource_user_agent_probe). -module(resource_user_agent_probe).
-author("Marc Worrell <marc@worrell>"). -author("Marc Worrell <marc@worrell.nl>").
-author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").


-export([ -export([
init/1, init/1,
service_available/2, service_available/2,
allowed_methods/2, allowed_methods/2,
malformed_request/2, malformed_request/2,
post_is_create/2, content_types_provided/2,
process_post/2 to_js/2
]). ]).


-include_lib("webmachine_resource.hrl"). -include_lib("webmachine_resource.hrl").
Expand All @@ -42,13 +41,11 @@ service_available(ReqData, DispatchArgs) ->
?WM_REPLY(true, ContextQs). ?WM_REPLY(true, ContextQs).


allowed_methods(ReqData, Context) -> allowed_methods(ReqData, Context) ->
{['POST'], ReqData, Context}. {['GET'], ReqData, Context}.


%% Check all parameters
%%
malformed_request(ReqData, Context) -> malformed_request(ReqData, Context) ->
C1 = ?WM_REQ(ReqData, Context),
try try
C1 = ?WM_REQ(ReqData, Context),
C2 = z_context:set(ua_args, [ C2 = z_context:set(ua_args, [
{width, list_to_integer(z_context:get_q("w", C1))}, {width, list_to_integer(z_context:get_q("w", C1))},
{height, list_to_integer(z_context:get_q("h", C1))}, {height, list_to_integer(z_context:get_q("h", C1))},
Expand All @@ -57,40 +54,22 @@ malformed_request(ReqData, Context) ->
C1), C1),
?WM_REPLY(false, C2) ?WM_REPLY(false, C2)
catch catch
_:_ -> {true, ReqData, C1} _:_ -> {true, ReqData, Context}
end. end.


%% The probe does not create a new resource. content_types_provided(ReqData, Context) ->
%% {[
post_is_create(ReqData, Context) -> {"application/x-javascript", to_js}
{false, ReqData, Context}. ], ReqData, Context }.

%% Process the probe. Send back a 204 when the user agent class does not
%% change. Force a reload of the page with a 303 See Other when a reload
%% is requested.
%%
process_post(ReqData, Context) ->
Context1 = ?WM_REQ(ReqData, Context),
UaArgs = z_context:get(ua_args, Context1),
ReplyContext = case z_user_agent:ua_probe(UaArgs, Context1) of
{reload, ReloadContext} ->
to_referer(ReloadContext);
{ok, CookieContext} ->
no_reply(CookieContext);
ok ->
no_reply(Context1)
end,
?WM_REPLY(true, ReplyContext).

to_referer(Context) ->
RD = z_context:get_reqdata(Context),
Referer = wrq:get_req_header_lc("referer", RD),
RD1 = wrq:do_redirect(true,
wrq:set_resp_header("location", Referer, RD)),
z_context:set_reqdata(RD1, Context).


no_reply(Context) -> to_js(ReqData, Context) ->
RD = z_context:get_reqdata(Context), CRD = z_context:set_nocache_headers(?WM_REQ(ReqData, Context)),
RD1 = wrq:set_resp_body(<<>>, RD), case z_user_agent:ua_probe(z_context:get(ua_args, CRD), CRD) of
z_context:set_reqdata(RD1, Context). {reload, CR} ->
?WM_REPLY(<<"window.location.href = window.location.protocol+'//'+window.location.host+page.val();">>, CR);
{ok, CR} ->
?WM_REPLY(<<>>, CR);
ok ->
?WM_REPLY(<<>>, CRD)
end.


18 changes: 9 additions & 9 deletions modules/mod_base/templates/_ua_probe.tpl
@@ -1,12 +1,12 @@
{# Javascript that probes the user-agent and performs a callback with the results. #} {# Javascript that probes the user-agent and performs a callback with the results. #}
{% with m.req.ua_props as ua_props %}
<script type="text/javascript"> <script type="text/javascript">
(function() { var ua_probe = "w="+window.screen.width
var ua_curr = {w: window.screen.width, h: window.screen.height, t: (('ontouchstart' in window)?"1":"0")}; + "&h="+window.screen.height
if(({{ ua_props.displayWidth|default:0 }} == ua_curr.w) + "&t="+(('ontouchstart' in window)?"1":"0");
&& ({{ ua_props.displayHeight|default:0 }} == ua_curr.h) && ({{ ua_props.is_touch|if:1:0 }} == ua_curr.t)) var ua_curr = "w="+{{ m.req.ua_props.displayWidth|default:0 }}
return; + "&h="+{{ m.req.ua_props.displayHeight|default:0 }}
z_post("/useragent/probe", ua_curr); + "&t="+{{ m.req.ua_props.is_touch|if:1:0 }};
})(); if (ua_probe != ua_curr) {
document.write(unescape("%3Cscript src='/useragent/probe.js?"+ua_probe+"' type='text/javascript'%3E%3C/script%3E"));
}
</script> </script>
{% endwith %}

0 comments on commit 5c21181

Please sign in to comment.