form.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. (function ($) {
  2. /*
  3. provides feedback form for zammad
  4. */
  5. var pluginName = 'zammad_form',
  6. defaults = {
  7. debug: false,
  8. noCSS: false,
  9. showTitle: false,
  10. messageTitle: 'Zammad Form',
  11. messageSubmit: 'Submit',
  12. messageThankYou: 'Thank you for your inquiry! We\'ll contact you soon as possible.',
  13. };
  14. function Plugin( element, options ) {
  15. this.element = element;
  16. this.$element = $(element)
  17. this.options = $.extend( {}, defaults, options) ;
  18. this._defaults = defaults;
  19. this._name = pluginName;
  20. this._endpoint_config = '/api/v1/form_config'
  21. this._endpoint_submit = '/api/v1/form_submit'
  22. this._script_location = '/assets/form/form.js'
  23. this._css_location = '/assets/form/form.css'
  24. src = document.getElementById('zammad_form_script').src
  25. this.css_location = src.replace(this._script_location, this._css_location)
  26. this.endpoint_config = src.replace(this._script_location, this._endpoint_config)
  27. this.endpoint_submit = src.replace(this._script_location, this._endpoint_submit)
  28. this._config = {}
  29. this.attributes = [
  30. {
  31. display: 'Name',
  32. name: 'name',
  33. tag: 'input',
  34. type: 'text',
  35. placeholder: 'Your Name',
  36. },
  37. {
  38. display: 'Email',
  39. name: 'email',
  40. tag: 'input',
  41. type: 'email',
  42. placeholder: 'Your Email',
  43. },
  44. {
  45. display: 'Message',
  46. name: 'body',
  47. tag: 'textarea',
  48. placeholder: 'Your Message...',
  49. rows: 7,
  50. },
  51. ]
  52. this.init();
  53. }
  54. Plugin.prototype.init = function () {
  55. var _this = this
  56. _this.log('init')
  57. if (!_this.options.noCSS) {
  58. _this.loadCss(_this.css_location)
  59. }
  60. _this.log('endpoint_config: ' + _this.endpoint_config)
  61. _this.log('endpoint_submit: ' + _this.endpoint_submit)
  62. // load config
  63. $.ajax({
  64. url: _this.endpoint_config,
  65. }).done(function(data) {
  66. _this.log('config:', data)
  67. _this._config = data
  68. }).fail(function() {
  69. alert('Faild to load form config!')
  70. });
  71. // show form
  72. if (!this.options.modal) {
  73. _this.render()
  74. }
  75. // bind form on call
  76. else {
  77. this.$element.on('click', function (e) {
  78. e.preventDefault()
  79. _this.render()
  80. return true
  81. })
  82. }
  83. }
  84. // load css
  85. Plugin.prototype.loadCss = function(filename) {
  86. if (document.createStyleSheet) {
  87. document.createStyleSheet(filename)
  88. }
  89. else {
  90. $('<link rel="stylesheet" type="text/css" href="' + filename + '" />').appendTo('head')
  91. }
  92. }
  93. // send
  94. Plugin.prototype.submit = function() {
  95. var _this = this
  96. $.ajax({
  97. method: 'post',
  98. url: _this.endpoint_submit,
  99. data: _this.getParams(),
  100. }).done(function(data) {
  101. // removed errors
  102. _this.$form.find('.has-error').removeClass('has-error')
  103. // set errors
  104. if (data.errors) {
  105. $.each(data.errors, function( key, value ) {
  106. _this.$form.find('[name=' + key + ']').closest('.form-group').addClass('has-error')
  107. })
  108. return
  109. }
  110. // ticket has been created
  111. _this.thanks()
  112. }).fail(function() {
  113. alert('Faild to submit form!')
  114. });
  115. }
  116. // get params
  117. Plugin.prototype.getParams = function() {
  118. var _this = this,
  119. params = {}
  120. $.each( _this.$form.serializeArray(), function( index, item ) {
  121. params[item.name] = item.value
  122. })
  123. if (!params.title) {
  124. params.title = this.options.messageTitle
  125. }
  126. _this.log('params', params)
  127. return params
  128. }
  129. Plugin.prototype.closeModal = function() {
  130. if (this.$modal) {
  131. this.$modal.remove()
  132. }
  133. }
  134. // render form
  135. Plugin.prototype.render = function(e) {
  136. var _this = this
  137. _this.closeModal()
  138. var element = '<div class="modal">\
  139. <div class="modal-backdrop js-close"></div>\
  140. <div class="modal-body">\
  141. <form class="zammad-form"></form>\
  142. </div>\
  143. </div>'
  144. if (!this.options.modal) {
  145. element = '<div><form class="zammad-form"></form></div>'
  146. }
  147. var $element = $(element)
  148. var $form = $element.find('form')
  149. if (this.options.showTitle && this.options.messageTitle != '') {
  150. $form.append('<h2>' + this.options.messageTitle + '</h2>')
  151. }
  152. $.each(this.attributes, function( index, value ) {
  153. var item = $('<div class="form-group"><label>' + value.display + '</label></div>')
  154. if (value.tag == 'input') {
  155. item.append('<input class="form-control" name="' + value.name + '" type="' + value.type + '" placeholder="' + value.placeholder + '">')
  156. }
  157. else if (value.tag == 'textarea') {
  158. item.append('<textarea class="form-control" name="' + value.name + '" placeholder="' + value.placeholder + '" rows="' + value.rows + '"></textarea>')
  159. }
  160. $form.append(item)
  161. })
  162. $form.append('<button type="submit" class="btn">' + this.options.messageSubmit + '</button')
  163. this.$modal = $element
  164. this.$form = $form
  165. // bind on close
  166. $element.find('.js-close').on('click', function (e) {
  167. e.preventDefault()
  168. _this.closeModal()
  169. return true
  170. })
  171. // bind form submit
  172. $element.on('submit', function (e) {
  173. e.preventDefault()
  174. _this.submit()
  175. return true
  176. })
  177. // show form
  178. if (!this.options.modal) {
  179. _this.$element.html($element)
  180. }
  181. // append modal to body
  182. else {
  183. $('body').append($element)
  184. }
  185. }
  186. // thanks
  187. Plugin.prototype.thanks = function(e) {
  188. var thanks = $('<div class="js-thankyou">' + this.options.messageThankYou + '</div>')
  189. this.$form.html(thanks)
  190. }
  191. // log method
  192. Plugin.prototype.log = function() {
  193. if (this.options.debug) {
  194. console.log(this._name, arguments)
  195. }
  196. }
  197. $.fn[pluginName] = function ( options ) {
  198. return this.each(function () {
  199. if (!$.data(this, 'plugin_' + pluginName)) {
  200. $.data(this, 'plugin_' + pluginName,
  201. new Plugin( this, options ));
  202. }
  203. });
  204. }
  205. }(jQuery));