1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
2 //>>description: Loads widget's prototype
3 //>>label: Widget prototype loader
6 define( [ ], function ( ) {
7 //>>excludeEnd("jqmBuildExclude");
9 ( function ( $, undefined ) {
11 ensureNS( "jQuery.mobile.tizen" );
13 jQuery.extend( jQuery.mobile.tizen, {
14 _widgetPrototypes: {},
17 * load the prototype for a widget.
19 * If @widget is a string, the function looks for @widget.prototype.html in the proto-html/ subdirectory of the
20 * framework's current theme and loads the file via AJAX into a string. Note that the file will only be loaded via
21 * AJAX once. If two widget instances based on the same @widget value are to be constructed, the second will be
22 * constructed from the cached copy of the prototype of the first instance.
24 * If @widget is not a string, it is assumed to be a hash containing at least one key, "proto", the value of which is
25 * the string to be used for the widget prototype. if another key named "key" is also provided, it will serve as the
26 * key under which to cache the prototype, so it need not be rendered again in the future.
28 * Given the string for the widget prototype, the following patterns occurring in the string are replaced:
30 * "${FRAMEWORK_ROOT}" - replaced with the path to the root of the framework
32 * The function then creates a jQuery $("<div>") object containing the prototype from the string.
34 * If @ui is not provided, the jQuery object containing the prototype is returned.
36 * If @ui is provided, it is assumed to be a (possibly multi-level) hash containing CSS selectors. For every level of
37 * the hash and for each string-valued key at that level, the CSS selector specified as the value is sought in the
38 * prototype jQuery object and, if found, the value of the key is replaced with the jQuery object resulting from the
39 * search. Additionally, if the CSS selector is of the form "#widgetid", the "id" attribute will be removed from the
40 * elements contained within the resulting jQuery object. The resulting hash is returned.
45 * $.mobile.tizen.loadPrototype("mywidget") => Returns a <div> containing the structure from the file
46 * mywidget.prototype.html located in the current theme folder of the current framework.
48 * 2. $.mobile.tizen.loadPrototype("mywidget", ui):
49 * where ui is a hash that looks like this:
51 * element1: "<css selector 1>",
52 * element2: "<css selector 2>",
54 * group1element1: "<css selector 3>",
55 * group1element1: "<css selector 4>"
60 * In this case, after loading the prototype as in Example 1, loadPrototype will traverse @ui and replace the CSS
61 * selector strings with the result of the search for the selector string upon the prototype. If any of the CSS
62 * selectors are of the form "#elementid" then the "id" attribute will be stripped from the elements selected. This
63 * means that they will no longer be accessible via the selector used initially. @ui is then returned thus modified.
66 loadPrototype: function ( widget, ui ) {
68 theScriptTag = $( "script[data-framework-version][data-framework-root][data-framework-theme]" ),
69 frameworkRootPath = theScriptTag.attr( "data-framework-root" ) + "/" +
70 theScriptTag.attr( "data-framework-version" ) + "/",
73 function replaceVariables( s ) {
74 return s.replace( /\$\{FRAMEWORK_ROOT\}/g, frameworkRootPath );
77 function fillObj( obj, uiProto ) {
81 if ( typeof obj[ key ] === "string" ) {
82 selector = obj[ key ];
83 obj[ key ] = uiProto.find( obj[ key ] );
84 if ( selector.substring( 0, 1 ) === "#" ) {
85 obj[ key ].removeAttr( "id" );
87 } else if (typeof obj[ key ] === "object") {
88 obj[ key ] = fillObj( obj[ key ], uiProto );
94 /* If @widget is a string ... */
95 if ( typeof widget === "string" ) {
96 /* ... try to use it as a key into the cached prototype hash ... */
97 ret = $.mobile.tizen._widgetPrototypes[widget];
98 if ( ret === undefined ) {
99 /* ... and if the proto was not found, try to load its definition ... */
100 protoPath = frameworkRootPath + "proto-html" + "/" +
101 theScriptTag.attr( "data-framework-theme" );
103 url: protoPath + "/" + widget + ".prototype.html",
107 .success( function ( data, textStatus, jqXHR ) {
108 /* ... and if loading succeeds, cache it and use a copy of it ... */
109 $.mobile.tizen._widgetPrototypes[ widget ] = $( "<div>" ).html( replaceVariables( data ) );
110 ret = $.mobile.tizen._widgetPrototypes[ widget ].clone();
115 /* ... if a key was provided ... */
116 if ( widget.key !== undefined ) {
117 /* ... try to use it as a key into the cached prototype hash ... */
118 ret = $.mobile.tizen._widgetPrototypes[ widget.key ];
121 /* ... and if the proto was not found in the cache ... */
122 if ( ret === undefined ) {
123 /* ... and a proto definition string was provided ... */
124 if ( widget.proto !== undefined ) {
125 /* ... create a new proto from the definition ... */
126 ret = $( "<div>" ).html(replaceVariables( widget.proto ) );
127 /* ... and if a key was provided ... */
128 if ( widget.key !== undefined ) {
129 /* ... cache a copy of the proto under that key */
130 $.mobile.tizen._widgetPrototypes[ widget.key ] = ret.clone();
134 /* otherwise, if the proto /was/ found in the cache, return a copy of it */
139 /* If the prototype was found/created successfully ... */
140 if ( ret != undefined ) {
141 /* ... and @ui was provided */
142 if ( ui != undefined ) {
143 /* ... return @ui, but replace the CSS selectors it contains with the elements they select */
144 ret = fillObj( ui, ret );
153 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
155 //>>excludeEnd("jqmBuildExclude");