1
0

view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /**
  2. * Copyright (c) 2015, Arthur Schiwon <blizzz@owncloud.com>
  3. * This file is licensed under the Affero General Public License version 3 or later.
  4. * See the COPYING-README file.
  5. */
  6. OCA = OCA || {};
  7. (function() {
  8. /**
  9. * @classdesc main view class. It takes care of tab-unrelated control
  10. * elements (status bar, control buttons) and does or requests configuration
  11. * checks. It also manages the separate tab views.
  12. *
  13. * @constructor
  14. */
  15. var WizardView = function() {};
  16. WizardView.prototype = {
  17. /** @constant {number} */
  18. STATUS_ERROR: 0,
  19. /** @constant {number} */
  20. STATUS_INCOMPLETE: 1,
  21. /** @constant {number} */
  22. STATUS_SUCCESS: 2,
  23. /** @constant {number} */
  24. STATUS_UNTESTED: 3,
  25. /**
  26. * initializes the instance. Always call it after creating the instance.
  27. */
  28. init: function () {
  29. this.tabs = {};
  30. this.tabs.server = new OCA.LDAP.Wizard.WizardTabElementary();
  31. this.$settings = $('#ldapSettings');
  32. this.$saveSpinners = $('.ldap_saving');
  33. this.saveProcesses = 0;
  34. _.bindAll(this, 'onTabChange', 'onTestButtonClick');
  35. },
  36. /**
  37. * applies click events to the forward and backward buttons
  38. */
  39. initControls: function() {
  40. var view = this;
  41. $('.ldap_action_continue').click(function(event) {
  42. event.preventDefault();
  43. view._controlContinue(view);
  44. });
  45. $('.ldap_action_back').click(function(event) {
  46. event.preventDefault();
  47. view._controlBack(view);
  48. });
  49. $('.ldap_action_test_connection').click(this.onTestButtonClick);
  50. },
  51. /**
  52. * registers a tab
  53. *
  54. * @param {OCA.LDAP.Wizard.WizardTabGeneric} tabView
  55. * @param {string} index
  56. * @returns {boolean}
  57. */
  58. registerTab: function(tabView, index) {
  59. if( _.isUndefined(this.tabs[index])
  60. && tabView instanceof OCA.LDAP.Wizard.WizardTabGeneric
  61. ) {
  62. this.tabs[index] = tabView;
  63. this.tabs[index].setModel(this.configModel);
  64. return true;
  65. }
  66. return false;
  67. },
  68. /**
  69. * checks certain config values for completeness and depending on them
  70. * enables or disables non-elementary tabs.
  71. */
  72. basicStatusCheck: function(view) {
  73. var host = view.configModel.configuration.ldap_host;
  74. var port = view.configModel.configuration.ldap_port;
  75. var base = view.configModel.configuration.ldap_base;
  76. var agent = view.configModel.configuration.ldap_dn;
  77. var pwd = view.configModel.configuration.ldap_agent_password;
  78. if((host && port && base) && ((!agent && !pwd) || (agent && pwd))) {
  79. view.enableTabs();
  80. } else {
  81. view.disableTabs();
  82. }
  83. },
  84. /**
  85. * if the configuration is sufficient the model is being request to
  86. * perform a configuration test. Otherwise, the status indicator is
  87. * being updated with the status "incomplete"
  88. */
  89. functionalityCheck: function() {
  90. // this method should be called only if necessary, because it may
  91. // cause an LDAP request!
  92. var host = this.configModel.configuration.ldap_host;
  93. var port = this.configModel.configuration.ldap_port;
  94. var base = this.configModel.configuration.ldap_base;
  95. var userFilter = this.configModel.configuration.ldap_userlist_filter;
  96. var loginFilter = this.configModel.configuration.ldap_login_filter;
  97. if(host && port && base && userFilter && loginFilter) {
  98. this.configModel.requestConfigurationTest();
  99. } else {
  100. this._updateStatusIndicator(this.STATUS_INCOMPLETE);
  101. }
  102. },
  103. /**
  104. * will request a functionality check if one of the related configuration
  105. * settings was changed.
  106. *
  107. * @param {ConfigSetPayload|Object} [changeSet]
  108. */
  109. considerFunctionalityCheck: function(changeSet) {
  110. var testTriggers = [
  111. 'ldap_host', 'ldap_port', 'ldap_dn', 'ldap_agent_password',
  112. 'ldap_base', 'ldap_userlist_filter', 'ldap_login_filter'
  113. ];
  114. for(var key in changeSet) {
  115. if($.inArray(key, testTriggers) >= 0) {
  116. this.functionalityCheck();
  117. return;
  118. }
  119. }
  120. },
  121. /**
  122. * keeps number of running save processes and shows a spinner if
  123. * necessary
  124. *
  125. * @param {WizardView} [view]
  126. * @listens ConfigModel#setRequested
  127. */
  128. onSetRequested: function(view) {
  129. view.saveProcesses += 1;
  130. if(view.saveProcesses === 1) {
  131. view.showSaveSpinner();
  132. }
  133. },
  134. /**
  135. * keeps number of running save processes and hides the spinner if
  136. * necessary. Also triggers checks, to adjust tabs state and status bar.
  137. *
  138. * @param {WizardView} [view]
  139. * @param {ConfigSetPayload} [result]
  140. * @listens ConfigModel#setCompleted
  141. */
  142. onSetRequestDone: function(view, result) {
  143. if(view.saveProcesses > 0) {
  144. view.saveProcesses -= 1;
  145. if(view.saveProcesses === 0) {
  146. view.hideSaveSpinner();
  147. }
  148. }
  149. view.basicStatusCheck(view);
  150. var param = {};
  151. param[result.key] = 1;
  152. view.considerFunctionalityCheck(param);
  153. },
  154. /**
  155. * Base DN test results will arrive here
  156. *
  157. * @param {WizardTabElementary} view
  158. * @param {FeaturePayload} payload
  159. */
  160. onDetectionTestCompleted: function(view, payload) {
  161. if(payload.feature === 'TestBaseDN') {
  162. if(payload.data.status === 'success') {
  163. var objectsFound = parseInt(payload.data.changes.ldap_test_base, 10);
  164. if(objectsFound > 0) {
  165. view._updateStatusIndicator(view.STATUS_SUCCESS);
  166. return;
  167. }
  168. }
  169. view._updateStatusIndicator(view.STATUS_ERROR);
  170. OC.Notification.showTemporary(t('user_ldap', 'The Base DN appears to be wrong'));
  171. }
  172. },
  173. /**
  174. * updates the status indicator based on the configuration test result
  175. *
  176. * @param {WizardView} [view]
  177. * @param {ConfigTestPayload} [result]
  178. * @listens ConfigModel#configurationTested
  179. */
  180. onTestCompleted: function(view, result) {
  181. if(result.isSuccess) {
  182. view.configModel.requestWizard('ldap_test_base');
  183. } else {
  184. view._updateStatusIndicator(view.STATUS_ERROR);
  185. }
  186. },
  187. /**
  188. * triggers initial checks upon configuration loading to update status
  189. * controls
  190. *
  191. * @param {WizardView} [view]
  192. * @listens ConfigModel#configLoaded
  193. */
  194. onConfigLoaded: function(view) {
  195. view._updateStatusIndicator(view.STATUS_UNTESTED);
  196. view.basicStatusCheck(view);
  197. view.functionalityCheck();
  198. },
  199. /**
  200. * reacts on attempts to switch to a different tab
  201. *
  202. * @param {object} event
  203. * @param {object} ui
  204. * @returns {boolean}
  205. */
  206. onTabChange: function(event, ui) {
  207. if(this.saveProcesses > 0) {
  208. return false;
  209. }
  210. var newTabID = ui.newTab[0].id;
  211. if(newTabID === '#ldapWizard1') {
  212. newTabID = 'server';
  213. }
  214. var oldTabID = ui.oldTab[0].id;
  215. if(oldTabID === '#ldapWizard1') {
  216. oldTabID = 'server';
  217. }
  218. if(!_.isUndefined(this.tabs[newTabID])) {
  219. this.tabs[newTabID].isActive = true;
  220. this.tabs[newTabID].onActivate();
  221. } else {
  222. console.warn('Unreferenced activated tab ' + newTabID);
  223. }
  224. if(!_.isUndefined(this.tabs[oldTabID])) {
  225. this.tabs[oldTabID].isActive = false;
  226. } else {
  227. console.warn('Unreferenced left tab ' + oldTabID);
  228. }
  229. if(!_.isUndefined(this.tabs[newTabID])) {
  230. this._controlUpdate(this.tabs[newTabID].tabIndex);
  231. }
  232. },
  233. /**
  234. * triggers checks upon configuration updates to keep status controls
  235. * up to date
  236. *
  237. * @param {WizardView} [view]
  238. * @param {object} [changeSet]
  239. * @listens ConfigModel#configUpdated
  240. */
  241. onConfigUpdated: function(view, changeSet) {
  242. view.basicStatusCheck(view);
  243. view.considerFunctionalityCheck(changeSet);
  244. },
  245. /**
  246. * requests a configuration test
  247. */
  248. onTestButtonClick: function() {
  249. this.configModel.requestWizard('ldap_action_test_connection', {ldap_serverconfig_chooser: this.configModel.configID});
  250. },
  251. /**
  252. * sets the model instance and registers event listeners
  253. *
  254. * @param {OCA.LDAP.Wizard.ConfigModel} [configModel]
  255. */
  256. setModel: function(configModel) {
  257. /** @type {OCA.LDAP.Wizard.ConfigModel} */
  258. this.configModel = configModel;
  259. for(var i in this.tabs) {
  260. this.tabs[i].setModel(configModel);
  261. }
  262. // make sure this is definitely run after tabs did their work, order is important here
  263. // for now this works, because tabs are supposed to register their listeners in their
  264. // setModel() method.
  265. // alternative: make Elementary Tab a Publisher as well.
  266. this.configModel.on('configLoaded', this.onConfigLoaded, this);
  267. this.configModel.on('configUpdated', this.onConfigUpdated, this);
  268. this.configModel.on('setRequested', this.onSetRequested, this);
  269. this.configModel.on('setCompleted', this.onSetRequestDone, this);
  270. this.configModel.on('configurationTested', this.onTestCompleted, this);
  271. this.configModel.on('receivedLdapFeature', this.onDetectionTestCompleted, this);
  272. },
  273. /**
  274. * enables tab and navigation buttons
  275. */
  276. enableTabs: function() {
  277. //do not use this function directly, use basicStatusCheck instead.
  278. if(this.saveProcesses === 0) {
  279. $('.ldap_action_continue').removeAttr('disabled');
  280. $('.ldap_action_back').removeAttr('disabled');
  281. this.$settings.tabs('option', 'disabled', []);
  282. }
  283. },
  284. /**
  285. * disables tab and navigation buttons
  286. */
  287. disableTabs: function() {
  288. $('.ldap_action_continue').attr('disabled', 'disabled');
  289. $('.ldap_action_back').attr('disabled', 'disabled');
  290. this.$settings.tabs('option', 'disabled', [1, 2, 3, 4, 5]);
  291. },
  292. /**
  293. * shows a save spinner
  294. */
  295. showSaveSpinner: function() {
  296. this.$saveSpinners.removeClass('hidden');
  297. $('#ldap *').addClass('save-cursor');
  298. },
  299. /**
  300. * hides the save spinner
  301. */
  302. hideSaveSpinner: function() {
  303. this.$saveSpinners.addClass('hidden');
  304. $('#ldap *').removeClass('save-cursor');
  305. },
  306. /**
  307. * performs a config load request to the model
  308. *
  309. * @param {string} [configID]
  310. * @private
  311. */
  312. _requestConfig: function(configID) {
  313. this.configModel.load(configID);
  314. },
  315. /**
  316. * bootstraps the visual appearance and event listeners, as well as the
  317. * first config
  318. */
  319. render: function () {
  320. $('#ldapAdvancedAccordion').accordion({ heightStyle: 'content', animate: 'easeInOutCirc'});
  321. this.$settings.tabs({});
  322. $('#ldapSettings button:not(.icon-default-style):not(.ui-multiselect)').button();
  323. $('#ldapSettings').tabs({ beforeActivate: this.onTabChange });
  324. this.initControls();
  325. this.disableTabs();
  326. this._requestConfig(this.tabs.server.getConfigID());
  327. },
  328. /**
  329. * updates the status indicator / bar
  330. *
  331. * @param {number} [state]
  332. * @private
  333. */
  334. _updateStatusIndicator: function(state) {
  335. var $indicator = $('.ldap_config_state_indicator');
  336. var $indicatorLight = $('.ldap_config_state_indicator_sign');
  337. switch(state) {
  338. case this.STATUS_UNTESTED:
  339. $indicator.text(t('user_ldap',
  340. 'Testing configuration…'
  341. ));
  342. $indicator.addClass('ldap_grey');
  343. $indicatorLight.removeClass('error');
  344. $indicatorLight.removeClass('success');
  345. break;
  346. case this.STATUS_ERROR:
  347. $indicator.text(t('user_ldap',
  348. 'Configuration incorrect'
  349. ));
  350. $indicator.removeClass('ldap_grey');
  351. $indicatorLight.addClass('error');
  352. $indicatorLight.removeClass('success');
  353. break;
  354. case this.STATUS_INCOMPLETE:
  355. $indicator.text(t('user_ldap',
  356. 'Configuration incomplete'
  357. ));
  358. $indicator.removeClass('ldap_grey');
  359. $indicatorLight.removeClass('error');
  360. $indicatorLight.removeClass('success');
  361. break;
  362. case this.STATUS_SUCCESS:
  363. $indicator.text(t('user_ldap', 'Configuration OK'));
  364. $indicator.addClass('ldap_grey');
  365. $indicatorLight.removeClass('error');
  366. $indicatorLight.addClass('success');
  367. if(!this.tabs.server.isActive) {
  368. this.configModel.set('ldap_configuration_active', 1);
  369. }
  370. break;
  371. }
  372. },
  373. /**
  374. * handles a click on the Back button
  375. *
  376. * @param {WizardView} [view]
  377. * @private
  378. */
  379. _controlBack: function(view) {
  380. var curTabIndex = view.$settings.tabs('option', 'active');
  381. if(curTabIndex == 0) {
  382. return;
  383. }
  384. view.$settings.tabs('option', 'active', curTabIndex - 1);
  385. view._controlUpdate(curTabIndex - 1);
  386. },
  387. /**
  388. * handles a click on the Continue button
  389. *
  390. * @param {WizardView} [view]
  391. * @private
  392. */
  393. _controlContinue: function(view) {
  394. var curTabIndex = view.$settings.tabs('option', 'active');
  395. if(curTabIndex == 3) {
  396. return;
  397. }
  398. view.$settings.tabs('option', 'active', 1 + curTabIndex);
  399. view._controlUpdate(curTabIndex + 1);
  400. },
  401. /**
  402. * updates the controls (navigation buttons)
  403. *
  404. * @param {number} [nextTabIndex] - index of the tab being switched to
  405. * @private
  406. */
  407. _controlUpdate: function(nextTabIndex) {
  408. if(nextTabIndex == 0) {
  409. $('.ldap_action_back').addClass('invisible');
  410. $('.ldap_action_continue').removeClass('invisible');
  411. } else
  412. if(nextTabIndex == 1) {
  413. $('.ldap_action_back').removeClass('invisible');
  414. $('.ldap_action_continue').removeClass('invisible');
  415. } else
  416. if(nextTabIndex == 2) {
  417. $('.ldap_action_continue').removeClass('invisible');
  418. $('.ldap_action_back').removeClass('invisible');
  419. } else
  420. if(nextTabIndex == 3) {
  421. $('.ldap_action_back').removeClass('invisible');
  422. $('.ldap_action_continue').addClass('invisible');
  423. }
  424. }
  425. };
  426. OCA.LDAP.Wizard.WizardView = WizardView;
  427. })();