API Docs for: 1.0.0
Show:

File: Resources/public/js/views/universaldiscovery/ez-universaldiscoveryfinderexplorerlevelview.js

  1. /*
  2. * Copyright (C) eZ Systems AS. All rights reserved.
  3. * For full copyright and license information view LICENSE file distributed with this source code.
  4. */
  5. YUI.add('ez-universaldiscoveryfinderexplorerlevelview', function (Y) {
  6. "use strict";
  7. /**
  8. * Provides the universal discovery finder explorer level view
  9. *
  10. * @module ez-universaldiscoveryfinderexplorerlevelview
  11. */
  12. Y.namespace('eZ');
  13.  
  14. var events = {
  15. '.ez-explorer-level-item': {
  16. 'tap': '_fireExplorerNavigate',
  17. }
  18. },
  19. viewName = 'universalDiscoveryFinderExplorerLevelView',
  20. HAS_SELECTED_ITEM = 'has-selected-item',
  21. IS_LOADING = 'is-loading',
  22. IS_DISABLED = 'is-disabled';
  23.  
  24. /**
  25. * The universal discovery finder explorer level. It shows content of a given depth
  26. *
  27. * @namespace eZ
  28. * @class UniversalDiscoveryFinderExplorerLevelView
  29. * @constructor
  30. * @extends eZ.TemplateBasedView
  31. */
  32. Y.eZ.UniversalDiscoveryFinderExplorerLevelView = Y.Base.create(viewName, Y.eZ.TemplateBasedView, [Y.eZ.AsynchronousView], {
  33. initializer: function () {
  34. var container = this.get('container');
  35.  
  36. /**
  37. * FLag to indicate if we are currently watching the scroll and trying to load items.
  38. *
  39. * @property _watchingScroll
  40. * @type Boolean
  41. * @protected
  42. */
  43. this._watchingScroll = true;
  44. this._fireMethod = this._fireLocationSearch;
  45. this._watchAttribute = 'items';
  46. container.addClass(IS_LOADING);
  47.  
  48. this.after('activeChange', function () {
  49. if (!this.get('active')) {
  50. this.get('container').addClass(IS_LOADING);
  51. }
  52. });
  53. this.on('itemsChange', function () {
  54. this._watchingScroll = true;
  55. container.removeClass(IS_LOADING);
  56. });
  57. this.after('offsetChange', function () {
  58. this._watchingScroll = false;
  59. this.get('container').addClass(IS_LOADING);
  60. this._fireLocationSearch();
  61. });
  62. container.plug(Y.Plugin.ScrollInfo);
  63. container.scrollInfo.on('scrollDown', this._handleScroll, this);
  64.  
  65. this.after('ownSelectedItemChange', this._uiOwnSelectedItem);
  66. this._uiDisableLevelView();
  67. this._uiOwnSelectedItem();
  68. this._addDOMEventHandlers(events);
  69. },
  70.  
  71. /**
  72. * Adds the is disabled item class on the container
  73. * depending on the `disabled` attribute value.
  74. *
  75. * @method _uiDisableLevelView
  76. * @protected
  77. */
  78. _uiDisableLevelView: function () {
  79. var container = this.get('container');
  80.  
  81. if (this.get('disabled')) {
  82. container.addClass(IS_DISABLED);
  83. }
  84. },
  85.  
  86. /**
  87. * Adds or removes the has selected item class on the container
  88. * depending on the `ownSelectedItem` attribute value.
  89. *
  90. * @method _uiOwnSelectedItem
  91. * @protected
  92. */
  93. _uiOwnSelectedItem: function () {
  94. var container = this.get('container');
  95.  
  96. if (!this.get('ownSelectedItem')) {
  97. container.removeClass(HAS_SELECTED_ITEM);
  98. } else {
  99. container.addClass(HAS_SELECTED_ITEM);
  100. }
  101. },
  102.  
  103. render: function () {
  104. var container = this.get('container'),
  105. itemsJSONified = [];
  106.  
  107. Y.Array.each(this.get('items'), function (item) {
  108. itemsJSONified.push({
  109. location: item.location.toJSON(),
  110. contentInfo: item.location.get('contentInfo').toJSON(),
  111. selectedLocationId: item.location.get('locationId') == this.get('selectLocationId') ? true : false,
  112. });
  113. }, this);
  114. container.setHTML(this.template({
  115. items: itemsJSONified,
  116. loadingError: this.get('loadingError'),
  117. }));
  118. return this;
  119. },
  120.  
  121. /**
  122. * Custom reset implementation to explicitly reset the items.
  123. *
  124. * @method reset
  125. */
  126. reset: function (name) {
  127. if (name == 'items') {
  128. this.set('items', null);
  129. } else {
  130. this.constructor.superclass.reset.apply(this, arguments);
  131. }
  132. },
  133.  
  134. /**
  135. * Scrolls to fully display the levelView.
  136. *
  137. * @method displayLevelView
  138. */
  139. displayLevelView: function () {
  140. this.get('container').one('.ez-ud-finder-explorerlevel-anchor').scrollIntoView({behavior: "smooth"});
  141. },
  142.  
  143. /**
  144. * Handles user scrolling to determine if it needs to load more items.
  145. *
  146. * @method _handleScroll
  147. * @protected
  148. */
  149. _handleScroll: function () {
  150. var offset = this.get('offset'),
  151. limit = this.get('limit');
  152.  
  153. if (this._watchingScroll && this._hasScrolledEnough() && limit + offset < this.get('childCount')) {
  154. this.set('offset', offset + limit);
  155. }
  156. },
  157.  
  158. /**
  159. * Determines if user has scrolled enough to try to load other items.
  160. *
  161. * @method _hasScrolledEnough
  162. * @protected
  163. */
  164. _hasScrolledEnough: function () {
  165. var container = this.get('container');
  166.  
  167. return container.get('scrollHeight') - container.get('scrollTop') <= 2 * container.get('offsetHeight');
  168. },
  169.  
  170. /**
  171. * Fires the `locationSearch` event to fetch the result list of the search.
  172. *
  173. * @method _fireLocationSearch
  174. * @protected
  175. */
  176. _fireLocationSearch: function () {
  177. this.fire('locationSearch', {
  178. viewName: 'udwexplorerlevel-',
  179. resultAttribute: 'items',
  180. resultTotalCountAttribute: 'childCount',
  181. loadContent: true,
  182. loadContentType: true,
  183. search: {
  184. filter: {
  185. "ParentLocationIdCriterion": this.get('parentLocation').get('locationId'),
  186. },
  187. sortLocation: this.get('parentLocation'),
  188. offset: this.get('offset'),
  189. limit: this.get('limit'),
  190. },
  191. });
  192. },
  193.  
  194. /**
  195. * items attribute setter. It sets items attribute with the results.
  196. *
  197. * @method _setItems
  198. * @param {Array} newItems
  199. * @protected
  200. */
  201. _setItems: function (newItems) {
  202. var items = [];
  203.  
  204. Y.Array.each(newItems, function (hit) {
  205. var location = hit.location,
  206. contentType = hit.contentType,
  207. content = hit.content,
  208. data = {
  209. location: location,
  210. contentInfo: location.get('contentInfo'),
  211. contentType: contentType,
  212. content: content,
  213. };
  214.  
  215. items.push(data);
  216. }, this);
  217. if (this.get('items')) {
  218. return this.get('items').concat(items);
  219. } else {
  220. return items;
  221. }
  222. },
  223.  
  224. /**
  225. * Finds location in item by a given locationId and returns the item.
  226. *
  227. * @method _findItemByLocationId
  228. * @param {Number} locationId
  229. * @protected
  230. * @return {Object} Returns an item containing a location.
  231. */
  232. _findItemByLocationId: function (locationId) {
  233. var item;
  234.  
  235. Y.Array.each(this.get('items'), function (hit) {
  236. if (hit.location.get('locationId') == locationId) {
  237. item = hit;
  238. }
  239. }, this);
  240. return item;
  241. },
  242.  
  243. /**
  244. * Fires the `explorerNavigate` event to explore the chosen location.
  245. *
  246. * @method _fireExplorerNavigate
  247. * @protected
  248. */
  249. _fireExplorerNavigate: function (e) {
  250. var nodeLocationId = e.target.getData('location-id'),
  251. item = this._findItemByLocationId(nodeLocationId);
  252.  
  253. if (!this.get('disabled') && (this.get('selectLocationId') != nodeLocationId || !this.get('ownSelectedItem')) && item) {
  254. this.set('selectLocationId', nodeLocationId);
  255.  
  256. /**
  257. * Navigates to the given item's location in the explorer.
  258. *
  259. * @event explorerNavigate
  260. * @param {Object} data
  261. * @param {eZ.Location} location
  262. * @param {Number} depth
  263. *
  264. */
  265. this.fire('explorerNavigate', {
  266. data: item,
  267. location: item.location,
  268. depth: this.get('depth')
  269. });
  270. }
  271. },
  272. }, {
  273. ATTRS: {
  274. /**
  275. * The items to display
  276. *
  277. * @attribute items
  278. * @type Array
  279. */
  280. items: {
  281. setter: function (resultList) {
  282. if (resultList !== null) {
  283. return this._setItems(resultList);
  284. }
  285. }
  286. },
  287.  
  288. /**
  289. * Defines if the level view own the current selected item
  290. *
  291. * @attribute ownSelecteditem
  292. * @type Boolean
  293. */
  294. ownSelectedItem: {
  295. value: false
  296. },
  297.  
  298. /**
  299. * The location id of the selected item
  300. *
  301. * @attribute selectLocationId
  302. * @default 0
  303. * @type Number
  304. */
  305. selectLocationId: {
  306. value: 0
  307. },
  308.  
  309. /**
  310. * The depth of the level view (the first level view depth is 1)
  311. *
  312. * @attribute depth
  313. * @type Number
  314. */
  315. depth: {},
  316.  
  317. /**
  318. * The offset to start the locationSearch to
  319. *
  320. * @attribute offset
  321. * @type Number
  322. */
  323. offset: {
  324. value: 0
  325. },
  326.  
  327. /**
  328. * The limit of the results
  329. *
  330. * @attribute limit
  331. * @type Number
  332. */
  333. limit: {
  334. value: 50
  335. },
  336.  
  337. /**
  338. * The number of total child the parent location has.
  339. *
  340. * @attribute childCount
  341. * @type Number
  342. */
  343. childCount: {},
  344.  
  345. /**
  346. * Boolean that tell if the view is disabled or not.
  347. * The items of a disabled level view can not be explored.
  348. *
  349. * @attribute disabled
  350. * @writeOnce
  351. * @default false
  352. * @type Boolean
  353. */
  354. disabled: {
  355. writeOnce: 'initOnly',
  356. value: false,
  357. },
  358. },
  359. });
  360. });
  361.