requestuseragentplugin.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. {id: 'mail', text: t('workflowengine', 'Thunderbird & Outlook addons')}
  68. ]
  69. }
  70. ];
  71. if (this.predefinedValues.indexOf(check.value) === -1) {
  72. data.unshift({
  73. id: check.value,
  74. text: check.value
  75. });
  76. }
  77. $(element).select2({
  78. data: data,
  79. createSearchChoice: function(term) {
  80. if (self.predefinedValues.indexOf(check.value) === -1) {
  81. return {
  82. id: term,
  83. text: term
  84. };
  85. }
  86. },
  87. id: function(element) {
  88. return element.id;
  89. },
  90. formatResult: function (tag) {
  91. return tag.text;
  92. },
  93. formatSelection: function (tag) {
  94. return tag.text;
  95. },
  96. escapeMarkup: function(m) {
  97. return m;
  98. }
  99. })
  100. }
  101. },
  102. _validateRegex: function(string) {
  103. var regexRegex = /^\/(.*)\/([gui]{0,3})$/,
  104. result = regexRegex.exec(string);
  105. return result !== null;
  106. }
  107. };
  108. })();
  109. OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.RequestUserAgentPlugin);