(function (global, $, _, Backbone, Dropzone) { 'use strict'; var SettingsView, SettingView, NamePositionView, FieldFormView, FormPaddingsView, FontView, BackgroundColorView, BackgroundImageView, ThemesView, themes = [ { settings: { background: { type: 'color', value: 'transparent' }, font: { family: "Arial", size: 15 }, names_position: 'before', form_paddings: 'no', borders_type: 'rounded' }, submit: { color: '#2184db' } }, { settings: { background: { type: 'color', value: '#f6f7fa' }, font: { family: "PT Sans", size: 15 }, names_position: 'before', form_paddings: 'yes', borders_type: 'rounded' }, submit: { color: '#49b65d' } }, { settings: { background: { type: 'color', value: '#323d45' }, font: { family: "Georgia", size: 15 }, names_position: 'above', form_paddings: 'yes', borders_type: 'rectangular' }, submit: { color: '#f78d46' } }, { settings: { background: { type: 'image', value: AMOFORMS.images.url + 'bg/bg1.jpg' }, font: { family: "PT Sans", size: 15 }, names_position: 'inside', form_paddings: 'yes', borders_type: 'rounded' }, submit: { color: '#ff5722' } }, { settings: { background: { type: 'image', value: AMOFORMS.images.url + 'bg/bg3.jpg' }, font: { family: "Times New Roman", size: 15 }, names_position: 'inside', form_paddings: 'yes', borders_type: 'rectangular' }, submit: { color: '#f78d46' } }, { settings: { background: { type: 'image', value: AMOFORMS.images.url + 'bg/bg2.jpg' }, font: { family: "Lucida Console", size: 15 }, names_position: 'inside', form_paddings: 'yes', borders_type: 'rounded' }, submit: { color: '#2e95d1' } } ]; SettingView = Backbone.View.extend({ ns: '.amoforms:setting:view', className: 'amoforms', events: { 'click': 'stopPropagation' }, initialize: function (options) { this.options = options || {}; $(document).on('click' + this.ns, _.bind(this.remove, this)); }, _remove: function () {}, remove: function () { if (this._removed) { return; } this._remove(); this._removed = true; $(document).off(this.ns); Backbone.View.prototype.remove.apply(this, arguments); }, render: function () { this._render_template(); this._render(); this.reposition(); }, _render_template: function() { this.$el.html(this.template()); }, _render: function () {}, stopPropagation: function (e) { e.stopPropagation(); }, reposition: function () { var offset = this.options.$button.offset(); this.$el.css({ position: 'absolute', top: offset.top + this.options.$button[0].offsetHeight + 10, left: offset.left - (this.el.offsetWidth/2) + (this.options.$button[0].offsetWidth/2), zIndex: 10 }); }, commit: function (new_settings) { if (this.ajax_sended) { return; } this.ajax_sended = true; $.ajax({ url: ajaxurl, data: { action: AMOFORMS.ajax_action_prefix + 'update_design_settings', form: { id: this.options.form_id, settings: _.extend({}, new_settings) } }, dataType: 'json', type: 'POST' }) .always(_.bind(function () { this.ajax_sended = false; }, this)) .done(_.bind(function (res) { }, this)); } }); /* Themes View */ ThemesView = SettingView.extend({ className: SettingView.prototype.className + ' amoforms__setting-modal-themes', template: _.template( '
 + '.png)
File
', init: _.bind(function () { }, this), success: _.bind(function (file, data) { // @TODO: handle !data.result if (!data.result) { return; } data = _.extend({ image_url: '', thumb_url: '', img_basename: '' }, data); var background = { url: data.image_url, thumb: {url: data.thumb_url}, img_basename: data.img_basename }; AMOFORMS.images.backgrounds.push(background); this._render_template(); this.initDropZone(); }, this) }); }, destroyDropZone: function () { this.drop_zone && this.drop_zone.destroy && this.drop_zone.destroy(); $(document).off('click' + this.ns, '.dz-input-hidden'); }, deleteImages: function (e) { var $this = $(e.currentTarget); e.stopPropagation(); new AMOFORMS.core.confirm({ template_params: { caption: 'Are you sure you want to delete?', accept_btn: 'Yes', decline_btn: 'No' }, accept: _.bind(function (confirm) { if (this.$el.find('.amoforms__captcha').length > 0) { $('[data-type="captcha"]').attr('data-active', 'true'); } $.ajax({ url: ajaxurl, data: { action: AMOFORMS.ajax_action_prefix + 'delete_form_background', img_basename: $this.parent().attr('data-item') }, dataType: 'json', type: 'POST' }).done(_.bind(function () { var nameImg = $this.parent().attr('data-item'); $this.closest('.js-background-choose').remove(); for (var p in AMOFORMS.images.backgrounds) { if (nameImg == AMOFORMS.images.backgrounds[p].img_basename) { AMOFORMS.images.backgrounds.splice(p, 1); } } }, this) ); this.changed = ['image', '']; confirm.done(); }, this), fail: _.bind(function () { confirm.failed(); }, this), }); }, removeImages: function(e) { var $this = $(e.currentTarget); e.remove(); this.commit({ background: { type: 'color', value: color } }); }, getTextColorClear: function (hex_color) { return AMOFORMS.core.fn.isDarkColor(hex_color) ? '#fff' : '#313942'; }, clearBackground: function (e) { var color = 'transparent', text_color = this.getTextColorClear(color); e.stopPropagation(); this.options.changeSetting('background-color', [color, text_color]); this.changed = ['color', color]; }, changeBackgroundImage: function (e) { var $this = $(e.currentTarget), val = $this.attr('data-value'); e.stopPropagation(); $this.parent().find('.selected').removeClass('selected'); $this.addClass('selected'); this.options.$button.attr('data-value', val); this.options.changeSetting(this.css_rule, [val]); this.changed = ['image', this.options.$button.attr('data-value')]; }, _remove: function () { if (!this.changed || !this.changed[0] || !this.changed[1]) { return; } this.commit({ background: { type: this.changed[0], value: this.changed[1] } }); }, destroy: function () { this.destroyDropZone(); SettingView.prototype.destroy.call(this, arguments); } }); /* End of Background Image View */ SettingsView = Backbone.View.extend({ events: _.extend({}, SettingView.prototype.events, { 'click .amoforms__fields__settings__btn': 'newSetting' }), style_el_id: 'amoforms_css_theme', css_rules: { 'background-image': { selector: ['.amoforms__fields__editor', '.amoforms__fields__view'], getValue: function (url) { return 'background-image:url(' + url + ')!important;color:#fff!important;background-size: cover;background-position: center center;'; }, afterUpdate: function () { this.css_rules['background-color'].value = ''; }, value: '' }, 'background-color': { selector: ['.amoforms__fields__editor', '.amoforms__fields__view'], getValue: function (color, text_color) { return 'background-color:' + color + '!important;color:' + text_color + '!important;'; }, afterUpdate: function () { this.css_rules['background-image'].value = ''; }, value: '' }, 'font': { selector: [ '.amoforms__fields__row__inner__name', '.amoforms__fields__row__inner__control', '.amoforms__fields__row__inner__control input', '.amoforms__fields__row__submit' ], getValue: function (font_family, font_size) { var heading_size = (font_size + Math.floor(font_size*1.3)); return 'font-family:"' + font_family + '"!important;font-size:' + font_size + 'px!important;}.amoforms .amoforms__heading .amoforms__h{line-height:' + (heading_size + 10) +'px!important;font-size:' + heading_size + 'px!important;'; }, value: '' }, 'field-form': { selector: [ '.amoforms__heading .amoforms__h', '.amoforms__text-input', '.selectize-control .selectize-input', '.selectize-dropdown', '.amoforms__fields__row__submit', '.amoforms__select__input' ], getValue: function (value) { return 'border-radius:' + ((value == 'rounded') ? 8 : 0) + 'px!important;'; }, value: '' }, 'form-paddings': { selector: [ '.amoforms__fields__view', '.amoforms__fields__editor' ], getValue: function (value) { return value == 'yes' ? '' : 'padding:0!important;border:0!important;} ' + '.amoforms .amoforms__fields__row:first-of-type {padding-top:0!important;'; }, value: '' } }, allowed_settings: { 'themes': ThemesView, 'name-position': NamePositionView, 'field-form': FieldFormView, 'form-paddings': FormPaddingsView, 'font': FontView, 'background-color': BackgroundColorView, 'background-image': BackgroundImageView }, initialize: function (options) { this.options = options || {}; this.updateRulesFromButtons(); }, updateRulesFromButtons: function () { var type; this.$('.amoforms__fields__settings__btn').each(_.bind(function (key, el) { type = el.getAttribute('data-type'); switch (type) { case 'background-color': if (!this.$('[data-type="background-image"]').attr('data-value')) { var color = $(el).find('.color').css('background-color'); if (color.indexOf('rgba') != -1) { var alpha = color.replace(/^.*,(.+)\)/,'$1') if (alpha == 0) { color = 'transparent'; } } else if (color.indexOf('transparent') != -1) { color = 'transparent'; } else { color = AMOFORMS.core.fn.rgb2hex(color); } this.css_rules['background-color'].value = [color, BackgroundColorView.prototype.getTextColor(color)]; } break; case 'form-paddings': case 'field-form': case 'background-image': if (el.getAttribute('data-value')) { this.css_rules[type].value = [el.getAttribute('data-value')]; } break; case 'font': this.css_rules['font'].value = [el.getAttribute('data-font-family'), parseInt(el.getAttribute('data-font-size'), 10)]; break; } }, this)); }, updateCSS: function (rule, value) { var css_rules_text = '', style_el = document.getElementById(this.style_el_id); if (rule && value && this.css_rules[rule]) { this.css_rules[rule].value = value; if (_.isFunction(this.css_rules[rule].afterUpdate)) { this.css_rules[rule].afterUpdate.call(this); } } _.each(this.css_rules, function (rule) { if (rule.value) { css_rules_text += '.amoforms ' + rule.selector.join(', .amoforms ') + '{' + rule.getValue.apply(this, rule.value) + '}'; } }, this); if (!style_el) { style_el = document.createElement('STYLE'); document.getElementsByTagName('head')[0].appendChild(style_el); style_el.setAttribute('id', this.style_el_id); } style_el.innerHTML = css_rules_text; $(document).trigger('amoforms:field:render', [this.$('[data-type="name-position"]').attr('data-value')]); }, newSetting: function (e) { var $this = $(e.currentTarget), current_inited = false; e.stopPropagation(); if (this.current_setting) { current_inited = !this.current_setting._removed && (this.current_setting instanceof this.allowed_settings[$this.attr('data-type')]); this.current_setting.remove(); if (current_inited) { return; } } this.current_setting = new this.allowed_settings[$this.attr('data-type')]({ form_id: this.options.form_id, $button: $this, $fields: this.options.$fields, changeSetting: _.bind(this.updateCSS, this), changeDesign: _.bind(this.updateDesign, this) }); $(document.body).append(this.current_setting.$el); this.current_setting.render(); }, updateDesign: function (theme_index) { var theme = themes[theme_index]; _.each(theme.settings, function (setting, key) { switch (key) { case 'background': if (setting.type == 'color') { this.$('[data-type="background-color"] .color').css('background-color', setting.value); this.$('[data-type="background-image"]').attr('data-value', ''); this.css_rules['background-image'].value = ''; } else { this.$('[data-type="background-image"]').attr('data-value', setting.value); this.$('[data-type="background-color"] .color').css('background-color', '#fff'); this.css_rules['background-color'].value = ''; } break; case 'names_position': this.$('[data-type="name-position"]').attr('data-value', setting); break; case 'font': this.$('[data-type="font"]') .attr('data-font-size', parseInt(setting.size, 10)) .attr('data-font-family', setting.family); break; case 'borders_type': this.$('[data-type="field-form"]').attr('data-value', setting); break; case 'form_paddings': this.$('[data-type="form-paddings"]').attr('data-value', setting); break; } }, this); this.updateRulesFromButtons(); this.updateCSS(); this.options.submit.renderChange([{ key: 'color', value: theme.submit.color }]); } }); global.AMOFORMS = $.extend(true, global.AMOFORMS || {}, { views: { settings: SettingsView } }); }(window, jQuery, _, Backbone, Dropzone));