Skip to content

Commit

Permalink
Template: add webclip template engine
Browse files Browse the repository at this point in the history
[Issue] n/a
[Problem] Templates are not implemented yet
[Solution]
 - add template engine using Mustache templates

Signed-off-by: Tomasz Lukawski <[email protected]>
  • Loading branch information
TomaszLukawskiSam committed Aug 9, 2021
1 parent e3b364d commit bfe5a2e
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 12 deletions.
5 changes: 5 additions & 0 deletions examples/mobile/UIComponents/components/card/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta content="width=device-width, user-scalable=no" name="viewport" />
<link href="../../lib/tau/mobile/theme/changeable/tau.css" rel="stylesheet" />
<link href="../../css/style.css" rel="stylesheet" />
<script src="https://unpkg.com/mustache@latest"></script>
<script data-build-remove="false" src="../../lib/tau/mobile/js/tau.js">
</script>
</head>
Expand All @@ -26,6 +27,10 @@ <h1>
<div class="ui-card" data-src="../../webclip/link-favorite/webclip.html"></div>
<div class="ui-card" data-src="../../webclip/link-container/webclip.html"></div>
<div class="ui-card" data-src="../../webclip/link-list/webclip.html"></div>
<div class="ui-card" data-src="../../webclip/link-video-template/webclip.html"
data-template-data='{"title":"Title","description":"description","video":"https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4","actionText":"Action"}'></div>
<div class="ui-card" data-src="../../webclip/link-tab-template/webclip.html"
data-template-data='{"title":"Title","images":[{"img":"images/tw_list_icon_connections.svg"},{"img":"images/thumbnail_013.jpg"},{"img":"images/thumbnail_013.jpg"}]}' data-template-html=""></div>
</div>
</div>
</body>
Expand Down
4 changes: 2 additions & 2 deletions examples/mobile/UIComponents/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</title>
<link href="lib/tau/mobile/theme/changeable/tau.css" rel="stylesheet" />
<link href="css/style.css" rel="stylesheet" />
<script data-build-remove="false" src="lib/tau/mobile/js/tau.js">
</script>
<script src="https://unpkg.com/mustache@latest"></script>
<script data-build-remove="false" src="lib/tau/mobile/js/tau.js"></script>
</head>

