Skip to content
Browse files

Merge branch 'master' of github.com:zotonic/zotonic

  • Loading branch information...
2 parents 5e14370 + 983b2b9 commit 5c211816c8c2d626fa290a20545e6b0b95a7e9b8 @mworrell mworrell committed May 23, 2012
View
2 modules/mod_base/dispatch/dispatch
@@ -69,7 +69,7 @@
]},
%% 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"], resource_user_agent_select, []},
View
20 modules/mod_base/lib/js/apps/zotonic-1.0.js
@@ -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
---------------------------------------------------------- */
View
61 modules/mod_base/resources/resource_user_agent_probe.erl
@@ -17,16 +17,15 @@
%% limitations under the License.
-module(resource_user_agent_probe).
--author("Marc Worrell <marc@worrell>").
--author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
+-author("Marc Worrell <marc@worrell.nl>").
-export([
init/1,
service_available/2,
allowed_methods/2,
malformed_request/2,
- post_is_create/2,
- process_post/2
+ content_types_provided/2,
+ to_js/2
]).
-include_lib("webmachine_resource.hrl").
@@ -42,13 +41,11 @@ service_available(ReqData, DispatchArgs) ->
?WM_REPLY(true, ContextQs).
allowed_methods(ReqData, Context) ->
- {['POST'], ReqData, Context}.
+ {['GET'], ReqData, Context}.
-%% Check all parameters
-%%
malformed_request(ReqData, Context) ->
- C1 = ?WM_REQ(ReqData, Context),
try
+ C1 = ?WM_REQ(ReqData, Context),
C2 = z_context:set(ua_args, [
{width, list_to_integer(z_context:get_q("w", C1))},
{height, list_to_integer(z_context:get_q("h", C1))},
@@ -57,40 +54,22 @@ malformed_request(ReqData, Context) ->
C1),
?WM_REPLY(false, C2)
catch
- _:_ -> {true, ReqData, C1}
+ _:_ -> {true, ReqData, Context}
end.
-%% The probe does not create a new resource.
-%%
-post_is_create(ReqData, Context) ->
- {false, 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).
+content_types_provided(ReqData, Context) ->
+ {[
+ {"application/x-javascript", to_js}
+ ], ReqData, Context }.
-no_reply(Context) ->
- RD = z_context:get_reqdata(Context),
- RD1 = wrq:set_resp_body(<<>>, RD),
- z_context:set_reqdata(RD1, Context).
+to_js(ReqData, Context) ->
+ CRD = z_context:set_nocache_headers(?WM_REQ(ReqData, Context)),
+ case z_user_agent:ua_probe(z_context:get(ua_args, CRD), CRD) of
+ {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.
View
18 modules/mod_base/templates/_ua_probe.tpl
@@ -1,12 +1,12 @@
{# 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">
-(function() {
- var ua_curr = {w: window.screen.width, h: window.screen.height, t: (('ontouchstart' in window)?"1":"0")};
- if(({{ ua_props.displayWidth|default:0 }} == ua_curr.w)
- && ({{ ua_props.displayHeight|default:0 }} == ua_curr.h) && ({{ ua_props.is_touch|if:1:0 }} == ua_curr.t))
- return;
- z_post("/useragent/probe", ua_curr);
-})();
+var ua_probe = "w="+window.screen.width
+ + "&h="+window.screen.height
+ + "&t="+(('ontouchstart' in window)?"1":"0");
+var ua_curr = "w="+{{ m.req.ua_props.displayWidth|default:0 }}
+ + "&h="+{{ m.req.ua_props.displayHeight|default:0 }}
+ + "&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>
-{% endwith %}

0 comments on commit 5c21181

Please sign in to comment.
Something went wrong with that request. Please try again.