API Docs for: 1.0.0
Show:

File: Resources/public/js/apps/ez-platformuiapp.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-platformuiapp', function (Y) {
  6. "use strict";
  7. /**
  8. * Provides the PlatformUI Application class
  9. *
  10. * @module ez-platformuiapp
  11. */
  12.  
  13. Y.namespace('eZ');
  14.  
  15. /**
  16. * PlatformUI Application
  17. *
  18. * @namespace eZ
  19. * @class PlatformUIApp
  20. * @constructor
  21. * @extends App
  22. */
  23. Y.eZ.PlatformUIApp = Y.Base.create('platformuiApp', Y.App, [Y.eZ.TranslateProperty], {
  24. /**
  25. * Initializes the application.
  26. *
  27. * @method initializer
  28. */
  29. initializer: function () {
  30. this._dispatchConfig();
  31. Y.eZ.Translator.setPreferredLanguages(this.get('interfaceLanguages'));
  32.  
  33. this._setGlobals();
  34. this._fireAppReadyEvent();
  35. },
  36.  
  37. /**
  38. * Instantiates and renders a view with the given `ViewConstructor` and
  39. * `ServiceConstructor`. It does pretty much the same thing as what
  40. * happened when navigating in the app but it skips the routing phase.
  41. * When done, the `done` callback is called with the view and view
  42. * service instances as parameters.
  43. *
  44. * @method renderView
  45. * @param {Function} ViewConstructor
  46. * @param {Function} ServiceConstructor
  47. * @param {Object} requestParams the request parameters expected by the
  48. * view service.
  49. * @param {Function} done
  50. * @param {false|Error} done.error
  51. * @param {eZ.ViewService} done.viewService
  52. * @param {eZ.View} done.view
  53. */
  54. renderView: function (ViewConstructor, ServiceConstructor, requestParams, done) {
  55. var req, res,
  56. view, viewService;
  57.  
  58. req = this._getRequest('renderComponent');
  59. req.params = requestParams;
  60. res = this._getResponse(req);
  61.  
  62. viewService = new ServiceConstructor({
  63. app: this,
  64. capi: this.get('capi'),
  65. request: req,
  66. response: res,
  67. plugins: Y.eZ.PluginRegistry.getPlugins(ServiceConstructor.NAME),
  68. config: this.get('config'),
  69. bubbleTargets: this,
  70. });
  71.  
  72. viewService.once('error', function (e) {
  73. done(new Error(e.message));
  74. });
  75. viewService.load(function () {
  76. view = new ViewConstructor(viewService.getViewParameters());
  77. view.render();
  78. view.addTarget(viewService);
  79. done(false, viewService, view);
  80. });
  81. },
  82.  
  83. /**
  84. * Instantiates and renders a side view with the given `ViewConstructor` and
  85. * `ServiceConstructor`. It does pretty much the same thing as what
  86. * happened when the app is creating a side view.
  87. *
  88. * @method renderSideView
  89. * @param {Function} ViewConstructor
  90. * @param {Function} ServiceConstructor
  91. * @param {Object} params the parameters expected by the side view
  92. * service
  93. * @param {Function} done
  94. * @param {false|Error} done.error
  95. * @param {eZ.ViewService} done.viewService
  96. * @param {eZ.View} done.view
  97. */
  98. renderSideView: function (ViewConstructor, ServiceConstructor, params, done) {
  99. var req, res,
  100. view, viewService;
  101.  
  102. req = this._getRequest('renderComponent');
  103. res = this._getResponse(req);
  104.  
  105. viewService = new ServiceConstructor({
  106. app: this,
  107. capi: this.get('capi'),
  108. plugins: Y.eZ.PluginRegistry.getPlugins(ServiceConstructor.NAME),
  109. config: this.get('config'),
  110. bubbleTargets: this,
  111. });
  112.  
  113. viewService.once('error', function (e) {
  114. done(new Error(e.message));
  115. });
  116.  
  117. viewService.setAttrs({
  118. 'parameters': params,
  119. 'request': req,
  120. 'response': res,
  121. });
  122. view = new ViewConstructor({bubbleTargets: viewService});
  123. viewService.load(function () {
  124. view.setAttrs(viewService.getViewParameters());
  125. view.render();
  126. view.addTarget(viewService);
  127. done(false, viewService, view);
  128. });
  129. },
  130.  
  131. /**
  132. * Registers the `Y` sandbox object and the app instance in the global
  133. * `eZ.YUI` namespace.
  134. *
  135. * @method _setGlobals
  136. * @private
  137. */
  138. _setGlobals: function () {
  139. window.eZ = window.eZ || {};
  140. window.eZ.YUI = {
  141. Y: Y,
  142. app: this,
  143. };
  144. },
  145.  
  146. /**
  147. * Fires the custom `ez:yui-app:ready` **DOM** event. It is dispatched
  148. * from the `document` object.
  149. *
  150. * @method _fireAppReadyEvent
  151. * @private
  152. */
  153. _fireAppReadyEvent: function () {
  154. var evt = new CustomEvent('ez:yui-app:ready');
  155.  
  156. document.dispatchEvent(evt);
  157. },
  158.  
  159. /**
  160. * Dispatches the `config` attribute value so that the app is configured
  161. * accordingly. The values consumed by the app are removed from the
  162. * configuration.
  163. *
  164. * @method _dispatchConfig
  165. * @protected
  166. */
  167. _dispatchConfig: function () {
  168. var config = this.get('config'),
  169. systemLanguageList = {},
  170. defaultLanguageCode;
  171.  
  172. if ( !config ) {
  173. return;
  174. }
  175. Y.Object.each(config.rootInfo, function (value, attrName) {
  176. if ( this.attrAdded(attrName) ) {
  177. this._set(attrName, value);
  178. delete config.rootInfo[attrName];
  179. }
  180. }, this);
  181. if ( config.anonymousUserId ) {
  182. this._set('anonymousUserId', config.anonymousUserId);
  183. delete config.anonymousUserId;
  184. }
  185. config.localesMap = config.localesMap || {};
  186. this._set('localesMap', config.localesMap);
  187.  
  188. this._set('capi', new Y.eZ.CAPI(
  189. this.get('apiRoot').replace(/\/{1,}$/, ''),
  190. new Y.eZ.SessionAuthAgent(
  191. (config.sessionInfo && config.sessionInfo.isStarted) ? config.sessionInfo : undefined
  192. )
  193. ));
  194. delete config.sessionInfo;
  195.  
  196. if ( config.languages ) {
  197. Y.Array.each(config.languages, function (language) {
  198. systemLanguageList[language.languageCode] = language;
  199. if ( language.default ) {
  200. defaultLanguageCode = language.languageCode;
  201. }
  202. });
  203. this._set('systemLanguageList', systemLanguageList);
  204. this._set('contentCreationDefaultLanguageCode', defaultLanguageCode);
  205. delete config.languages;
  206. }
  207. if ( config.interfaceLanguages ) {
  208. this._set('interfaceLanguages', config.interfaceLanguages);
  209. delete config.interfaceLanguages;
  210. }
  211. },
  212.  
  213. /**
  214. * Returns the language name of the language with the given
  215. * `languageCode`. If no language is found with this code, the language
  216. * code is returned.
  217. *
  218. * @method getLanguageName
  219. * @since 1.1
  220. * @param {String} languageCode
  221. * @return {String}
  222. */
  223. getLanguageName: function (languageCode) {
  224. var systemLanguageList = this.get('systemLanguageList');
  225.  
  226. if ( systemLanguageList[languageCode] ) {
  227. return systemLanguageList[languageCode].name;
  228. }
  229. return languageCode;
  230. },
  231. }, {
  232. ATTRS: {
  233. /**
  234. * The application configuration. It is dispatched to the others
  235. * application attributes/properties at build time.
  236. *
  237. * @attribute config
  238. * @type {Object|undefined}
  239. * @writeOnce
  240. */
  241. config: {
  242. writeOnce: 'initOnly',
  243. },
  244.  
  245. /**
  246. * The base URI to build the URI of the ajax request
  247. *
  248. * @attribute apiRoot
  249. * @default "/"
  250. * @type String
  251. * @readOnly
  252. */
  253. apiRoot: {
  254. readOnly: true,
  255. value: "/"
  256. },
  257.  
  258. /**
  259. * The root directory where to find the assets.
  260. *
  261. * @attribute assetRoot
  262. * @default "/"
  263. * @type String
  264. * @readOnly
  265. */
  266. assetRoot: {
  267. readOnly: true,
  268. value: "/"
  269. },
  270.  
  271. /**
  272. * eZ Platform REST client
  273. *
  274. * @attribute capi
  275. * @default null
  276. * @type {eZ.CAPI}
  277. * @readOnly
  278. * @required
  279. */
  280. capi: {
  281. readOnly: true,
  282. value: null
  283. },
  284.  
  285. /**
  286. * Stores the REST id of the configured anonymous user
  287. *
  288. * @attribute anonymousUserId
  289. * @type {String}
  290. * @readOnly
  291. */
  292. anonymousUserId: {
  293. readOnly: true,
  294. value: "/api/ezp/v2/user/users/10",
  295. },
  296.  
  297. /**
  298. * System language list provided with config. The list is hash
  299. * containing language objects and is indexed by languageCode.
  300. *
  301. * @attribute systemLanguageList
  302. * @default {}
  303. * @type {Object}
  304. * @readOnly
  305. */
  306. systemLanguageList: {
  307. readOnly: true,
  308. value: {}
  309. },
  310.  
  311. /**
  312. * Default language code, as defined by the current siteaccess language config.
  313. *
  314. * @attribute contentCreationDefaultLanguageCode
  315. * @default "eng-GB"
  316. * @type {String}
  317. * @readOnly
  318. */
  319. contentCreationDefaultLanguageCode: {
  320. readOnly: true,
  321. value: "eng-GB"
  322. },
  323.  
  324. /**
  325. * Holds the Locales conversion map between eZ Locale codes and
  326. * POSIX ones. See locale.yml
  327. *
  328. * @attribute localesMap
  329. * @type {Object}
  330. * @readOnly
  331. * @default {}
  332. */
  333. localesMap: {
  334. readOnly: true,
  335. value: {},
  336. },
  337.  
  338. /**
  339. * List of preferred languages in which the interface should be
  340. * translated.
  341. *
  342. * @attribute interfaceLanguages
  343. * @readOnly
  344. * @default ['en']
  345. * @type {Array}
  346. */
  347. interfaceLanguages: {
  348. readOnly: true,
  349. value: ['en'],
  350. },
  351. }
  352. });
  353. });
  354.