2 (function($, undefined) {
4 ensureNS("jQuery.mobile.tizen");
6 jQuery.extend( jQuery.mobile.tizen,
11 * load the prototype for a widget.
13 * If @widget is a string, the function looks for @widget.prototype.html in the proto-html/ subdirectory of the
14 * framework's current theme and loads the file via AJAX into a string. Note that the file will only be loaded via
15 * AJAX once. If two widget instances based on the same @widget value are to be constructed, the second will be
16 * constructed from the cached copy of the prototype of the first instance.
18 * If @widget is not a string, it is assumed to be a hash containing at least one key, "proto", the value of which is
19 * the string to be used for the widget prototype. if another key named "key" is also provided, it will serve as the
20 * key under which to cache the prototype, so it need not be rendered again in the future.
22 * Given the string for the widget prototype, the following patterns occurring in the string are replaced:
24 * "${FRAMEWORK_ROOT}" - replaced with the path to the root of the framework
26 * The function then creates a jQuery $("<div>") object containing the prototype from the string.
28 * If @ui is not provided, the jQuery object containing the prototype is returned.
30 * If @ui is provided, it is assumed to be a (possibly multi-level) hash containing CSS selectors. For every level of
31 * the hash and for each string-valued key at that level, the CSS selector specified as the value is sought in the
32 * prototype jQuery object and, if found, the value of the key is replaced with the jQuery object resulting from the
33 * search. Additionally, if the CSS selector is of the form "#widgetid", the "id" attribute will be removed from the
34 * elements contained within the resulting jQuery object. The resulting hash is returned.
39 * $.mobile.tizen.loadPrototype("mywidget") => Returns a <div> containing the structure from the file
40 * mywidget.prototype.html located in the current theme folder of the current framework.
42 * 2. $.mobile.tizen.loadPrototype("mywidget", ui):
43 * where ui is a hash that looks like this:
45 * element1: "<css selector 1>",
46 * element2: "<css selector 2>",
48 * group1element1: "<css selector 3>",
49 * group1element1: "<css selector 4>"
54 * In this case, after loading the prototype as in Example 1, loadPrototype will traverse @ui and replace the CSS
55 * selector strings with the result of the search for the selector string upon the prototype. If any of the CSS
56 * selectors are of the form "#elementid" then the "id" attribute will be stripped from the elements selected. This
57 * means that they will no longer be accessible via the selector used initially. @ui is then returned thus modified.
60 loadPrototype: function(widget, ui) {
62 theScriptTag = $("script[data-framework-version][data-framework-root][data-framework-theme]"),
63 frameworkRootPath = theScriptTag.attr("data-framework-root") + "/" +
64 theScriptTag.attr("data-framework-version") + "/";
66 function replaceVariables(s) {
67 return s.replace(/\$\{FRAMEWORK_ROOT\}/g, frameworkRootPath);
70 function fillObj(obj, uiProto) {
73 for (var key in obj) {
74 if (typeof obj[key] === "string") {
76 obj[key] = uiProto.find(obj[key]);
77 if (selector.substring(0, 1) === "#")
78 obj[key].removeAttr("id");
81 if (typeof obj[key] === "object")
82 obj[key] = fillObj(obj[key], uiProto);
87 /* If @widget is a string ... */
88 if (typeof widget === "string") {
89 /* ... try to use it as a key into the cached prototype hash ... */
90 ret = $.mobile.tizen._widgetPrototypes[widget];
91 if (ret === undefined) {
92 /* ... and if the proto was not found, try to load its definition ... */
93 var protoPath = frameworkRootPath + "proto-html" + "/" +
94 theScriptTag.attr("data-framework-theme");
96 url: protoPath + "/" + widget + ".prototype.html",
100 .success(function(data, textStatus, jqXHR) {
101 /* ... and if loading succeeds, cache it and use a copy of it ... */
102 $.mobile.tizen._widgetPrototypes[widget] = $("<div>").html(replaceVariables(data));
103 ret = $.mobile.tizen._widgetPrototypes[widget].clone();
109 /* ... if a key was provided ... */
110 if (widget.key !== undefined)
111 /* ... try to use it as a key into the cached prototype hash ... */
112 ret = $.mobile.tizen._widgetPrototypes[widget.key];
114 /* ... and if the proto was not found in the cache ... */
115 if (ret === undefined) {
116 /* ... and a proto definition string was provided ... */
117 if (widget.proto !== undefined) {
118 /* ... create a new proto from the definition ... */
119 ret = $("<div>").html(replaceVariables(widget.proto));
120 /* ... and if a key was provided ... */
121 if (widget.key !== undefined)
122 /* ... cache a copy of the proto under that key */
123 $.mobile.tizen._widgetPrototypes[widget.key] = ret.clone();
127 /* otherwise, if the proto /was/ found in the cache, return a copy of it */
131 /* If the prototype was found/created successfully ... */
132 if (ret != undefined)
133 /* ... and @ui was provided */
135 /* ... return @ui, but replace the CSS selectors it contains with the elements they select */
136 ret = fillObj(ui, ret);