API Docs for: 1.0.0
Show:

File: Resources/public/js/views/services/ez-navigationhubviewservice.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-navigationhubviewservice', function (Y) {
  6. "use strict";
  7. /**
  8. * Provides the view service component for the navigation hub
  9. *
  10. * @module ez-navigationhubviewservice
  11. */
  12. Y.namespace('eZ');
  13.  
  14. /**
  15. * Navigation hub view service.
  16. *
  17. * @namespace eZ
  18. * @class NavigationHubViewService
  19. * @constructor
  20. * @extends eZ.ViewService
  21. */
  22. Y.eZ.NavigationHubViewService = Y.Base.create('navigationHubViewService', Y.eZ.ViewService, [Y.eZ.SideViewService], {
  23. initializer: function () {
  24. this.on('*:logOut', this._logOut);
  25. this.after('*:activeNavigationChange', this._navigateToFirstItemRoute);
  26. },
  27.  
  28. /**
  29. * navigateTo event handler. it redirects the user to the given route.
  30. *
  31. * @method _navigateTo
  32. * @protected
  33. * @deprecated
  34. * @param {EventFacade} e
  35. */
  36. _navigateTo: function (e) {
  37. var route = e.route;
  38.  
  39. console.log('[DEPRECATED] _navigateTo is deprecated');
  40. console.log('[DEPRECATED] this method will be removed from PlatformUI 2.0');
  41. this.get('app').navigateTo(route.name, route.params);
  42. },
  43.  
  44. /**
  45. * logOut event handler, it logs out the user and redirect to the login
  46. * form.
  47. *
  48. * @method _logOut
  49. */
  50. _logOut: function () {
  51. var app = this.get('app');
  52.  
  53. app.set('loading', true);
  54. app.logOut(function () {
  55. app.navigateTo('loginForm');
  56. });
  57. },
  58.  
  59. /**
  60. * Returns true if a provided item is a NavigationItemView, false if item is a NavigationItemParameterView
  61. *
  62. * @method _isNavigationViewItem
  63. * @private
  64. * @param {Object} item
  65. * @return {Boolean}
  66. */
  67. _isNavigationViewItem: function (item) {
  68. return !!item.get;
  69. },
  70.  
  71. /**
  72. * Redirects to the first item of the active zone
  73. *
  74. * @method _navigateToFirstItemRoute
  75. * @protected
  76. * @param {EventFacade} e
  77. * @param {String} e.newVal new value of `activeNavigation`
  78. */
  79. _navigateToFirstItemRoute: function (e) {
  80. var zone = this.get(e.newVal + 'NavigationItems'),
  81. firstItem,
  82. route;
  83.  
  84. if (zone && zone.length > 0 && e.prevVal !== null) {
  85. firstItem = zone[0];
  86.  
  87. if (this._isNavigationViewItem(firstItem)) {
  88. route = firstItem.get('route');
  89. } else {
  90. route = firstItem.config.route;
  91. }
  92. this.get('app').navigateTo(route.name, route.params);
  93. }
  94. },
  95.  
  96. _load: function (next) {
  97. var api = this.get('capi'),
  98. loadError = false,
  99. service = this,
  100. discoveryService = api.getDiscoveryService(),
  101. contentService = api.getContentService(),
  102. user = this.get('app').get('user'),
  103. tasks = new Y.Parallel();
  104.  
  105. if (user.get('avatar')) {
  106. contentService.loadImageVariation(user.get('avatar').variations.platformui_profileview.href, tasks.add(function (error, response) { //jshint ignore:line
  107. if ( error && response.status !== 401 ) {
  108. // 401 error is ignored because the user might not have the right to read
  109. // his/her own avatar see https://jira.ez.no/browse/EZP-25522
  110. loadError = true;
  111. return;
  112. }
  113.  
  114. service.set('userAvatar', response.document.ContentImageVariation);
  115. }));
  116. }
  117.  
  118. discoveryService.getInfoObject('rootLocation', function (error, response){
  119. var rootLocationId;
  120.  
  121. if ( error ) {
  122. loadError = true;
  123. return;
  124. }
  125.  
  126. rootLocationId = response._href;
  127.  
  128. service._loadLocation(rootLocationId, 'rootLocation', tasks.add(function (error) {
  129. var params = {id: '', languageCode: ''};
  130.  
  131. if ( error ) {
  132. loadError = true;
  133. return;
  134. }
  135.  
  136. params.id = service.get('rootLocation').get('id');
  137. params.languageCode = service.get('rootLocation').get('contentInfo').get('mainLanguageCode');
  138.  
  139. service.getNavigationItem('content-structure').set(
  140. 'route',
  141. {name: 'viewLocation', params: params}
  142. );
  143. }));
  144. });
  145.  
  146. discoveryService.getInfoObject('rootMediaFolder', function (error, response){
  147. var rootMediaLocationId;
  148.  
  149. if ( error ) {
  150. loadError = true;
  151. return;
  152. }
  153.  
  154. rootMediaLocationId = response._href;
  155.  
  156. service._loadLocation(rootMediaLocationId, 'rootMediaLocation' , tasks.add(function (error){
  157. var params = {id: '', languageCode: ''};
  158.  
  159. if ( error ) {
  160. loadError = true;
  161. return;
  162. }
  163.  
  164. params.id = service.get('rootMediaLocation').get('id');
  165. params.languageCode = service.get('rootMediaLocation').get('contentInfo').get('mainLanguageCode');
  166.  
  167. service.getNavigationItem('media-library').set(
  168. 'route',
  169. {name: 'viewLocation', params: params}
  170. );
  171. }));
  172. });
  173.  
  174. tasks.done(function () {
  175. if ( loadError ) {
  176. service._error(Y.eZ.trans('failed.to.load.root.locations', {}, 'navigationhub'));
  177. return;
  178. }
  179.  
  180. next(service);
  181. });
  182. },
  183.  
  184. /**
  185. * Loads the given `locationId`
  186. *
  187. * @protected
  188. * @method _loadLocation
  189. * @param {Integer} locationId
  190. * @param {string} attributeName where data need to be loaded
  191. * @param {Function} callback the function to call when the location is loaded
  192. * @param {Boolean} callback.error the error, true if an error occurred
  193. * @param {ez.Location} callback.result the location object
  194. */
  195. _loadLocation: function (locationId, attributeName, callback) {
  196. var loadOptions = {
  197. api: this.get('capi')
  198. },
  199. location = this.get(attributeName);
  200.  
  201. location.set('id', locationId);
  202.  
  203. location.load(loadOptions, callback);
  204. },
  205.  
  206. /**
  207. * Returns a navigation item object. See the *NavigationItems attribute.
  208. *
  209. * @private
  210. * @method _getItem
  211. * @param {Function} constructor
  212. * @param {Object} config
  213. * @return {Object}
  214. */
  215. _getItem: function(constructor, config) {
  216. return {
  217. Constructor: constructor,
  218. config: config
  219. };
  220. },
  221.  
  222. /**
  223. * Returns a navigation item object describing a {{#crossLink
  224. * "eZ.NavigationItemView"}}eZ.NavigationItemView{{/crossLink}}
  225. *
  226. * @private
  227. * @method _getNavigationItem
  228. * @param {String} title
  229. * @param {String} identifier
  230. * @param {String} routeName
  231. * @param {Object} routeParams
  232. * @return {Object}
  233. */
  234. _getNavigationItem: function (title, identifier, routeName, routeParams) {
  235. return this._getItem(
  236. Y.eZ.NavigationItemView, {
  237. title: title,
  238. identifier: identifier,
  239. route: {
  240. name: routeName,
  241. params: routeParams
  242. }
  243. }
  244. );
  245. },
  246.  
  247. /**
  248. * Returns the navigation item object describing a {{#crossLink
  249. * "eZ.NavigationItemSubtreeView"}}eZ.NavigationItemSubtreeView{{/crossLink}}.
  250. *
  251. * @method _getSubtreeItem
  252. * @private
  253. * @param {String} title
  254. * @param {String} identifier
  255. * @param {String} locationId
  256. * @param {String} languageCode
  257. * @return {Object}
  258. */
  259. _getSubtreeItem: function (title, identifier, locationId, languageCode) {
  260. return new Y.eZ.NavigationItemSubtreeView( {
  261. title: title,
  262. identifier: identifier,
  263. route: {
  264. name: 'viewLocation',
  265. params: {
  266. id: locationId,
  267. languageCode: languageCode
  268. }
  269. }
  270. });
  271. },
  272.  
  273. /**
  274. * Returns a navigation item object describing a {{#crossLink
  275. * "eZ.NavigationItemParameterView"}}eZ.NavigationItemParameterView{{/crossLink}}
  276. *
  277. * @private
  278. * @method _getParameterItem
  279. * @param {String} title
  280. * @param {String} identifier
  281. * @param {String} routeName
  282. * @param {Object} routeParams
  283. * @param {String} matchParameter
  284. * @return {Object}
  285. */
  286. _getParameterItem: function (title, identifier, routeName, routeParams, matchParameter) {
  287. return this._getItem(
  288. Y.eZ.NavigationItemParameterView, {
  289. title: title,
  290. identifier: identifier,
  291. route: {
  292. name: routeName,
  293. params: routeParams,
  294. },
  295. matchParameter: matchParameter
  296. }
  297. );
  298. },
  299.  
  300. /**
  301. * Returns the parameters for the navigation hub view
  302. *
  303. * @method getViewParameters
  304. * @return {Object}
  305. */
  306. _getViewParameters: function () {
  307. return {
  308. user: this.get('app').get('user'),
  309. userAvatar: this.get('userAvatar'),
  310. platformNavigationItems: this.get('platformNavigationItems'),
  311. studioNavigationItems: this.get('studioNavigationItems'),
  312. studioplusNavigationItems: this.get('studioplusNavigationItems'),
  313. adminNavigationItems: this.get('adminNavigationItems'),
  314. matchedRoute: this._matchedRoute(),
  315. };
  316. },
  317.  
  318. /**
  319. * A matched route object from the request. This object will be used by
  320. * the navigation hub view to check which navigation item view is
  321. * selected. A matched route object is a clone of the application active
  322. * route without the service, serviceInstance and callbacks entries and
  323. * with an additional `parameters` property holding the route
  324. * parameters and their values.
  325. *
  326. * @method _matchedRoute
  327. * @protected
  328. * @return {Object}
  329. */
  330. _matchedRoute: function () {
  331. var request = this.get('request'),
  332. route = request.route,
  333. matchedRoute = Y.merge(route);
  334.  
  335. matchedRoute.parameters = request.params;
  336. delete matchedRoute.service;
  337. delete matchedRoute.serviceInstance;
  338. delete matchedRoute.callbacks;
  339.  
  340. return matchedRoute;
  341. },
  342.  
  343. /**
  344. * Adds a navigation item for the given zone.
  345. * Note: adding a navigation item will only work before the first
  346. * initialization of the navigation hub. As a result, in a navigation
  347. * hub view service plugin, this method should be called only in or from
  348. * the initializer method.
  349. *
  350. * @method addNavigationItem
  351. * @param {Object} item a navigation item, see the description of the
  352. * *NavigationItems attributes
  353. * @param {String} zone the identifier of the zone
  354. */
  355. addNavigationItem: function (item, zone) {
  356. this.get(zone + 'NavigationItems').push(item);
  357. },
  358.  
  359. /**
  360. * Removes a navigation item for the given zone.
  361. * Note: removing a navigation item will only work before the first
  362. * initialization of the navigation hub. As a result, in a navigation
  363. * hub view service plugin, this method should be called only in or from
  364. * the initializer method.
  365. *
  366. * @method removeNavigationItem
  367. * @param {String} identifier the identifier of the navigation item to
  368. * remove
  369. * @param {String} zone the identifier of the zone
  370. */
  371. removeNavigationItem: function (identifier, zone) {
  372. var attr = zone + 'NavigationItems';
  373.  
  374. this._set(attr, Y.Array.reject(this.get(attr), Y.bind(function (elt) {
  375. if (this._isNavigationViewItem(elt)) {
  376. return elt.get('identifier') === identifier;
  377. } else {
  378. return elt.config.identifier === identifier;
  379. }
  380. },this)));
  381. },
  382.  
  383. /**
  384. * Retrieves a navigation item
  385. *
  386. * @method getNavigationItem
  387. * @param {String} identifier the identifier of the navigation item to retrieve
  388. * @return {Object}
  389. */
  390. getNavigationItem: function(identifier) {
  391. var zones = ['platform', 'studio', 'studioplus', 'admin'],
  392. items = [];
  393.  
  394. Y.Array.each(zones, function (zone) {
  395. items = items.concat(this.get(zone + 'NavigationItems'));
  396. }, this);
  397.  
  398. return Y.Array.find(items, Y.bind(function (elt) {
  399. if (this._isNavigationViewItem(elt)) {
  400. return elt.get('identifier') === identifier;
  401. } else {
  402. return false;
  403. }
  404. }, this));
  405. }
  406. }, {
  407. ATTRS: {
  408.  
  409. /**
  410. * Stores the root `location`
  411. *
  412. * @attribute rootLocation
  413. * @type {eZ.Location}
  414. */
  415. rootLocation: {
  416. valueFn: function () {
  417. return new Y.eZ.Location();
  418. },
  419. },
  420.  
  421. /**
  422. * Stores the root media `location`
  423. *
  424. * @attribute rootMediaLocation
  425. * @type {eZ.Location}
  426. */
  427. rootMediaLocation: {
  428. valueFn: function () {
  429. return new Y.eZ.Location();
  430. },
  431. },
  432.  
  433. /**
  434. * Stores the navigation item objects for the 'platform' zone. Each
  435. * object must contain a `Constructor` property referencing
  436. * the constructor function to use to build the navigation item
  437. * view and a `config` property will be used as a configuration
  438. * object for the navigation item view. This configuration must
  439. * contain a `title` and an `identifier` properties.
  440. *
  441. * @attribute platformNavigationItems
  442. * @type Array
  443. * @default array containing the object the 'Content structure' and
  444. * 'Media library' items
  445. * @readOnly
  446. */
  447. platformNavigationItems: {
  448. getter: function (val) {
  449. if (val) {
  450. return val;
  451. }
  452.  
  453. val = [
  454. this._getSubtreeItem(
  455. Y.eZ.trans("navigationhub.content.structure", {}, 'navigationhub'),
  456. "content-structure",
  457. this.get('rootLocation').get('id'),
  458. this.get('rootLocation').get('contentInfo').get('mainLanguageCode')
  459. ),
  460. this._getSubtreeItem(
  461. Y.eZ.trans("navigationhub.media.library",{}, 'navigationhub'),
  462. "media-library",
  463. this.get('rootMediaLocation').get('id'),
  464. this.get('rootMediaLocation').get('contentInfo').get('mainLanguageCode')
  465. ),
  466. ];
  467.  
  468. this._set('platformNavigationItems', val);
  469. return val;
  470. },
  471. readOnly: true,
  472. },
  473.  
  474. /**
  475. * Stores the navigation item objects for the 'studioplus' zone. Each
  476. * object must contain a `Constructor` property referencing
  477. * the constructor function to use to build the navigation item
  478. * view and a `config` property will be used as a configuration
  479. * object for the navigation item view. This configuration must
  480. * contain a `title` and an `identifier` properties.
  481. *
  482. * @attribute studioplusNavigationItems
  483. * @type Array
  484. * @default empty array
  485. * @readOnly
  486. */
  487. studioplusNavigationItems: {
  488. getter: function (val) {
  489. if (val) {
  490. return val;
  491. }
  492.  
  493. val = [
  494. this._getNavigationItem(
  495. "eZ Studio Plus presentation", "studioplus-presentation",
  496. "studioPlusPresentation", {}
  497. ),
  498. ];
  499.  
  500. this._set('studioplusNavigationItems', val);
  501. return val;
  502. },
  503. readOnly: true,
  504. },
  505.  
  506. /**
  507. * Stores the navigation item objects for the 'studio' zone. Each
  508. * object must contain a `Constructor` property referencing
  509. * the constructor function to use to build the navigation item
  510. * view and a `config` property will be used as a configuration
  511. * object for the navigation item view. This configuration must
  512. * contain a `title` and an `identifier` properties.
  513. *
  514. * @attribute studioNavigationItems
  515. * @type Array
  516. * @default empty array
  517. * @readOnly
  518. */
  519. studioNavigationItems: {
  520. getter: function (val) {
  521. if (val) {
  522. return val;
  523. }
  524.  
  525. val = [
  526. this._getNavigationItem(
  527. "eZ Studio presentation", "studio-presentation",
  528. "studioPresentation", {}
  529. ),
  530. ];
  531.  
  532. this._set('studioNavigationItems', val);
  533. return val;
  534. },
  535. readOnly: true,
  536. },
  537.  
  538. /**
  539. * Stores the navigation item objects for the 'admin' zone. Each
  540. * object must contain a `Constructor` property referencing
  541. * the constructor function to use to build the navigation item
  542. * view and a `config` property will be used as a configuration
  543. * object for the navigation item view. This configuration must
  544. * contain a `title` and an `identifier` properties.
  545. *
  546. * @attribute adminNavigationItems
  547. * @type Array
  548. * @default array containing the items for the admin
  549. * @readOnly
  550. */
  551. adminNavigationItems: {
  552. getter: function (val) {
  553. if (val) {
  554. return val;
  555. }
  556.  
  557. val = [
  558. this._getParameterItem(
  559. Y.eZ.trans('navigationhub.dashboard.title', {}, 'navigationhub'), "admin-dashboard",
  560. "adminGenericRoute", {uri: "pjax/dashboard"}, "uri"
  561. ),
  562. this._getParameterItem(
  563. Y.eZ.trans('navigationhub.system.information', {}, 'navigationhub'), "admin-systeminfo",
  564. "adminGenericRoute", {uri: "pjax/systeminfo"}, "uri"
  565. ),
  566. this._getNavigationItem(
  567. Y.eZ.trans('navigationhub.section.list', {}, 'navigationhub'), "admin-sections",
  568. "adminSection", {uri: "pjax/section/list"}
  569. ),
  570. this._getNavigationItem(
  571. Y.eZ.trans('navigationhub.content_type.dashboard_title', {}, 'navigationhub'), "admin-contenttypes",
  572. "adminContentType", {uri: "pjax/contenttype"}
  573. ),
  574. this._getNavigationItem(
  575. Y.eZ.trans('navigationhub.language.list', {}, 'navigationhub'), "admin-languages",
  576. "adminLanguage", {uri: "pjax/language/list"}
  577. ),
  578. //TODO in EZP-24860. For now link to users node is defined in a static way.
  579. this._getSubtreeItem(
  580. Y.eZ.trans('navigationhub.user.list', {}, 'navigationhub'),
  581. "admin-users",
  582. "/api/ezp/v2/content/locations/1/5",
  583. "eng-GB"
  584. ),
  585. this._getNavigationItem(
  586. Y.eZ.trans('navigationhub.role.list', {}, 'navigationhub'), "admin-roles",
  587. "adminRole", {uri: "pjax/role"}
  588. ),
  589. ];
  590.  
  591. this._set('adminNavigationItems', val);
  592. return val;
  593. },
  594. readOnly: true,
  595. },
  596.  
  597. /**
  598. * Stores the user's avatar image
  599. *
  600. * @attribute userAvatar
  601. * @type {String}
  602. * @default null
  603. */
  604. userAvatar: {
  605. value: null
  606. }
  607. },
  608. });
  609. });
  610.