(function ($) { /* * * provides feedback form for zammad * */ var pluginName = 'ZammadForm', defaults = { debug: false, noCSS: false, showTitle: false, messageTitle: 'Zammad Form', messageSubmit: 'Submit', messageThankYou: 'Thank you for your inquiry! We\'ll contact you soon as possible.', messageNoConfig: 'Unable to load form config from server. Maybe featrue is disabled.', attributes: [ { display: 'Name', name: 'name', tag: 'input', type: 'text', placeholder: 'Your Name', }, { display: 'Email', name: 'email', tag: 'input', type: 'email', placeholder: 'Your Email', }, { display: 'Message', name: 'body', tag: 'textarea', placeholder: 'Your Message...', rows: 7, }, ] }; function Plugin( element, options ) { this.element = element; this.$element = $(element) this.options = $.extend( {}, defaults, options) ; this._defaults = defaults; this._name = pluginName; this._endpoint_config = '/api/v1/form_config' this._endpoint_submit = '/api/v1/form_submit' this._script_location = '/assets/form/form.js' this._css_location = '/assets/form/form.css' this._src = document.getElementById('zammad_form_script').src this.css_location = this._src.replace(this._script_location, this._css_location) this.endpoint_config = this._src.replace(this._script_location, this._endpoint_config) this.endpoint_submit = this._src.replace(this._script_location, this._endpoint_submit) this._config = {} this.init(); } Plugin.prototype.init = function () { var _this = this _this.log('debug', 'init', this._src) if (!_this.options.noCSS) { _this.loadCss(_this.css_location) } _this.log('debug', 'endpoint_config: ' + _this.endpoint_config) _this.log('debug', 'endpoint_submit: ' + _this.endpoint_submit) // load config $.ajax({ url: _this.endpoint_config, }).done(function(data) { _this.log('debug', 'config:', data) _this._config = data }).fail(function(jqXHR, textStatus, errorThrown) { if (jqXHR.status == 401) { _this.log('error', 'Faild to load form config, feature is disabled!') } else { _this.log('error', 'Faild to load form config!') } _this.noConfig() }); // show form if (!this.options.modal) { _this.render() } // bind form on call else { this.$element.on('click', function (e) { e.preventDefault() _this.render() return true }) } } // load css Plugin.prototype.loadCss = function(filename) { if (document.createStyleSheet) { document.createStyleSheet(filename) } else { $('').appendTo('head') } } // send Plugin.prototype.submit = function() { var _this = this // check min modal open time if (_this.modalOpenTime) { var currentTime = new Date().getTime() var diff = currentTime - _this.modalOpenTime.getTime() _this.log('debug', 'currentTime', currentTime) _this.log('debug', 'modalOpenTime', _this.modalOpenTime.getTime()) _this.log('debug', 'diffTime', diff) if (diff < 1000*8) { alert('Sorry, you look like an robot!') return } } $.ajax({ method: 'post', url: _this.endpoint_submit, data: _this.getParams(), }).done(function(data) { // removed errors _this.$form.find('.has-error').removeClass('has-error') // set errors if (data.errors) { $.each(data.errors, function( key, value ) { _this.$form.find('[name=' + key + ']').closest('.form-group').addClass('has-error') }) return } // ticket has been created _this.thanks() }).fail(function() { alert('Faild to submit form!') }); } // get params Plugin.prototype.getParams = function() { var _this = this, params = {} $.each( _this.$form.serializeArray(), function( index, item ) { params[item.name] = item.value }) if (!params.title) { params.title = this.options.messageTitle } _this.log('debug', 'params', params) return params } Plugin.prototype.closeModal = function() { if (this.$modal) { this.$modal.remove() } } // render form Plugin.prototype.render = function(e) { var _this = this _this.closeModal() _this.modalOpenTime = new Date() _this.log('debug', 'modalOpenTime:', _this.modalOpenTime) var element = '