wizardTabAbstractFilter.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 This class represents the view belonging to the server tab
  10. * in the LDAP wizard.
  11. */
  12. var WizardTabAbstractFilter = OCA.LDAP.Wizard.WizardTabGeneric.subClass({
  13. /**
  14. * @property {number} number that needs to exceeded to use complex group
  15. * selection element
  16. */
  17. _groupElementSwitchThreshold: 40,
  18. /**
  19. * @property {boolean} - tells whether multiselect or complex element is
  20. * used for selecting groups
  21. */
  22. isComplexGroupChooser: false,
  23. /** @property {string} */
  24. tabID: '',
  25. /**
  26. * initializes the instance. Always call it after initialization.
  27. * concrete view must set managed items first, and then call the parent
  28. * init.
  29. *
  30. * @param {OCA.LDAP.Wizard.FilterOnTypeFactory} fotf
  31. * @param {number} [tabIndex]
  32. * @param {string} [tabID]
  33. */
  34. init: function (fotf, tabIndex, tabID) {
  35. this._super(tabIndex, tabID);
  36. /** @type {OCA.LDAP.Wizard.FilterOnTypeFactory} */
  37. this.foTFactory = fotf;
  38. this._initMultiSelect(
  39. this.getGroupsItem().$element,
  40. t('user_ldap', 'Select groups')
  41. );
  42. this._initMultiSelect(
  43. this.getObjectClassItem().$element,
  44. t('user_ldap', 'Select object classes')
  45. );
  46. this.filterName = this.getFilterItem().keyName;
  47. this._initFilterModeSwitcher(
  48. this.getToggleItem().$element,
  49. this.getRawFilterContainerItem().$element,
  50. [ this.getObjectClassItem().$element ],
  51. this.getFilterModeKey(),
  52. {
  53. status: 'disabled',
  54. $element: this.getGroupsItem().$element
  55. }
  56. );
  57. _.bindAll(this, 'onCountButtonClick', 'onSelectGroup', 'onDeselectGroup');
  58. this.getCountItem().$relatedElements.click(this.onCountButtonClick);
  59. if(this.manyGroupsSupport) {
  60. var $selectBtn = $(this.tabID).find('.ldapGroupListSelect');
  61. $selectBtn.click(this.onSelectGroup);
  62. var $deselectBtn = $(this.tabID).find('.ldapGroupListDeselect');
  63. $deselectBtn.click(this.onDeselectGroup);
  64. }
  65. },
  66. /**
  67. * returns managed item for the object class chooser. must be
  68. * implemented by concrete view
  69. */
  70. getObjectClassItem: function () {},
  71. /**
  72. * returns managed item for the group chooser. must be
  73. * implemented by concrete view
  74. */
  75. getGroupsItem: function () {},
  76. /**
  77. * returns managed item for the effective filter. must be
  78. * implemented by concrete view
  79. */
  80. getFilterItem: function () {},
  81. /**
  82. * returns managed item for the toggle element. must be
  83. * implemented by concrete view
  84. */
  85. getToggleItem: function () {},
  86. /**
  87. * returns managed item for the raw filter container. must be
  88. * implemented by concrete view
  89. */
  90. getRawFilterContainerItem: function () {},
  91. /**
  92. * returns managed item for the count control. must be
  93. * implemented by concrete view
  94. */
  95. getCountItem: function () {},
  96. /**
  97. * returns name of the filter mode key. must be implemented by concrete
  98. * view
  99. */
  100. getFilterModeKey: function () {},
  101. /**
  102. * Sets the config model for this view and subscribes to some events.
  103. * Also binds the config chooser to the model
  104. *
  105. * @param {OCA.LDAP.Wizard.ConfigModel} configModel
  106. */
  107. setModel: function(configModel) {
  108. this._super(configModel);
  109. this.configModel.on('configLoaded', this.onConfigSwitch, this);
  110. this.configModel.on('receivedLdapFeature', this.onFeatureReceived, this);
  111. },
  112. /**
  113. * @inheritdoc
  114. */
  115. _setFilterModeAssisted: function () {
  116. this._super();
  117. if(this.isComplexGroupChooser) {
  118. this.enableElement(this.getGroupsItem().$relatedElements);
  119. }
  120. },
  121. /**
  122. * @inheritdoc
  123. */
  124. _setFilterModeRaw: function () {
  125. this._super();
  126. if(this.manyGroupsSupport) {
  127. this.disableElement(this.getGroupsItem().$relatedElements);
  128. }
  129. },
  130. /**
  131. * sets the selected user object classes
  132. *
  133. * @param {Array} classes
  134. */
  135. setObjectClass: function(classes) {
  136. this.setElementValue(this.getObjectClassItem().$element, classes);
  137. this.getObjectClassItem().$element.multiselect('refresh');
  138. },
  139. /**
  140. * sets the selected groups
  141. *
  142. * @param {string} groups
  143. */
  144. setGroups: function(groups) {
  145. if(typeof groups === 'string') {
  146. groups = groups.split("\n");
  147. }
  148. if(!this.isComplexGroupChooser) {
  149. this.setElementValue(this.getGroupsItem().$element, groups);
  150. this.getGroupsItem().$element.multiselect('refresh');
  151. } else {
  152. var $element = $(this.tabID).find('.ldapGroupListSelected');
  153. this.equipMultiSelect($element, groups);
  154. this.updateFilterOnType();
  155. }
  156. },
  157. /**
  158. * sets the filter
  159. *
  160. * @param {string} filter
  161. */
  162. setFilter: function(filter) {
  163. this.setElementValue(this.getFilterItem().$element, filter);
  164. this.$filterModeRawContainer.siblings('.ldapReadOnlyFilterContainer').find('.ldapFilterReadOnlyElement').text(filter);
  165. },
  166. /**
  167. * sets the user count string
  168. *
  169. * @param {string} countInfo
  170. */
  171. setCount: function(countInfo) {
  172. this.setElementValue(this.getCountItem().$element, countInfo);
  173. },
  174. /**
  175. * @inheritdoc
  176. */
  177. considerFeatureRequests: function() {
  178. if(!this.isActive) {
  179. return;
  180. }
  181. if(this.getObjectClassItem().$element.find('option').length === 0) {
  182. this.disableElement(this.getObjectClassItem().$element);
  183. this.disableElement(this.getGroupsItem().$element);
  184. if(this.parsedFilterMode === this.configModel.FILTER_MODE_ASSISTED) {
  185. this.configModel.requestWizard(this.getObjectClassItem().keyName);
  186. this.configModel.requestWizard(this.getGroupsItem().keyName);
  187. }
  188. }
  189. },
  190. /**
  191. * updates (creates, if necessary) filterOnType instances
  192. */
  193. updateFilterOnType: function() {
  194. if(_.isUndefined(this.filterOnType)) {
  195. this.filterOnType = [];
  196. var $availableGroups = $(this.tabID).find('.ldapGroupListAvailable');
  197. this.filterOnType.push(this.foTFactory.get(
  198. $availableGroups, $(this.tabID).find('.ldapManyGroupsSearch')
  199. ));
  200. var $selectedGroups = $(this.tabID).find('.ldapGroupListSelected');
  201. this.filterOnType.push(this.foTFactory.get(
  202. $selectedGroups, $(this.tabID).find('.ldapManyGroupsSearch')
  203. ));
  204. }
  205. },
  206. /**
  207. * @inheritdoc
  208. */
  209. onActivate: function() {
  210. this._super();
  211. this.considerFeatureRequests();
  212. },
  213. /**
  214. * resets the view when a configuration switch happened.
  215. *
  216. * @param {WizardTabAbstractFilter} view
  217. * @param {Object} configuration
  218. */
  219. onConfigSwitch: function(view, configuration) {
  220. view.getObjectClassItem().$element.find('option').remove();
  221. view.getGroupsItem().$element.find('option').remove();
  222. view.getCountItem().$element.text('');
  223. $(view.tabID).find('.ldapGroupListAvailable').empty();
  224. $(view.tabID).find('.ldapGroupListSelected').empty();
  225. view.updateFilterOnType();
  226. $(view.tabID).find('.ldapManyGroupsSearch').val('');
  227. if(view.isComplexGroupChooser) {
  228. view.isComplexGroupChooser = false;
  229. view.getGroupsItem().$element.multiselect({classes: view.multiSelectPluginClass});
  230. $(view.tabID).find(".ldapManyGroupsSupport").addClass('hidden');
  231. }
  232. view.onConfigLoaded(view, configuration);
  233. },
  234. /**
  235. * @inheritdoc
  236. */
  237. onConfigLoaded: function(view, configuration) {
  238. for(var key in view.managedItems){
  239. if(!_.isUndefined(configuration[key])) {
  240. var value = configuration[key];
  241. var methodName = view.managedItems[key].setMethod;
  242. if(!_.isUndefined(view[methodName])) {
  243. view[methodName](value);
  244. // we reimplement it here to update the filter index
  245. // for groups. Maybe we can isolate it?
  246. if(methodName === 'setGroups') {
  247. view.updateFilterOnType();
  248. }
  249. }
  250. }
  251. }
  252. },
  253. /**
  254. * if UserObjectClasses are found, the corresponding element will be
  255. * updated
  256. *
  257. * @param {WizardTabAbstractFilter} view
  258. * @param {FeaturePayload} payload
  259. */
  260. onFeatureReceived: function(view, payload) {
  261. if(payload.feature === view.getObjectClassItem().featureName) {
  262. view.equipMultiSelect(view.getObjectClassItem().$element, payload.data);
  263. if( !view.getFilterItem().$element.val()
  264. && view.parsedFilterMode === view.configModel.FILTER_MODE_ASSISTED
  265. ) {
  266. view.configModel.requestWizard(view.getFilterItem().keyName);
  267. }
  268. } else if (payload.feature === view.getGroupsItem().featureName) {
  269. if(view.manyGroupsSupport && payload.data.length > view._groupElementSwitchThreshold) {
  270. // we need to fill the left list box, excluding the values
  271. // that are already selected
  272. var $element = $(view.tabID).find('.ldapGroupListAvailable');
  273. var selected = view.configModel.configuration[view.getGroupsItem().keyName];
  274. var available = $(payload.data).not(selected).get();
  275. view.equipMultiSelect($element, available);
  276. $(view.tabID).find(".ldapManyGroupsSupport").removeClass('hidden');
  277. view.getGroupsItem().$element.multiselect({classes: view.multiSelectPluginClass + ' forceHidden'});
  278. view.isComplexGroupChooser = true;
  279. } else {
  280. view.isComplexGroupChooser = false;
  281. view.equipMultiSelect(view.getGroupsItem().$element, payload.data);
  282. view.getGroupsItem().$element.multiselect({classes: view.multiSelectPluginClass});
  283. $(view.tabID).find(".ldapManyGroupsSupport").addClass('hidden');
  284. }
  285. }
  286. },
  287. /**
  288. * request to count the users with the current filter
  289. *
  290. * @param {Event} event
  291. */
  292. onCountButtonClick: function(event) {
  293. event.preventDefault();
  294. // let's clear the field
  295. this.getCountItem().$element.text('');
  296. this.configModel.requestWizard(this.getCountItem().keyName);
  297. },
  298. /**
  299. * saves groups when using the complex UI
  300. *
  301. * @param {Array} groups
  302. * @returns {boolean}
  303. * @private
  304. */
  305. _saveGroups: function(groups) {
  306. var toSave = '';
  307. $(groups).each(function() { toSave = toSave + "\n" + this; } );
  308. this.configModel.set(this.getGroupsItem().keyName, $.trim(toSave));
  309. },
  310. /**
  311. * acts on adding groups to the filter
  312. */
  313. onSelectGroup: function() {
  314. var $available = $(this.tabID).find('.ldapGroupListAvailable');
  315. if(!$available.val()) {
  316. return; // no selection – nothing to do
  317. }
  318. var $selected = $(this.tabID).find('.ldapGroupListSelected');
  319. var selected = $.map($selected.find('option'), function(e) { return e.value; });
  320. let selectedGroups = [];
  321. $available.find('option:selected:visible').each(function() {
  322. selectedGroups.push($(this).val());
  323. });
  324. this._saveGroups(selected.concat(selectedGroups));
  325. $available.find('option:selected:visible').prependTo($selected);
  326. this.updateFilterOnType(); // selected groups are not updated yet
  327. $available.find('option:selected').prop("selected", false);
  328. },
  329. /**
  330. * acts on removing groups to the filter
  331. */
  332. onDeselectGroup: function() {
  333. var $available = $(this.tabID).find('.ldapGroupListAvailable');
  334. var $selected = $(this.tabID).find('.ldapGroupListSelected');
  335. var selected = $.map($selected.find('option:not(:selected:visible)'), function(e) { return e.value; });
  336. this._saveGroups(selected);
  337. $selected.find('option:selected:visible').appendTo($available);
  338. this.updateFilterOnType(); // selected groups are not updated yet
  339. $selected.find('option:selected').prop("selected", false);
  340. }
  341. });
  342. OCA.LDAP.Wizard.WizardTabAbstractFilter = WizardTabAbstractFilter;
  343. })();