You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
11 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-Template-method-constructor'><span id='Ext-Template'>/**
  19. </span></span> * @class Ext.Template
  20. * &lt;p&gt;Represents an HTML fragment template. Templates may be {@link #compile precompiled}
  21. * for greater performance.&lt;/p&gt;
  22. * &lt;p&gt;For example usage {@link #Template see the constructor}.&lt;/p&gt;
  23. *
  24. * @constructor
  25. * An instance of this class may be created by passing to the constructor either
  26. * a single argument, or multiple arguments:
  27. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  28. * &lt;li&gt;&lt;b&gt;single argument&lt;/b&gt; : String/Array
  29. * &lt;div class=&quot;sub-desc&quot;&gt;
  30. * The single argument may be either a String or an Array:&lt;ul&gt;
  31. * &lt;li&gt;&lt;tt&gt;String&lt;/tt&gt; : &lt;/li&gt;&lt;pre&gt;&lt;code&gt;
  32. var t = new Ext.Template(&quot;&amp;lt;div&gt;Hello {0}.&amp;lt;/div&gt;&quot;);
  33. t.{@link #append}('some-element', ['foo']);
  34. * &lt;/code&gt;&lt;/pre&gt;
  35. * &lt;li&gt;&lt;tt&gt;Array&lt;/tt&gt; : &lt;/li&gt;
  36. * An Array will be combined with &lt;code&gt;join('')&lt;/code&gt;.
  37. &lt;pre&gt;&lt;code&gt;
  38. var t = new Ext.Template([
  39. '&amp;lt;div name=&quot;{id}&quot;&amp;gt;',
  40. '&amp;lt;span class=&quot;{cls}&quot;&amp;gt;{name:trim} {value:ellipsis(10)}&amp;lt;/span&amp;gt;',
  41. '&amp;lt;/div&amp;gt;',
  42. ]);
  43. t.{@link #compile}();
  44. t.{@link #append}('some-element', {id: 'myid', cls: 'myclass', name: 'foo', value: 'bar'});
  45. &lt;/code&gt;&lt;/pre&gt;
  46. * &lt;/ul&gt;&lt;/div&gt;&lt;/li&gt;
  47. * &lt;li&gt;&lt;b&gt;multiple arguments&lt;/b&gt; : String, Object, Array, ...
  48. * &lt;div class=&quot;sub-desc&quot;&gt;
  49. * Multiple arguments will be combined with &lt;code&gt;join('')&lt;/code&gt;.
  50. * &lt;pre&gt;&lt;code&gt;
  51. var t = new Ext.Template(
  52. '&amp;lt;div name=&quot;{id}&quot;&amp;gt;',
  53. '&amp;lt;span class=&quot;{cls}&quot;&amp;gt;{name} {value}&amp;lt;/span&amp;gt;',
  54. '&amp;lt;/div&amp;gt;',
  55. // a configuration object:
  56. {
  57. compiled: true, // {@link #compile} immediately
  58. disableFormats: true // See Notes below.
  59. }
  60. );
  61. * &lt;/code&gt;&lt;/pre&gt;
  62. * &lt;p&gt;&lt;b&gt;Notes&lt;/b&gt;:&lt;/p&gt;
  63. * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
  64. * &lt;li&gt;Formatting and &lt;code&gt;disableFormats&lt;/code&gt; are not applicable for Ext Core.&lt;/li&gt;
  65. * &lt;li&gt;For a list of available format functions, see {@link Ext.util.Format}.&lt;/li&gt;
  66. * &lt;li&gt;&lt;code&gt;disableFormats&lt;/code&gt; reduces &lt;code&gt;{@link #apply}&lt;/code&gt; time
  67. * when no formatting is required.&lt;/li&gt;
  68. * &lt;/ul&gt;&lt;/div&gt;
  69. * &lt;/div&gt;&lt;/li&gt;
  70. * &lt;/ul&gt;&lt;/div&gt;
  71. * @param {Mixed} config
  72. */
  73. Ext.Template = function(html){
  74. var me = this,
  75. a = arguments,
  76. buf = [],
  77. v;
  78. if (Ext.isArray(html)) {
  79. html = html.join(&quot;&quot;);
  80. } else if (a.length &gt; 1) {
  81. for(var i = 0, len = a.length; i &lt; len; i++){
  82. v = a[i];
  83. if(typeof v == 'object'){
  84. Ext.apply(me, v);
  85. } else {
  86. buf.push(v);
  87. }
  88. };
  89. html = buf.join('');
  90. }
  91. <span id='Ext-Template-property-html'> /**@private*/
  92. </span> me.html = html;
  93. <span id='Ext-Template-cfg-compiled'> /**
  94. </span> * @cfg {Boolean} compiled Specify &lt;tt&gt;true&lt;/tt&gt; to compile the template
  95. * immediately (see &lt;code&gt;{@link #compile}&lt;/code&gt;).
  96. * Defaults to &lt;tt&gt;false&lt;/tt&gt;.
  97. */
  98. if (me.compiled) {
  99. me.compile();
  100. }
  101. };
  102. Ext.Template.prototype = {
  103. <span id='Ext-Template-cfg-re'> /**
  104. </span> * @cfg {RegExp} re The regular expression used to match template variables.
  105. * Defaults to:&lt;pre&gt;&lt;code&gt;
  106. * re : /\{([\w\-]+)\}/g // for Ext Core
  107. * re : /\{([\w\-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g // for Ext JS
  108. * &lt;/code&gt;&lt;/pre&gt;
  109. */
  110. re : /\{([\w\-]+)\}/g,
  111. <span id='Ext-Template-property-re'> /**
  112. </span> * See &lt;code&gt;{@link #re}&lt;/code&gt;.
  113. * @type RegExp
  114. * @property re
  115. */
  116. <span id='Ext-Template-method-applyTemplate'> /**
  117. </span> * Returns an HTML fragment of this template with the specified &lt;code&gt;values&lt;/code&gt; applied.
  118. * @param {Object/Array} values
  119. * The template values. Can be an array if the params are numeric (i.e. &lt;code&gt;{0}&lt;/code&gt;)
  120. * or an object (i.e. &lt;code&gt;{foo: 'bar'}&lt;/code&gt;).
  121. * @return {String} The HTML fragment
  122. */
  123. applyTemplate : function(values){
  124. var me = this;
  125. return me.compiled ?
  126. me.compiled(values) :
  127. me.html.replace(me.re, function(m, name){
  128. return values[name] !== undefined ? values[name] : &quot;&quot;;
  129. });
  130. },
  131. <span id='Ext-Template-method-set'> /**
  132. </span> * Sets the HTML used as the template and optionally compiles it.
  133. * @param {String} html
  134. * @param {Boolean} compile (optional) True to compile the template (defaults to undefined)
  135. * @return {Ext.Template} this
  136. */
  137. set : function(html, compile){
  138. var me = this;
  139. me.html = html;
  140. me.compiled = null;
  141. return compile ? me.compile() : me;
  142. },
  143. <span id='Ext-Template-method-compile'> /**
  144. </span> * Compiles the template into an internal function, eliminating the RegEx overhead.
  145. * @return {Ext.Template} this
  146. */
  147. compile : function(){
  148. var me = this,
  149. sep = Ext.isGecko ? &quot;+&quot; : &quot;,&quot;;
  150. function fn(m, name){
  151. name = &quot;values['&quot; + name + &quot;']&quot;;
  152. return &quot;'&quot;+ sep + '(' + name + &quot; == undefined ? '' : &quot; + name + ')' + sep + &quot;'&quot;;
  153. }
  154. eval(&quot;this.compiled = function(values){ return &quot; + (Ext.isGecko ? &quot;'&quot; : &quot;['&quot;) +
  155. me.html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, &quot;\\'&quot;).replace(this.re, fn) +
  156. (Ext.isGecko ? &quot;';};&quot; : &quot;'].join('');};&quot;));
  157. return me;
  158. },
  159. <span id='Ext-Template-method-insertFirst'> /**
  160. </span> * Applies the supplied values to the template and inserts the new node(s) as the first child of el.
  161. * @param {Mixed} el The context element
  162. * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
  163. * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  164. * @return {HTMLElement/Ext.Element} The new node or Element
  165. */
  166. insertFirst: function(el, values, returnElement){
  167. return this.doInsert('afterBegin', el, values, returnElement);
  168. },
  169. <span id='Ext-Template-method-insertBefore'> /**
  170. </span> * Applies the supplied values to the template and inserts the new node(s) before el.
  171. * @param {Mixed} el The context element
  172. * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
  173. * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  174. * @return {HTMLElement/Ext.Element} The new node or Element
  175. */
  176. insertBefore: function(el, values, returnElement){
  177. return this.doInsert('beforeBegin', el, values, returnElement);
  178. },
  179. <span id='Ext-Template-method-insertAfter'> /**
  180. </span> * Applies the supplied values to the template and inserts the new node(s) after el.
  181. * @param {Mixed} el The context element
  182. * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
  183. * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  184. * @return {HTMLElement/Ext.Element} The new node or Element
  185. */
  186. insertAfter : function(el, values, returnElement){
  187. return this.doInsert('afterEnd', el, values, returnElement);
  188. },
  189. <span id='Ext-Template-method-append'> /**
  190. </span> * Applies the supplied &lt;code&gt;values&lt;/code&gt; to the template and appends
  191. * the new node(s) to the specified &lt;code&gt;el&lt;/code&gt;.
  192. * &lt;p&gt;For example usage {@link #Template see the constructor}.&lt;/p&gt;
  193. * @param {Mixed} el The context element
  194. * @param {Object/Array} values
  195. * The template values. Can be an array if the params are numeric (i.e. &lt;code&gt;{0}&lt;/code&gt;)
  196. * or an object (i.e. &lt;code&gt;{foo: 'bar'}&lt;/code&gt;).
  197. * @param {Boolean} returnElement (optional) true to return an Ext.Element (defaults to undefined)
  198. * @return {HTMLElement/Ext.Element} The new node or Element
  199. */
  200. append : function(el, values, returnElement){
  201. return this.doInsert('beforeEnd', el, values, returnElement);
  202. },
  203. doInsert : function(where, el, values, returnEl){
  204. el = Ext.getDom(el);
  205. var newNode = Ext.DomHelper.insertHtml(where, el, this.applyTemplate(values));
  206. return returnEl ? Ext.get(newNode, true) : newNode;
  207. },
  208. <span id='Ext-Template-method-overwrite'> /**
  209. </span> * Applies the supplied values to the template and overwrites the content of el with the new node(s).
  210. * @param {Mixed} el The context element
  211. * @param {Object/Array} values The template values. Can be an array if your params are numeric (i.e. {0}) or an object (i.e. {foo: 'bar'})
  212. * @param {Boolean} returnElement (optional) true to return a Ext.Element (defaults to undefined)
  213. * @return {HTMLElement/Ext.Element} The new node or Element
  214. */
  215. overwrite : function(el, values, returnElement){
  216. el = Ext.getDom(el);
  217. el.innerHTML = this.applyTemplate(values);
  218. return returnElement ? Ext.get(el.firstChild, true) : el.firstChild;
  219. }
  220. };
  221. <span id='Ext-Template-method-apply'>/**
  222. </span> * Alias for {@link #applyTemplate}
  223. * Returns an HTML fragment of this template with the specified &lt;code&gt;values&lt;/code&gt; applied.
  224. * @param {Object/Array} values
  225. * The template values. Can be an array if the params are numeric (i.e. &lt;code&gt;{0}&lt;/code&gt;)
  226. * or an object (i.e. &lt;code&gt;{foo: 'bar'}&lt;/code&gt;).
  227. * @return {String} The HTML fragment
  228. * @member Ext.Template
  229. * @method apply
  230. */
  231. Ext.Template.prototype.apply = Ext.Template.prototype.applyTemplate;
  232. <span id='Ext-Template-static-method-from'>/**
  233. </span> * Creates a template from the passed element's value (&lt;i&gt;display:none&lt;/i&gt; textarea, preferred) or innerHTML.
  234. * @param {String/HTMLElement} el A DOM element or its id
  235. * @param {Object} config A configuration object
  236. * @return {Ext.Template} The created template
  237. * @static
  238. */
  239. Ext.Template.from = function(el, config){
  240. el = Ext.getDom(el);
  241. return new Ext.Template(el.value || el.innerHTML, config || '');
  242. };
  243. </pre>
  244. </body>
  245. </html>