API Docs for: 1.0.0
Show:

File: Resources/public/js/views/subitem/ez-subitemlistview.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-subitemlistview', function (Y) {
  6. "use strict";
  7. /**
  8. * Provides the subitem list view. This module is deprecated
  9. *
  10. * @module ez-subitemlistview
  11. * @deprecated
  12. */
  13. Y.namespace('eZ');
  14.  
  15. var IS_PAGE_LOADING = 'is-page-loading',
  16. IS_DISABLED = 'is-disabled';
  17.  
  18. function linkIsDisabled(link) {
  19. return link.hasClass(IS_DISABLED);
  20. }
  21.  
  22. console.log('[DEPRECRATED] eZ.SubitemListView is deprecated');
  23. console.log('[DEPRECRATED] it will be removed from PlatformUI 2.0');
  24. console.log('[DEPRECRATED] use eZ.SubitemListMoreView instead');
  25.  
  26. /**
  27. * The subitem list view.
  28. *
  29. * @namespace eZ
  30. * @class SubitemListView
  31. * @deprecated
  32. * @constructor
  33. * @extends eZ.SubitemBaseView
  34. */
  35. Y.eZ.SubitemListView = Y.Base.create('subitemListView', Y.eZ.SubitemBaseView, [Y.eZ.AsynchronousView], {
  36. events: {
  37. '.ez-subitemlist-navigation-link': {
  38. 'tap': '_handlePagination',
  39. },
  40. },
  41.  
  42. initializer: function () {
  43. this._set('identifier', 'list');
  44. this._set('name', 'List view');
  45. this._fireMethod = this._fireLocationSearch;
  46. this._watchAttribute = 'subitems';
  47.  
  48. this.after(['subitemsChange', 'loadingErrorChange'], function (e) {
  49. this._set('loading', false);
  50. });
  51.  
  52. if ( this.get('loading') ) {
  53. this._uiPageLoading();
  54. }
  55. this.after('loadingChange', function () {
  56. if ( this.get('loading') ) {
  57. this._uiPageLoading();
  58. } else {
  59. this._uiPageEndLoading();
  60. }
  61. });
  62.  
  63. this.after('offsetChange', this._refresh);
  64.  
  65. this.get('location').after(['hiddenChange', 'invisibleChange'], Y.bind(this._refresh, this));
  66.  
  67. /**
  68. * Holds the displayed subitem list item view instances
  69. *
  70. * @property _itemViews
  71. * @type {Array}
  72. * @protected
  73. */
  74. this._itemViews = [];
  75.  
  76. this.after('*:editingPriorityChange', function (e) {
  77. if ( e.newVal ) {
  78. this._lockPriorityEdit(e.target);
  79. } else {
  80. this._unlockPriorityEdit();
  81. }
  82. });
  83. },
  84.  
  85. /**
  86. * Restores the `canEditPriority` attribute so that the priority can be
  87. * editing in all views
  88. *
  89. * @method _unlockPriorityEdit
  90. * @protected
  91. */
  92. _unlockPriorityEdit: function () {
  93. this._itemViews.forEach(function (itemView) {
  94. itemView.set('canEditPriority', true);
  95. });
  96. },
  97.  
  98. /**
  99. * Makes sure only one priority can be edited at a time.
  100. *
  101. * @protected
  102. * @method _lockPriorityEdit
  103. * @param {View} view the view where the priority occurs
  104. */
  105. _lockPriorityEdit: function (view) {
  106. this._itemViews.forEach(function (itemView) {
  107. itemView.set('canEditPriority', (itemView === view));
  108. });
  109. },
  110.  
  111. destructor: function () {
  112. this._destroyItemViews();
  113. },
  114.  
  115. /**
  116. * Refreshes the subitem list
  117. *
  118. * @protected
  119. * @method _refresh
  120. */
  121. _refresh: function () {
  122. if ( this.get('active') ) {
  123. this._fireLocationSearch();
  124. }
  125. },
  126.  
  127. /**
  128. * Sets the UI in the loading the state
  129. *
  130. * @protected
  131. * @method _uiPageLoading
  132. */
  133. _uiPageLoading: function () {
  134. this.get('container').addClass(IS_PAGE_LOADING);
  135. },
  136.  
  137. /**
  138. * Removes the loading state of the UI
  139. *
  140. * @method _uiPageEndLoading
  141. * @protected
  142. */
  143. _uiPageEndLoading: function () {
  144. this.get('container').removeClass(IS_PAGE_LOADING);
  145. },
  146.  
  147. /**
  148. * tap event handler on the navigation links. Changes the page if the
  149. * link is not disabled
  150. *
  151. * @method _handlePagination
  152. * @param {EventFacade} e
  153. * @protected
  154. */
  155. _handlePagination: function (e) {
  156. var type = e.target.getAttribute('rel');
  157.  
  158. e.preventDefault();
  159. if ( !linkIsDisabled(e.target) ) {
  160. this._getGotoMethod(type).call(this);
  161. }
  162. },
  163.  
  164. /**
  165. * Returns the *goto* function for the given type operation
  166. *
  167. * @method _getGotoMethod
  168. * @private
  169. * @param {String} type
  170. * @return {Function}
  171. */
  172. _getGotoMethod: function (type) {
  173. return this['_goto' + type.charAt(0).toUpperCase() + type.substr(1)];
  174. },
  175.  
  176. /**
  177. * Go to the first page
  178. *
  179. * @method _gotoFirst
  180. * @protected
  181. */
  182. _gotoFirst: function () {
  183. this.set('offset', 0);
  184. },
  185.  
  186. /**
  187. * Go to the next page
  188. *
  189. * @method _gotoNext
  190. * @protected
  191. */
  192. _gotoNext: function () {
  193. this.set('offset', this.get('offset') + this.get('limit'));
  194. },
  195.  
  196. /**
  197. * Go to the previous page
  198. *
  199. * @method _gotoPrev
  200. * @protected
  201. */
  202. _gotoPrev: function () {
  203. this.set('offset', this.get('offset') - this.get('limit'));
  204. },
  205.  
  206. /**
  207. * Go to the last page
  208. *
  209. * @method _gotoLast
  210. * @protected
  211. */
  212. _gotoLast: function () {
  213. var limit = this.get('limit');
  214.  
  215. this.set('offset', (Math.ceil(this.get('location').get('childCount') / limit) - 1) * limit);
  216. },
  217.  
  218. render: function () {
  219. this.get('container').setHTML(this.template({
  220. location: this.get('location').toJSON(),
  221. subitems: this._convertToJSONList(),
  222. loadingError: this.get('loadingError'),
  223. isFirst: this._isFirstPage(),
  224. isLast: this._isLastPage(),
  225. hasPages: this._hasPages(),
  226. columns: this._getColumns(),
  227. }));
  228. this._renderItems();
  229. return this;
  230. },
  231.  
  232. /**
  233. * Returns an array of objects describing the columns to add to the
  234. * list. Each object contains an `identifier` and a `name`.
  235. *
  236. * @method _getColumns
  237. * @protected
  238. * @return Array
  239. */
  240. _getColumns: function () {
  241. return this.get('displayedProperties').map(function (identifier) {
  242. return {
  243. name: this.get('propertyNames')[identifier],
  244. identifier: identifier,
  245. };
  246. }, this);
  247. },
  248.  
  249. /**
  250. * Destroys the instantiated item views.
  251. *
  252. * @method _destroyItemViews
  253. * @protected
  254. */
  255. _destroyItemViews: function () {
  256. this._itemViews.forEach(function(view) {
  257. view.destroy();
  258. });
  259. this._itemViews = [];
  260. },
  261.  
  262. /**
  263. * Renders an item view per subitem.
  264. *
  265. * @protected
  266. * @method _renderItems
  267. */
  268. _renderItems: function () {
  269. var contentNode = this.get('container').one('.ez-subitemlist-content'),
  270. ItemView = this.get('itemViewConstructor');
  271.  
  272. if ( !this.get('subitems') ) {
  273. return;
  274. }
  275. this._destroyItemViews();
  276. this.get('subitems').forEach(function (struct) {
  277. var view = new ItemView({
  278. displayedProperties: this.get('displayedProperties'),
  279. location: struct.location,
  280. content: struct.content,
  281. contentType: struct.contentType,
  282. bubbleTargets: this,
  283. });
  284.  
  285. this._itemViews.push(view);
  286. contentNode.append(view.render().get('container'));
  287. }, this);
  288. },
  289.  
  290. /**
  291. * Checks whether the pagination will be useful
  292. *
  293. * @method _hasPages
  294. * @private
  295. * @return {Boolean}
  296. */
  297. _hasPages: function () {
  298. return this.get('location').get('childCount') > this.get('limit');
  299. },
  300.  
  301. /**
  302. * Checks whether the user is on the first "page".
  303. *
  304. * @method _isFirstPage
  305. * @private
  306. * @return {Boolean}
  307. */
  308. _isFirstPage: function () {
  309. return (this.get('offset') === 0);
  310. },
  311.  
  312. /**
  313. * Checks whether the user is on the last "page".
  314. *
  315. * @method _isLastPage
  316. * @private
  317. * @return {Boolean}
  318. */
  319. _isLastPage: function () {
  320. return this.get('offset') >= (this.get('location').get('childCount') - this.get('limit'));
  321. },
  322.  
  323. /**
  324. * Converts the subitems array to JSON so that it can be used in the
  325. * template.
  326. * **Deprecated:** this method and the corresponding `subitems` template
  327. * variable will be removed in PlatformUI 2.0
  328. *
  329. * @method _convertToJSONList
  330. * @protected
  331. * @deprecated in 1.3
  332. * @return undefined|Array
  333. */
  334. _convertToJSONList: function () {
  335. if ( !this.get('subitems') ) {
  336. return this.get('subitems');
  337. }
  338. return Y.Array.map(this.get('subitems'), function (locStruct) {
  339. return locStruct.location.toJSON();
  340. });
  341. },
  342.  
  343. /**
  344. * Fires the `locationSearch` event to fetch the subitems of the
  345. * currently displayed Location.
  346. *
  347. * @method _fireLocationSearch
  348. * @protected
  349. */
  350. _fireLocationSearch: function () {
  351. var location = this.get('location'),
  352. locationId = location.get('locationId');
  353.  
  354. if ( !location.get('childCount') ) {
  355. this._set('loading', false);
  356. return;
  357. }
  358. this._set('loading', true);
  359. this.fire('locationSearch', {
  360. viewName: 'subitemlist-' + locationId,
  361. resultAttribute: 'subitems',
  362. loadContentType: true,
  363. loadContent: true,
  364. search: {
  365. criteria: {
  366. "ParentLocationIdCriterion": locationId,
  367. },
  368. offset: this.get('offset'),
  369. limit: this.get('limit'),
  370. /*
  371. * @TODO see https://jira.ez.no/browse/EZP-24315
  372. * this is not yet supported by the views in the REST API
  373. sortClauses: {
  374. SortClause: {
  375. SortField: this.get('location').get('sortField'),
  376. SortOrder: this.get('location').get('sortOrder'),
  377. },
  378. },
  379. */
  380. },
  381. });
  382. },
  383. }, {
  384. ATTRS: {
  385. /**
  386. * Indicates the whether the subitem list is currently in loading.
  387. * The default value depends on the number of subitems.
  388. *
  389. * @attribute loading
  390. * @type {Boolean}
  391. * @readOnly
  392. */
  393. loading: {
  394. valueFn: function () {
  395. return this.get('location').get('childCount') > 0;
  396. },
  397. readOnly: true,
  398. },
  399.  
  400. /**
  401. * The max number of the Locations to display in the subitem list
  402. * per "page".
  403. *
  404. * @attribute limit
  405. * @default 10
  406. * @type Number
  407. */
  408. limit: {
  409. value: 10,
  410. },
  411.  
  412. /**
  413. * The offset in the Location list.
  414. *
  415. * @attribute offset
  416. * @default 0
  417. * @type Number
  418. */
  419. offset: {
  420. value: 0,
  421. },
  422.  
  423. /**
  424. * The subitems list.
  425. *
  426. * @attribute subitems
  427. * @type Array of {Object} array containing location structs
  428. */
  429. subitems: {},
  430.  
  431. /**
  432. * The properties to display
  433. *
  434. * @attribute displayedProperties
  435. * @type Array
  436. */
  437. displayedProperties: {
  438. value: ['name', 'lastModificationDate', 'contentType', 'priority', 'translations'],
  439. },
  440.  
  441. /**
  442. * A key value object to store the human readable names of the
  443. * columns.
  444. *
  445. * @attribute propertyNames
  446. * @type {Object}
  447. */
  448. propertyNames: {
  449. value: {
  450. 'name': 'Name',
  451. 'lastModificationDate': 'Modified',
  452. 'contentType': 'Content type',
  453. 'priority': 'Priority',
  454. 'translations': 'Translations',
  455. }
  456. },
  457.  
  458. /**
  459. * The constructor function to use to instance the item view
  460. * instances.
  461. *
  462. * @attribute itemViewConstructor
  463. * @type {Function}
  464. * @default {Y.eZ.SubitemListItemView}
  465. */
  466. itemViewConstructor: {
  467. valueFn: function () {
  468. return Y.eZ.SubitemListItemView;
  469. },
  470. },
  471. }
  472. });
  473. });
  474.