wizardDetectorGeneric.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. OCA = OCA || {};
  7. (function() {
  8. /**
  9. * @classdesc a generic (abstract) Detector template. A Detector's task is
  10. * to kick off server side detection of certain LDAP features. It is invoked
  11. * when changes to specified configuration keys happen.
  12. *
  13. * @constructor
  14. */
  15. var WizardDetectorGeneric = OCA.LDAP.Wizard.WizardObject.subClass({
  16. /**
  17. * initializes the instance. Always call it after creating the instance.
  18. */
  19. init: function() {
  20. this.setTrigger([]);
  21. this.targetKey = '';
  22. this.runsOnRequest = false;
  23. },
  24. /**
  25. * sets the configuration keys the detector is listening on
  26. *
  27. * @param {string[]} triggers
  28. */
  29. setTrigger: function(triggers) {
  30. this.triggers = triggers;
  31. },
  32. /**
  33. * tests whether the detector is triggered by the provided key
  34. *
  35. * @param {string} key
  36. * @returns {boolean}
  37. */
  38. triggersOn: function(key) {
  39. return ($.inArray(key, this.triggers) >= 0);
  40. },
  41. /**
  42. * whether the detector runs on explicit request
  43. *
  44. * @param {string} key
  45. * @returns {boolean}
  46. */
  47. runsOnFeatureRequest: function(key) {
  48. return !!(this.runsOnRequest && this.targetKey === key);
  49. },
  50. /**
  51. * sets the configuration key the detector is attempting to auto-detect
  52. *
  53. * @param {string} key
  54. */
  55. setTargetKey: function(key) {
  56. this.targetKey = key;
  57. },
  58. /**
  59. * returns the configuration key the detector is attempting to
  60. * auto-detect
  61. */
  62. getTargetKey: function() {
  63. return this.targetKey;
  64. },
  65. /**
  66. * runs the detector. This method is supposed to be implemented by the
  67. * concrete detector.
  68. *
  69. * Must return false if the detector decides not to run.
  70. * Must return a jqXHR object otherwise, which is provided by the
  71. * model's callWizard()
  72. *
  73. * @param {OCA.LDAP.Wizard.ConfigModel} model
  74. * @param {string} configID - the configuration prefix
  75. * @returns {boolean|jqXHR}
  76. * @abstract
  77. */
  78. run: function(model, configID) {
  79. // to be implemented by subClass
  80. return false;
  81. },
  82. /**
  83. * processes the result of the Nextcloud server
  84. *
  85. * @param {OCA.LDAP.Wizard.ConfigModel} model
  86. * @param {WizardDetectorGeneric} detector
  87. * @param {object} result
  88. */
  89. processResult: function(model, detector, result) {
  90. model['notifyAboutDetectionCompletion'](detector.getTargetKey());
  91. if(result.status === 'success') {
  92. for (var id in result.changes) {
  93. // update and not set method, as values are already stored
  94. model['update'](id, result.changes[id]);
  95. }
  96. } else {
  97. var payload = { relatedKey: detector.targetKey };
  98. if(!_.isUndefined(result.message)) {
  99. payload.message = result.message;
  100. }
  101. model.gotServerError(payload);
  102. }
  103. }
  104. });
  105. OCA.LDAP.Wizard.WizardDetectorGeneric = WizardDetectorGeneric;
  106. })();