requestuseragentplugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  3. *
  4. * @license GNU AGPL version 3 or any later version
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. (function() {
  21. OCA.WorkflowEngine = OCA.WorkflowEngine || {};
  22. OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
  23. OCA.WorkflowEngine.Plugins.RequestUserAgentPlugin = {
  24. predefinedValues: ['android', 'ios', 'desktop'],
  25. getCheck: function() {
  26. return {
  27. 'class': 'OCA\\WorkflowEngine\\Check\\RequestUserAgent',
  28. 'name': t('workflowengine', 'Request user agent'),
  29. 'operators': [
  30. {'operator': 'is', 'name': t('workflowengine', 'is')},
  31. {'operator': '!is', 'name': t('workflowengine', 'is not')},
  32. {'operator': 'matches', 'name': t('workflowengine', 'matches')},
  33. {'operator': '!matches', 'name': t('workflowengine', 'does not match')}
  34. ]
  35. };
  36. },
  37. render: function(element, check) {
  38. if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\RequestUserAgent') {
  39. return;
  40. }
  41. var placeholder = 'Mozilla/5.0 User Agent';
  42. if (check['operator'] === 'matches' || check['operator'] === '!matches') {
  43. placeholder = '/^Mozilla\\/5\\.0 (.*)$/i';
  44. }
  45. $(element).css('width', '250px')
  46. .attr('placeholder', placeholder)
  47. .attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: placeholder}))
  48. .addClass('has-tooltip')
  49. .tooltip({
  50. placement: 'bottom'
  51. });
  52. if (check['operator'] === 'matches' || check['operator'] === '!matches') {
  53. if (this._validateRegex(check['value'])) {
  54. $(element).removeClass('invalid-input');
  55. } else {
  56. $(element).addClass('invalid-input');
  57. }
  58. } else {
  59. var self = this,
  60. data = [
  61. {
  62. text: t('workflowengine', 'Sync clients'),
  63. children: [
  64. {id: 'android', text: t('workflowengine', 'Android client')},
  65. {id: 'ios', text: t('workflowengine', 'iOS client')},
  66. {id: 'desktop', text: t('workflowengine', 'Desktop client')}
  67. ]
  68. }
  69. ];
  70. if (this.predefinedValues.indexOf(check['value']) === -1) {
  71. data.unshift({
  72. id: check['value'],
  73. text: check['value']
  74. })
  75. }
  76. $(element).select2({
  77. data: data,
  78. createSearchChoice: function(term) {
  79. if (self.predefinedValues.indexOf(check['value']) === -1) {
  80. return {
  81. id: term,
  82. text: term
  83. };
  84. }
  85. },
  86. id: function(element) {
  87. return element.id;
  88. },
  89. formatResult: function (tag) {
  90. return tag.text;
  91. },
  92. formatSelection: function (tag) {
  93. return tag.text;
  94. },
  95. escapeMarkup: function(m) {
  96. return m;
  97. }
  98. })
  99. }
  100. },
  101. _validateRegex: function(string) {
  102. var regexRegex = /^\/(.*)\/([gui]{0,3})$/,
  103. result = regexRegex.exec(string);
  104. return result !== null;
  105. }
  106. };
  107. })();
  108. OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.RequestUserAgentPlugin);