Skip to content

Commit

Permalink
Add lib tag options 'async' and 'minify'. (#2447)
Browse files Browse the repository at this point in the history
* Add lib tag options 'async' and 'minify'.

Also make jquery ui optional for simple zotonic widgets.

* Fix rst table
  • Loading branch information
mworrell committed Jun 2, 2020
1 parent 35f3465 commit 48f5b6b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 24 deletions.
70 changes: 58 additions & 12 deletions apps/zotonic_core/src/support/z_lib_include.erl
Expand Up @@ -40,10 +40,10 @@ tag(Files, Context) ->

%% @doc Generate the link and/or script tags for the given files.
tag(Files, Args, Context) ->
case m_config:get_value(mod_development, libsep, Context) of
Empty when Empty == []; Empty == <<>>; Empty == undefined ->
case m_config:get_boolean(mod_development, libsep, Context) of
false ->
tag1(Files, Args, Context);
_Other ->
true ->
lists:foldr(fun(F, [Css,Js]) ->
[C,J] = tag1([F], Args, Context),
[[C|Css], [J|Js]]
Expand All @@ -65,10 +65,12 @@ url(Files, Args, Context) ->

url_for(_F, [], _Ext, _Args, _Context) -> [];
url_for(F, P, Ext, Args, Context) ->
[z_dispatcher:url_for(lib(Context), url_for_args(F, P, Ext, Args, Context), Context)].
[z_dispatcher:url_for(lib(Args, Context), url_for_args(F, P, Ext, Args, Context), Context)].

lib(Context) ->
case z_convert:to_bool(m_site:get(minification_enabled, Context)) of
lib(Args, Context) ->
case z_convert:to_bool( proplists:get_value(minify, Args, false))
orelse m_config:get_boolean(site, minification_enabled, Context)
of
true -> lib_min;
false -> lib
end.
Expand All @@ -95,16 +97,60 @@ link_element(Css, CssPath, Args, Context) ->
undefined -> [];
TitleValue -> [<<" title=\"">>, TitleValue, $"]
end,
MediaAttr = [<<" media=\"">>, proplists:get_value(media, Args, "all"), $"],
RelAttr = [<<" rel=\"">>, proplists:get_value(rel, Args, "stylesheet"), $"],
CssUrl = z_dispatcher:url_for(lib(Context), url_for_args(Css, CssPath, <<".css">>, Args, Context), Context),
iolist_to_binary([<<"<link href=\"">>, CssUrl, <<"\" type=\"text/css\"">>, MediaAttr, TitleAttr, RelAttr, $/, $>]).
Media = proplists:get_value(media, Args, <<"all">>),
Rel = proplists:get_value(rel, Args, <<"stylesheet">>),
CssUrl = z_dispatcher:url_for(
lib(Args, Context),
url_for_args(Css, CssPath, <<".css">>, Args, Context),
Context),
case z_convert:to_bool( proplists:get_value(async, Args, false)) of
true ->
iolist_to_binary([
<<"<link href=\"">>, CssUrl, <<"\" type=\"text/css\"">>,
TitleAttr,
<<" media=\"none\"">>,
<<" onload=\"if(media!='">>, Media, <<"')media='">>, Media, <<"'\"">>,
<<" rel=\"">>, Rel, $",
<<">">>,
<<"<noscript>">>,
<<"<link href=\"">>, CssUrl, <<"\" type=\"text/css\"">>,
TitleAttr,
<<" media=\"">>, Media, $",
<<" rel=\"">>, Rel, $",
<<">">>,
<<"</noscript>">>

]);
false ->
iolist_to_binary([
<<"<link href=\"">>, CssUrl, <<"\" type=\"text/css\"">>,
TitleAttr,
<<" media=\"">>, Media, $",
<<" rel=\"">>, Rel, $",
<<">">>
])
end.

script_element(_Js, [], _Args, _Context) ->
[];
script_element(Js, JsPath, Args, Context) ->
JsUrl = z_dispatcher:url_for(lib(Context), url_for_args(Js, JsPath, <<".js">>, Args, Context), Context),
iolist_to_binary([<<"<script src=\"">>, JsUrl, <<"\" type=\"text/javascript\"></script>">>]).
JsUrl = z_dispatcher:url_for(
lib(Args, Context),
url_for_args(Js, JsPath, <<".js">>, Args, Context),
Context),
AsyncAttr = case z_convert:to_bool( proplists:get_value(async, Args, false)) of
true ->
<<" async ">>;
false ->
<<>>
end,
iolist_to_binary([
<<"<script src=\"">>, JsUrl, <<"\"">>,
AsyncAttr,
<<" type=\"text/javascript\"">>,
<<">">>,
<<"</script>">>
]).

url_for_args(Files, JoinedPath, Extension, Args, Context) ->
AbsUrlArg = case proplists:get_value(absolute_url, Args, false) of
Expand Down
16 changes: 16 additions & 0 deletions apps/zotonic_mod_base/priv/lib/js/apps/z.widgetmanager.js
Expand Up @@ -190,4 +190,20 @@ limitations under the License.
return this;
};

// Make jQuery UI optional
if (typeof $.ui === 'undefined') {
$.ui = {
dialog: {}
};
}

if (typeof $.widget !== 'function')
{
$.widget = function( widgetName ) {
let name = widgetName.replace(/^ui\./, '');
$.ui[name] = {
};
}
}

})(jQuery);
3 changes: 3 additions & 0 deletions apps/zotonic_mod_translation/priv/lib-src/Makefile
@@ -0,0 +1,3 @@

../lib/css/mod_translation.css: mod_translation.less
lessc --include-path="../../../zotonic_mod_admin/priv/lib-src/zotonic-admin/less/" mod_translation.less ../lib/css/mod_translation.css
@@ -1,4 +1,4 @@
@import (reference) "../../../../zotonic_mod_admin/priv/lib/less/variables.less";
@import (reference) "variables.less";

.dropdown-menu {
&.mod_translation_menu-has-icons {
Expand Down
8 changes: 0 additions & 8 deletions apps/zotonic_mod_translation/priv/lib/less/config

This file was deleted.

1 change: 0 additions & 1 deletion apps/zotonic_mod_translation/priv/lib/less/less_to_css

This file was deleted.

19 changes: 17 additions & 2 deletions doc/ref/tags/tag_lib.rst
Expand Up @@ -29,19 +29,34 @@ Will output::

The number at the end is the Unix modification time of the most recently changed file.

The `lib` tag supports optional arguments to control the resulting html tag:
The `lib` tag supports optional arguments to control the resulting html tag. The arguments are supplied
after the list of files::

{% lib
"css/not-so-important-styles.css"
minify
async
%}


Accepted arguments are:

+-----------------+-------------+---------------------------------------------------------+
|Option |Default |Description |
+=================+=============+=========================================================+
|absolute_url |false |If true, prefix the generated URL with |
| | |"http://{hostname}/". |
| | |"https://{hostname}/". |
+-----------------+-------------+---------------------------------------------------------+
|title |<empty> |Specify a value for the title attribute of the link tag. |
+-----------------+-------------+---------------------------------------------------------+
|media |"all" |Specify value for the media attribute of the link tag. |
+-----------------+-------------+---------------------------------------------------------+
|rel |"stylesheet" |Specify value for the rel attribute of the link tag. |
+-----------------+-------------+---------------------------------------------------------+
|minify | |Force minification use as ``{% lib ... minify %}`` |
+-----------------+-------------+---------------------------------------------------------+
|async | |Load css or javascript asynchronously, use as |
| | | ``{% lib ... async %}`` |
+-----------------+-------------+---------------------------------------------------------+

.. seealso:: :ref:`mod_development`.

0 comments on commit 48f5b6b

Please sign in to comment.