<body>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<div class="ui-card ui-card-service">
<div class="ui-header">
<div class="ui-title">{{title}}</div>
<div class="ui-icon app-icon"></div>
<div class="ui-controls">
<button data-icon="down"></button>
<button data-icon="reorder"></button>
</div>
</div>
<div class="ui-content ui-tabs" style="height: 215px;">
<div class="ui-sub-tab ui-sub-tab-static">
<ul>
<li class="ui-li-anchor">
<a class="ui-tab-active" href="#">
One
</a>
</li>
<li class="ui-li-anchor">
<a href="#">
Two
</a>
</li>
<li class="ui-li-anchor">
<a href="#">
Three
</a>
</li>
</ul>
</div>
<div class="ui-section-changer">
<div>
<section class="ui-content ui-section-active">
<ul class="ui-listview ui-content-area">
<li class="ui-li-divider">
<div class="ui-li-text">
<span class="ui-li-text-title">
Main text 01
</span>
<span class="ui-li-text-sub ui-li-text-value">
Value text
</span>
</div>
</li>
<li class="ui-li-divider">
<div class="ui-li-text">
<span class="ui-li-text-title">
Main text 02
</span>
</div>
</li>
</ul>
</section>
<section class="ui-content">
<ul class="ui-listview ui-content-area">
{{#images}}
<li class="ui-li-has-icon ui-li-divider">
<div class="ui-li-icon">
<img src="{{img}}"/>
</div>
<div class="ui-li-text">
<span class="ui-li-text-title">
Main text 01
</span>
<span class="ui-li-text-sub">
Subtitle on list item
</span>
</div>
</li>
{{/images}}
</ul>
</section>
<section class="ui-content">
<ul class="ui-listview ui-content-area">
<li class="ui-li-divider">
<div class="ui-li-text">
<span class="ui-li-text-title">
Main text 03
</span>
<span class="ui-li-text-sub">
Subtitle on list item
</span>
</div>
</li>
<li>
<div class="ui-li-text">
<span class="ui-li-text-title">
Main text 04
</span>
</div>
</li>
</ul>
</section>
</div>
</div>
</div>
<div class="ui-footer">
<button data-inline="true">View more</button>
</div>
</div>
13 changes: 13 additions & 0 deletions examples/mobile/UIComponents/webclip/link-video-template/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"video": "https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4",
"actionText": "View More",
"title": "Title",
"description": "description",
"items": [{
"name": "abc",
"type": "bcd"
}, {
"name": "123",
"type": "456"
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
`<div class="ui-card ui-card-ads">
<link href="css/style.css" rel="stylesheet" />
<div class="ui-content">
<script src="js/app.js"></script>
<video src="{{video}}" id="video" style="width: 100%;"></video>
<div class="ui-title">{{title}}</div>
<div class="ui-subtitle">{{description}}</div>
<script>
document.getElementById("video").addEventListener("click", function () {
this.play();
});
</script>
</div>
<div class="ui-footer">
<button data-inline="true">{{actionText}}</button>
</div>
</div>`
2 changes: 2 additions & 0 deletions examples/mobile/UIComponents/webclip/link-video/webclip.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div class="ui-card ui-card-service">
<link href="css/style.css" rel="stylesheet" />
<script src="js/app.js"></script>
<div class="ui-header">
<div class="ui-title">Card Video</div>
<div class="ui-icon app-icon"></div>
Expand Down
4 changes: 2 additions & 2 deletions src/js/core/router/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* global define, HTMLElement, ns */
/* global define, ns */
/**
* #Router
*
Expand Down Expand Up @@ -1069,7 +1069,7 @@
} else {
self._loadError(status.absUrl, options, deferred);
}
});
}, options.templateEngine);
}
}
};
Expand Down
7 changes: 4 additions & 3 deletions src/js/core/router/route/card.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
DOM = ns.util.DOM,
defaults = {
volatileRecord: true,
orderNumber: 2
orderNumber: 2,
templateEngine: "webclip"
},
CardRoute = function () {
this.filter = ".ui-card";
Expand Down Expand Up @@ -85,8 +86,8 @@
card,
dataUrl = self._createDataUrl(absUrl);

// Finding matching page inside created element
card = html.querySelector(self.filter);
// Finding matching card inside created element
card = ns.util.selectors.getClosestBySelector(html, self.filter) || html.querySelector(self.filter);

// If a card exists...
if (card) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
templateFunctions = {},
globalOptions = {
"pathPrefix": "",
"default": ""
"default": "html"
};

/**
Expand Down
93 changes: 93 additions & 0 deletions src/js/core/template/webclip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*global define, ns, Mustache*/

/**
* #HTML template engine
*
* Parser for HTML files.
*
* This class hasn't public interface. This class is registered as template engine
* in template manager.
*
* This engine give support of load HTML(Webclip) files for give URL.
*
* @class ns.template.webclip
* @author Tomasz Lukawski <[email protected]>
*/
(function () {
"use strict";
//>>excludeStart("tauBuildExclude", pragmas.tauBuildExclude);
define(
[
"../template",
"../util/path"
],
function () {
//>>excludeEnd("tauBuildExclude");
var template = ns.template;

function fillMarkup(text, data) {
if (Mustache) {
return Mustache.render(text, data);
} else {
ns.warn("Mustache library is required (https://github.com/janl/mustache.js)");
}
}

/**
* Callback for event load and error on XMLHttpRequest
* @param {Function} callback Function called after parse response
* @param {Object} data Data passed to render function
* @param {Event} event event object
*/
function callbackFunction(callback, data, event) {
var request = event.target,
status = {},
text,
doc,
element;

if (request.readyState === 4) {
status.success = (request.status === 200 || (request.status === 0 && request.responseXML));
text = request.responseText;

text = fillMarkup(text, data);
doc = document.implementation.createHTMLDocument("for-create-webclip-elements");
element = doc.createElement("div");
element.innerHTML = text;

element = element.firstElementChild;
element.parentElement.removeChild(element);
element.setAttribute("data-template-html", encodeURI(request.responseText));
callback(status, element);
}
}

/**
* Function process given path, get file by XMLHttpRequest and return
* HTML element.
* @param {Object} globalOptions
* @param {string} path
* @param {Object} data
* @param {Function} callback
*/
function webclipTemplate(globalOptions, path, data, callback) {
var absUrl = path,
request,
eventCallback = callbackFunction.bind(null, callback, data);

// Load the new content.
request = new XMLHttpRequest();
request.responseType = "text";
request.overrideMimeType("text/html");
request.open("GET", absUrl);
request.addEventListener("error", eventCallback);
request.addEventListener("load", eventCallback);
request.send();
}

template.register("webclip", webclipTemplate);
//>>excludeStart("tauBuildExclude", pragmas.tauBuildExclude);
}
);
//>>excludeEnd("tauBuildExclude");
}());
Loading

0 comments on commit bfe5a2e

Please sign in to comment.