(function($) { smuzform.App = { Models: {}, Collections: {}, Views: {}, Dispatcher: _.clone(Backbone.Events), }; smuzform.App.isNewForm = function() { return true; }; function smuzform_deentitize_str( str ) { var ret = str.replace(/>/g, '>'); ret = ret.replace(/</g, '<'); ret = ret.replace(/"/g, '"'); ret = ret.replace(/'/g, "'"); ret = ret.replace(/&/g, '&'); return ret; } smuzform.App.Models.FormSettings = Backbone.Model.extend({ initialize: function() { }, defaults: function() { return { tmpid: '', fields: {}, title: 'Form Name', description: 'Click me to change the description.', lang: 'en', labelPlacement: 'top', confirmationOption: 'text', textMessage: 'Thanks for submitting your information', redirectUrl: '', captcha: false, noOfEntries: '', onlySingleIP: false, formTimer: false, formStartTime: null, formEndTime: null, theme: {}, extraData: {}, confirmationEmailToUser: false, emailAddressField: '', replyToEmail: '', submitBtn: true, submitBtnText: 'Submit', submitBtnType: 'normal', errorMessages: {}, style: {}, mailchimpAddOnData: {}, addOnData: [] }; }, url: ajaxurl+'?action=smuzform_api&nonce='+smuzform.form_model_nonce }); var Field = Backbone.Model.extend({ validate: function( attr ) { if ( attr.type === undefined ) return 'Set Field Type'; }, initialize: function() { this.on( 'invalid', function( model, error ) { console.log( error ); } ); this.on( 'change:label', function( model ) { } ); }, defaults: function() { return { label: 'Untitled Label', type: undefined, size: 'medium', required: false, noDuplicates: false, visibility: true, labelVisible: true, rangeMin: '', rangeMax: '', rangeFormat: 'char', preValue: '', helpText: '', cssClasses: '', placeholderText: '', radios: false, checkboxes: false, textAreaRows: 10, textAreaCols: 60, cssID: 'wpfieldid', choices: ['First Choice','Second Choice', 'Third Choice'], selectedChoice: null, checkboxSelectedChoice: [], sectionTitle: 'Section Title', sectionDescription: 'Click me to change the description', extraData: null, ruleEnabled: false, rules: { field: '', operator: 'is', cmpValue: '', action: 'show' }, statements: {}, columns: {}, file: { allowed: [ 'jpg','jpeg','png','gif','pdf','doc','docx','key','ppt','pptx','pps','ppsx','odt','xls','xlsx','zip','mp3','m4a','ogg','wav','mp4','m4v','wmv','avi','mpg','ogv','3gp','3g2' ], maxSize: 2 }, login: {} }; } }); var FieldsCollection = Backbone.Collection.extend({ model: Field, initialize: function() { } }); $(document).ready(function() { smuzform.App.Views.NavBar = Backbone.View.extend({ template: _.template( $('#formNavBar-template').html() ), el: $('#navBarActionsCont'), events: { 'click #formExportAction': 'onClickFormExportAction', 'click #formEntriesAction': 'onClickFormEntriesAction', 'click #formNotificationAction': 'onClickFormNotificationAction', 'click #formStyleAction': 'onClickFormStyleAction' }, initialize: function( model ) { this.model = model; this.model.on( 'change', this.render, this ); }, render: function() { this.$el.html( this.template( this.model.attributes ) ); return this; }, onClickFormExportAction: function(e) { if ( ! FormPreviewModel.get('id') ) { alert('Please create a form first. Click the create form button below'); return false; } //this.render(); FormPreviewModel.set( 'tmpid', FormPreviewModel.get('id') ); $('#exportModal').modal('show'); }, onClickFormEntriesAction: function(e) { var redirectLink = smuzform.admin_url+'admin.php?page=smuz-forms-entry&form_id='+FormPreviewModel.get('id'); if ( ! FormPreviewModel.get('id') ) alert('Please create a form first. Click the create form button below'); else window.location = redirectLink; }, onClickFormNotificationAction: function(e) { var redirectLink = smuzform.admin_url+'admin.php?page=smuz-forms-notifications&form_id='+FormPreviewModel.get('id'); if ( ! FormPreviewModel.get('id') ) alert('Please create a form first. Click the create form button below'); else window.location = redirectLink; }, onClickFormStyleAction: function(e) { var redirectLink = smuzform.admin_url+'admin.php?page=smuz-forms-style&form_id='+FormPreviewModel.get('id'); if ( ! FormPreviewModel.get('id') ) alert('Please create a form first. Click the create form button below'); else window.location = redirectLink; } }); smuzform.App.Views.FormPreview = Backbone.View.extend({ template: _.template( $('#formPreview-template').html() ), el: $('#formPreviewCont'), initialize: function( model ) { this.model = model; this.model.on( 'change', this.render, this ); }, render: function() { this.$el.html( this.template( this.model.attributes ) ); return this; } }); smuzform.App.Views.FormSettings = Backbone.View.extend({ template: _.template( $('#formSettings-template').html() ), el: $('#formSettingsCont'), initialize: function() { this.model.on( 'change', this.render, this ); }, render: function() { this.$el.html( this.template( this.model.attributes ) ); return this; }, events: { 'change #formTitle': 'changeTitle', 'change #formDescription': 'changeDescription', 'change #formLang': 'changeLanguage', 'change #formLabelPlacement': 'changeLabelPlacement', 'change input[name=optConfOption]': 'changeConfirmationOption', 'change #formCaptcha': 'changeCaptcha', 'change #formSubmitBtnText': 'changeSubmitBtnText', 'change #formRedirectUrl': 'changeRedirectUrl', 'change #formTextMessage': 'changeTextMessage' }, changeTitle: function(e) { this.model.set( 'title', $(e.currentTarget).val() ); }, changeDescription: function(e) { this.model.set( 'description', $(e.currentTarget).val() ); }, changeLanguage: function(e) { this.model.set( 'lang', $(e.currentTarget).val() ); }, changeLabelPlacement: function(e) { this.model.set( 'labelPlacement', $(e.currentTarget).val() ); }, changeConfirmationOption: function(e) { if ( $(e.currentTarget).val() === 'text' ) this.model.set( 'confirmationOption', 'text' ); if ( $(e.currentTarget).val() === 'redirect' ) this.model.set( 'confirmationOption', 'redirect' ); }, changeCaptcha: function(e) { if ( $(e.currentTarget).is(':checked') ) this.model.set( 'captcha', true ); else this.model.set( 'captcha', false ); }, changeSubmitBtnText: function(e) { this.model.set( 'submitBtnText', $(e.currentTarget).val() ); }, changeRedirectUrl: function(e) { this.model.set( 'redirectUrl', $(e.currentTarget).val() ); }, changeTextMessage: function(e) { this.model.set( 'textMessage', $(e.currentTarget).val() ); }, setModel: function(model) { this.model = model; } }); var FieldsSettingsView = Backbone.View.extend({ template: _.template( $('#fieldSettings-template').html() ), el: $('#fieldSettingsCont'), callCounter: 0, initialize: function() { }, events: { 'change #fieldLabel': 'changeLabel', 'change #fieldPlaceholder': 'changePlaceholder', 'change #fieldValue': 'changeValue', 'change .fieldOptChoice': 'changeChoice', 'click .optChoiceRemove': 'removeChoice', 'click #addNewChoice': 'addChoice', 'click .optChoiceSelect': 'toggleChoice', 'change #optionRequired': 'changeRequired', 'change input[name=fieldOptionRadio]': 'changeVisibility', 'change #fieldSectionTitle': 'changeSectionTitle', 'change #fieldSectionDescription': 'changeSectionDescription', 'change #fieldSize': 'changeSize', 'change #fieldFileMaxSize': 'changeFileMaxSize', 'change #fieldFileAllowed': 'changeFileAllowed', 'change #fieldMinLength': 'changeFieldMinLength', 'change #fieldMaxLength': 'changeFieldMaxLength', 'change #enableConditionalLogic': 'changeEnableRule', 'change #ruleFields': 'changeRuleField', 'change #ruleFieldAction': 'changeRuleFieldAction', 'change #ruleFieldLogicValue': 'changeRuleFieldLogicValue', 'change #ruleFieldOperator': 'changeRuleFieldOperator', 'change #optionNoDuplicate': 'changeNoDuplicates', 'change #fieldCssClasses': 'changeCssClasses', /** events for custom text **/ 'change #customTextTag': 'changeCustomTextTag', 'change #customTextColor': 'changeCustomTextColor', 'change #customTextFontSize': 'changeCustomTextFontSize', 'click #customTextBold': 'changeCustomTextBold', 'change #customTextValue': 'changeCustomTextValue', /** events for custom image **/ 'change #customImageUrl': 'changeCustomImageUrl', 'change #customImageWidth': 'changeCustomImageWidth', 'change #customImageHeight': 'changeCustomImageHeight', /** events for custom html **/ 'change #customHtmlSource': 'changeCustomHtmlSource' }, render: function() { if ( this.model ) this.$el.html( this.template( this.model.attributes ) ); else this.$el.html( $('#nofieldSelected-template').html() ); return this; }, changeRuleField: function(e) { var value = $(e.currentTarget).val(); if ( value === '' ) return; var temp = _.clone( this.model.get( 'rules' ) ); temp.field = value; this.model.set( 'rules', temp ); }, changeRuleFieldOperator: function(e) { var value = $(e.currentTarget).val(); if ( value === '' ) return; var temp = _.clone( this.model.get( 'rules' ) ); temp.operator = value; this.model.set( 'rules', temp ); }, changeRuleFieldAction: function(e) { var value = $(e.currentTarget).val(); if ( value === '' ) return; var temp = _.clone( this.model.get( 'rules' ) ); temp.action = value; this.model.set( 'rules', temp ); }, changeRuleFieldLogicValue: function(e) { var value = $(e.currentTarget).val(); if ( value === '' ) return; var temp = _.clone( this.model.get( 'rules' ) ); temp.cmpValue = value; this.model.set( 'rules', temp ); }, changeEnableRule: function(e) { if ( $(e.currentTarget).is(':checked') ) this.model.set( 'ruleEnabled', true ); else this.model.set( 'ruleEnabled', false ); }, changeSize: function(e) { this.model.set( 'size', $(e.currentTarget).val() ); }, changeVisibility: function(e) { if ( $(e.currentTarget).val() === 'visible' ) this.model.set( 'labelVisible', true ); else this.model.set( 'labelVisible', false ); }, changeRequired: function(e) { if ( $(e.currentTarget).is(':checked') ) this.model.set( 'required', true ); else this.model.set( 'required', false ); }, changeNoDuplicates: function(e) { if ( $(e.currentTarget).is(':checked') ) this.model.set( 'noDuplicates', true ); else this.model.set('noDuplicates', false ); }, changeLabel: function(e) { this.model.set( 'label', $('#fieldLabel').val() ); e.stopImmediatePropagation(); }, changePlaceholder: function(e) { this.model.set( 'placeholderText', $('#fieldPlaceholder').val() ); e.stopImmediatePropagation(); }, changeValue: function(e) { var value = $(e.currentTarget).val(); if ( this.model.get('type') === 'date' ) { if ( value !== '' ) { var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/; if ( pattern.test( value ) ) { var arr = value.split('/'); var temp = { day: arr[0], month: arr[1], year: arr[2] } this.model.set( 'extraData', temp ); } } } this.model.set( 'preValue', value ); e.stopImmediatePropagation(); }, changeChoice: function(e) { var choices = this.model.get( 'choices' ); choices = _.clone( choices ); var choiceText = $(e.currentTarget).val(); var index = $(e.currentTarget).data('index'); index = parseInt( index ); choices[index] = choiceText; this.model.set( 'choices', choices ); e.stopImmediatePropagation(); }, removeChoice: function(e) { var choices = this.model.get( 'choices' ); choices = _.clone( choices ); var index = $(e.currentTarget).data('index'); index = parseInt( index ); choices.splice( index, 1 ); this.model.set( 'choices', choices ); smuzform.App.Views.SettingsView.render(); e.stopImmediatePropagation(); }, addChoice: function(e) { var choices = this.model.get( 'choices' ); choices = _.clone( choices ); choices.push(''); this.model.set( 'choices', choices ); smuzform.App.Views.SettingsView.render(); e.stopImmediatePropagation(); }, toggleChoice: function(e) { var index = $(e.currentTarget).data('index'); var selectedIndex = this.model.get( 'selectedChoice' ); selectedIndex = parseInt( selectedIndex ); index = parseInt( index ); if ( $(e.currentTarget).is(':checked') ) { if ( selectedIndex === index ) { $(e.currentTarget).prop( 'checked', false ); this.model.set( 'selectedChoice', null ); return; } } if ( $(e.currentTarget).is(':checked') ) this.model.set( 'selectedChoice', index ); else this.model.set( 'selectedChoice', null ); e.stopImmediatePropagation(); }, changeSectionTitle: function(e) { this.model.set( 'sectionTitle', $(e.currentTarget).val() ); }, changeSectionDescription: function(e) { this.model.set( 'sectionDescription', $(e.currentTarget).val() ); }, changeFileAllowed: function(e) { var temp = this.model.get( 'file' ); var types = $(e.currentTarget).val().split(','); temp.allowed = types; this.model.set( 'file', temp ); }, changeFileMaxSize: function(e) { var temp = this.model.get( 'file' ); var size = parseInt( $(e.currentTarget).val() ); temp.maxSize = size; this.model.set( 'file', temp ); }, changeFieldMinLength: function(e) { var minLength = $(e.currentTarget).val(); if ( ! minLength === '' ) minLength = parseInt(minLength); this.model.set( 'rangeMin', minLength ); }, changeFieldMaxLength: function(e) { var maxLength = $(e.currentTarget).val(); if ( ! maxLength === '' ) maxLength = parseInt(maxLength); this.model.set( 'rangeMax', maxLength ); }, processCustomTextHtml: function() { var text = _.clone( this.model.get( 'extraData' ) ); var value = ''; if ( text.bold ) text.bold = 'bold'; else text.bold = 'normal'; value = $('
Change Me
', tag: 'p', size: 14, color: '#000000', bold: false }; var TextModel = new Field( { type: 'customText', label: 'Custom Text', cssID: 'wpformfield'+Math.floor((Math.random() * 200000) + 100), extraData: textData }); smuzform.App.Collections.FieldsCol.add( TextModel ); }); $('.customImage button').click(function() { var imageData = { imgUrl: '', readyHtml: '