Show:
  1. A.HTMLScreen = A.Base.create('htmlScreen', A.Screen, [], {
  2. /**
  3. * Holds the IO request.
  4. *
  5. * @property _request
  6. * @type {Object}
  7. * @protected
  8. */
  9. _request: null,
  10. /**
  11. * Aborts any outstanding request.
  12. *
  13. * @method abortRequest
  14. */
  15. abortRequest: function() {
  16. if (this._request) {
  17. this._request.abort();
  18. }
  19. },
  20. /**
  21. * Returns content for given surface from the provided content.
  22. *
  23. * @method getSurfaceContent
  24. * @param {String} surfaceId The ID of the surface, which content should be
  25. * loaded.
  26. * @contents {Node} The content container from which the surface content
  27. * will be retrieved.
  28. * @return {String|Node} String or Node instance which contains the content
  29. * of the surface.
  30. */
  31. getSurfaceContent: function(surfaceId, contents) {
  32. var frag = contents.one('#' + surfaceId);
  33. if (frag) {
  34. return frag.getHTML();
  35. }
  36. },
  37. /**
  38. * Loads the content for all surfaces in one AJAX request from the server.
  39. *
  40. * @method load
  41. * @return {CancellablePromise} Promise, which should be resolved with the
  42. * returned content from the server.
  43. */
  44. load: function(path) {
  45. var cache = this.getCache(),
  46. url;
  47. if (A.Lang.isValue(cache)) {
  48. return A.CancellablePromise.resolve(cache);
  49. }
  50. url = new A.Url(path);
  51. url.addParameters(this.get('urlParams'));
  52. return this.loadContent(url.toString());
  53. },
  54. /**
  55. * Loads the content from server via single AJAX request.
  56. *
  57. * @method loadContent
  58. * @protected
  59. * @return {CancellablePromise} Promise, which should be resolved with the returned
  60. * content from the server.
  61. */
  62. loadContent: function(path) {
  63. var instance = this,
  64. promise;
  65. instance.abortRequest();
  66. promise = new A.CancellablePromise(
  67. function(resolve) {
  68. var params = {
  69. headers: {
  70. 'X-PJAX': 'true'
  71. },
  72. method: instance.get('method'),
  73. on: {
  74. failure: function(id, response) {
  75. promise.cancel(response.responseText);
  76. },
  77. success: function(id, response) {
  78. var frag = A.Node.create('<div/>');
  79. frag.append(response.responseText);
  80. instance._setScreenTitleFromFragment(frag);
  81. instance.addCache(frag);
  82. resolve(frag);
  83. }
  84. },
  85. timeout: instance.get('timeout')
  86. };
  87. // For Internet Explorer and MSIE don't rely on the caching mechanism
  88. // http://www.greenvilleweb.us/how-to-web-design/problem-with-ie-9-caching-ajax-get-request/
  89. if (A.UA.edge || A.UA.ie) {
  90. params.cache = false;
  91. }
  92. instance._request = A.io(path, params);
  93. },
  94. function() {
  95. instance.abortRequest();
  96. }
  97. );
  98. return promise;
  99. },
  100. /**
  101. * Setter for urlParams attribute.
  102. *
  103. * @method _setUrlParams
  104. * @protected
  105. * @return {String|Object} If the provided value was string, it will be
  106. * converted to Object with one property - the provided string and "1"
  107. * as value. Otherwise, the provided object will be passed directly to
  108. * the attribute value.
  109. */
  110. _setUrlParams: function(val) {
  111. var params = val;
  112. if (A.Lang.isString(val)) {
  113. params = {};
  114. params[val] = 1;
  115. }
  116. return params;
  117. },
  118. /**
  119. * Retrieves the title from the provided content and sets it to title
  120. * attribute of the class.
  121. *
  122. * @method _setScreenTitleFromFragment
  123. * @param {Node} frag The container from which the title should be
  124. * retrieve.
  125. */
  126. _setScreenTitleFromFragment: function(frag) {
  127. var title = frag.one(this.get('titleSelector'));
  128. if (title) {
  129. this.set('title', title.get('text'));
  130. }
  131. },
  132. /**
  133. * Validates the value of urlParams. The value could be String or Object
  134. * with key and values. During URL construction, they will be added to the
  135. * other parameters.
  136. *
  137. * @method _validateUrlParams
  138. * @protected
  139. * @return {Boolean} true if val is String or an Object.
  140. */
  141. _validateUrlParams: function(val) {
  142. return A.Lang.isString(val) || A.Lang.isObject(val);
  143. }
  144. }, {
  145. ATTRS: {
  146. /**
  147. * @attribute cacheable
  148. * @default true
  149. * @type Boolean
  150. */
  151. cacheable: {
  152. value: true
  153. },
  154. /**
  155. * Ajax request method.
  156. *
  157. * @attribute method
  158. * @type {String}
  159. * @default GET
  160. **/
  161. method: {
  162. validator: A.Lang.isString,
  163. value: 'GET'
  164. },
  165. /**
  166. * CSS selector used to extract a page title from the content of a page
  167. * loaded via Pjax.
  168. *
  169. * By default this is set to extract the title from the `<title>`
  170. * element, but you could customize it to extract the title from an
  171. * `<h1>`, or from any other element, if that's more appropriate for the
  172. * content you're loading.
  173. *
  174. * @attribute titleSelector
  175. * @type String
  176. * @default "title"
  177. **/
  178. titleSelector: {
  179. value: 'title'
  180. },
  181. /**
  182. * Time in milliseconds after which an Ajax request should time out.
  183. *
  184. * @attribute timeout
  185. * @type {Number}
  186. * @default 30000
  187. **/
  188. timeout: {
  189. value: 30000
  190. },
  191. /**
  192. * Could be String or Object with multiple keys and values. If String,
  193. * the defaule value will be "1". If an Object with multiple keys and
  194. * values, they will be concatenated to the URL.
  195. *
  196. * @attribute urlParams
  197. * @type {String|Object}
  198. * @default pjax
  199. */
  200. urlParams: {
  201. setter: '_setUrlParams',
  202. validator: '_validateUrlParams',
  203. value: 'pjax'
  204. }
  205. }
  206. });