You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

view.js 12KB

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