(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.CS_generator = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
window.csg = window.csg || {};

(function( $, csg ){

  /**
   * Load templates
   */
  csg.Templates = require('./templates'),

  /**
   * Accessor for precomplied templates
   */
  csg.template = function () {
    return this.Templates[ (arguments.length == 1) ? arguments[0] : arguments.join('/') ];
  },

  /**
   * Localization helper
   */
  csg.l18n = function( key ) {
    return csgData.strings[key] || '';
  }

  /**
   * Load Controls
   */
  csg.Controls = {
    'base': require('./views/controls/base'),
    'textfield': require('./views/controls/text'),
    'textarea': require('./views/controls/text-area'),
    'textarea_html': require('./views/controls/text-area'),
    'dropdown': require('./views/controls/dropdown'),
    'checkbox': require('./views/controls/checkbox'),
    'colorpicker': require('./views/controls/colorpicker'),
    'attach_image': require('./views/controls/image-upload'),
  }

  /**
   * Control Lookup helper
   */
  csg.controlLookup = function( type ) {
    return (csg.Controls[type]) ? csg.Controls[type] : csg.Controls['base'];
  }

  /**
   * Load Models & Views
   */
  var ShortcodeCollection = require('./models/shortcode-collection');
  var ModalView =  require('./views/modal');

  // Controller
  // =============================================================================

  var Controller = function( wrapper ) {



    $(window).on( 'load', function(){
    	this.shortcodes = new ShortcodeCollection();
    	this.shortcodes.fetch( {reset: true } );
    }.bind(this));


    $(document).on( 'click', '#cs-insert-shortcode-button', function( e ){
      e.preventDefault();
      this.openModal();
    }.bind(this));

  }

  Controller.prototype.openModal = function() {

  	if ( this.modalView === undefined ) {
  		this.modalView = new ModalView({ collection: this.shortcodes, controller: this });
  	}
  }

  Controller.prototype.deleteModal = function() {
  	this.modalView = undefined;
  }

  exports.Controller = Controller;
  exports.controller = new Controller();

})( jQuery, window.csg );

},{"./models/shortcode-collection":4,"./templates":13,"./views/controls/base":17,"./views/controls/checkbox":18,"./views/controls/colorpicker":19,"./views/controls/dropdown":20,"./views/controls/image-upload":21,"./views/controls/text":23,"./views/controls/text-area":22,"./views/modal":24}],2:[function(require,module,exports){
module.exports = Backbone.Collection.extend( { model: require('./control') } );
},{"./control":3}],3:[function(require,module,exports){
module.exports = Backbone.Model.extend({
  defaults: {
    param_name: 'generic-param',
    heading: 'Generic Control',
    description: '',
    type: 'Generic',
    default_value: "",
    value: null
  }
});
},{}],4:[function(require,module,exports){
var ShortcodeCollection = Backbone.Collection.extend({

  model: require('./shortcode'),
	url: window.csgData.shortcodeCollectionUrl,

  selected: null,

  section: function( section ) {

    bySection = this.filter( function( shortcode ) {
      return shortcode.get('section') === section;
    });

    return new ShortcodeCollection( bySection );
  },

  setSelected: function( shortcode ) {
    this.selected = shortcode;
    this.trigger('new_selection');
  }
});

module.exports = ShortcodeCollection;
},{"./shortcode":5}],5:[function(require,module,exports){
module.exports = Backbone.Model.extend({
	defaults: {
		id: 'generic-shortcode',
		title: 'Generic Shortcode',
		icon: 'icon',
		section: 'Generic',
		description: 'A Shortcode Description',
		params: [],
		demo: ''
	},

  setSelected:function() {
    this.collection.setSelected(this);
  }
});
},{}],6:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/blank-state 
__p+=' <div class="csg-intro"> <p>';
 print(csg.l18n('blank-window')); 
__p+='</p> </div>';
}
return __p;
};

},{}],7:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/controls/checkbox 
__p+=' <label for="param-'+
((__t=( param_name ))==null?'':__t)+
'"><strong>'+
((__t=( heading ))==null?'':__t)+
'</strong><span>'+
((__t=( description ))==null?'':__t)+
'</span></label> <input type="checkbox" id="param-'+
((__t=( param_name ))==null?'':__t)+
'" name="param-'+
((__t=( param_name ))==null?'':__t)+
'" value="'+
((__t=( value ))==null?'':__t)+
'">';
}
return __p;
};

},{}],8:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/controls/color-picker 
__p+=' <label for="param-'+
((__t=( param_name ))==null?'':__t)+
'"><strong>'+
((__t=( heading ))==null?'':__t)+
'</strong><span>'+
((__t=( description ))==null?'':__t)+
'</span></label> <input type="text" name="param-'+
((__t=( param_name ))==null?'':__t)+
'" id="param-'+
((__t=( param_name ))==null?'':__t)+
'" class="wp-color-picker" value="" size="30">';
}
return __p;
};

},{}],9:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/controls/dropdown 
__p+=' <label for="param-'+
((__t=( param_name ))==null?'':__t)+
'"><strong>'+
((__t=( heading ))==null?'':__t)+
'</strong><span>'+
((__t=( description ))==null?'':__t)+
'</span></label> <select name="param-'+
((__t=( param_name ))==null?'':__t)+
'" id="param-'+
((__t=( param_name ))==null?'':__t)+
'"> ';
 _.each(value, function(option, name) { 
__p+=' <option value="';
 print( option.value || option ) 
__p+='">';
 print( (option.value) ? name : option) 
__p+=' </option> ';
 }); 
__p+=' </select>';
}
return __p;
};

},{}],10:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/controls/image-upload 
__p+=' <label for="param-'+
((__t=( param_name ))==null?'':__t)+
'"><strong>'+
((__t=( heading ))==null?'':__t)+
'</strong><span>'+
((__t=( description ))==null?'':__t)+
'</span></label> <div class="csg-image-uploader"> <a href="#" class="csg-img-control set"><span class="dashicons dashicons-plus"></span></a> <a href="#" class="csg-img-control remove hidden"><span class="dashicons dashicons-no"></span></a> </div> <input type="hidden" name="param-'+
((__t=( param_name ))==null?'':__t)+
'" id="param-'+
((__t=( param_name ))==null?'':__t)+
'">';
}
return __p;
};

},{}],11:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/controls/text-area 
__p+=' <label for="param-'+
((__t=( param_name ))==null?'':__t)+
'"><strong>'+
((__t=( heading ))==null?'':__t)+
'</strong><span>'+
((__t=( description ))==null?'':__t)+
'</span></label> <textarea name="param-'+
((__t=( param_name ))==null?'':__t)+
'" id="param-'+
((__t=( param_name ))==null?'':__t)+
'" rows="8" cols="5"></textarea>';
}
return __p;
};

},{}],12:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/controls/text 
__p+=' <label for="param-'+
((__t=( param_name ))==null?'':__t)+
'"><strong>'+
((__t=( heading ))==null?'':__t)+
'</strong><span>'+
((__t=( description ))==null?'':__t)+
'</span></label> <input type="text" name="param-'+
((__t=( param_name ))==null?'':__t)+
'" id="param-'+
((__t=( param_name ))==null?'':__t)+
'" value="'+
((__t=( value ))==null?'':__t)+
'" size="30">';
}
return __p;
};

},{}],13:[function(require,module,exports){
module.exports = {

  'blank-state':  require( './blank-state' ),
  'modal':        require( './modal' ),
  'preview':      require( './preview' ),

  'controls/checkbox':     require( './controls/checkbox'),
  'controls/color-picker': require( './controls/color-picker'),
  'controls/dropdown':     require( './controls/dropdown'),
  'controls/image-upload': require( './controls/image-upload'),
  'controls/text-area':    require( './controls/text-area'),
  'controls/text':         require( './controls/text')

};

},{"./blank-state":6,"./controls/checkbox":7,"./controls/color-picker":8,"./controls/dropdown":9,"./controls/image-upload":10,"./controls/text":12,"./controls/text-area":11,"./modal":14,"./preview":15}],14:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/modal 
__p+=' <div class="csg-modal"> <header> <h1>'+
((__t=( csg.l18n('modal-title') ))==null?'':__t)+
'</h1> <a class="csg-modal-btn csg-modal-toggle-advanced" href="#"> <span class="dashicons dashicons-media-code"></span> <span class="tip">'+
((__t=( csg.l18n('modal-toggle-advanced') ))==null?'':__t)+
'</span> </a> <a class="csg-modal-btn csg-modal-close" href="#"> <span class="dashicons dashicons-no"></span> </a> </header> <div class="csg-modal-content"> <section class="csg-modal-sidebar"></section> <section class="csg-modal-main" role="main"></section> </div> <footer> <button id="btn-ok" disabled="disabled" class="button button-primary button-large">'+
((__t=( csg.l18n('modal-insert-shortcode') ))==null?'':__t)+
'</button> </footer> </div> <div class="csg-modal-backdrop">&nbsp;</div>';
}
return __p;
};

},{}],15:[function(require,module,exports){
module.exports = function(obj){
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};
with(obj||{}){
__p+='';
 // generator/preview 
__p+=' <h2 class="csg-preview-title">';
 print(csg.l18n('preview-title')); 
__p+='</h2> <div class="csg-preview-content"> <p>';
 print(csgData.previewContentBefore) 
__p+='</p> <p><a class="button" href="'+
((__t=( demo ))==null?'':__t)+
'" target="_blank">';
 print(csg.l18n('preview-button')); 
__p+='</a></p> <p>';
 print(csgData.previewContentAfter) 
__p+='</p> </div>';
}
return __p;
};

},{}],16:[function(require,module,exports){
var ControlView = require('./controls/base');

module.exports = Backbone.View.extend({

  className: "csg-modal-controls",

  render: function(){
      this.collection.each( function( control ) {
        this.$el.append( ControlView.makeControl( control.get('type'), { model: control } ).render().$el );
      }, this );
      return this;
  },

});
},{"./controls/base":17}],17:[function(require,module,exports){
module.exports = Backbone.View.extend({

  className: "csg-control",
  template: csg.template('controls/text'),

  renderSuper: function() {
    if (this.model.get('advanced') == true )
      this.$el.addClass('advanced');

    this.$el.html( this.template( this.model.toJSON() ) );

    return this;
  },

  render: function() {
    this.renderSuper();
    this.bindInput();
    return this;
  },

  bindInput: function() {

    var model = this.model;

    model.set( 'data', this.$( '#param-' + model.get('param_name') ).val() );

    this.$( '#param-' + model.get('param_name') ).on('change',function(){
      model.set( 'data', jQuery(this).val() );
    });
  }
},{

  makeControl: function( type, options ) {
    var View = csg.controlLookup( type );
    return new View( options );
  }

});
},{}],18:[function(require,module,exports){
var ControlView = require('./base');

module.exports = ControlView.extend({
  template: csg.template('controls/checkbox'),

  bindInput: function() {

    var model = this.model;

    if (this.$( '#param-' + model.get('param_name') ).prop('checked')) {
      model.set( 'data', this.$( '#param-' + model.get('param_name') ).val() );
    }

    this.$( '#param-' + model.get('param_name') ).on('change',function(){

      if (jQuery(this).prop('checked')) {
        model.set( 'data', jQuery(this).val() );
        return;
      }

      model.unset( 'data' );

    });

  }
});
},{"./base":17}],19:[function(require,module,exports){
var ControlView = require('./base');
module.exports = ControlView.extend({

  template: csg.template('controls/color-picker'),

  render: function() {
    this.renderSuper();

    this.$('.wp-color-picker').wpColorPicker({ change: this.colorChange.bind(this) });

    return this;
  },

  colorChange: function( e, ui) {
    this.model.set('data', ui.color.toString() );
  },

  bindInput: function(){}

});
},{"./base":17}],20:[function(require,module,exports){
var ControlView = require('./base');
module.exports = ControlView.extend({
  template: csg.template('controls/dropdown'),
  initialize: function() {
    this.model.set('selected', this.model.get('default_value') || _(this.model.get('value')).find(function(){ return true; }) );
  }
});
},{"./base":17}],21:[function(require,module,exports){
var ControlView = require('./base');

var ControlImageUploadView = ControlView.extend({

  initialize: function() {
    ControlImageUploadView.createMediaFrame();
  },

  events: {
    'click a.csg-img-control.set'    : 'setImage',
    'click a.csg-img-control.remove' : 'removeImage',
  },

  template: csg.template('controls/image-upload'),

  setImage: function(e) {

    if (e) e.preventDefault();

    var uploader = ControlImageUploadView.uploader;

    uploader.off( 'insert' );
    uploader.on( 'insert', function() {

      data = uploader.state().get( 'selection' ).first().toJSON();

      this.$('a.csg-img-control.set').addClass('hidden');
      this.$('a.csg-img-control.remove').removeClass('hidden');

      this.$('.csg-image-uploader').css({ 'background-image': 'url('+ data.url +')' });
      this.$('input').val(data.url).change();

    }.bind( this ) );

    uploader.open();

  },

  removeImage: function(e) {

    if (e) e.preventDefault();

    this.$('a.csg-img-control.set').removeClass('hidden');
    this.$('a.csg-img-control.remove').addClass('hidden');

    this.$('.csg-image-uploader').css({ 'background-image': 'none' });
    this.$('input').val('');
  }
},{
  // Static methods

  uploader: null,

  createMediaFrame: function() {

    if ( this.uploader ==  null) {
      this.uploader = wp.media({ frame:    'post', state:    'insert', multiple: false });
    }

  }

});

module.exports = ControlImageUploadView;
},{"./base":17}],22:[function(require,module,exports){
var ControlView = require('./base');
module.exports = ControlView.extend({ template: csg.template('controls/text-area') });
},{"./base":17}],23:[function(require,module,exports){
var ControlView = require('./base');
module.exports = ControlView.extend();
},{"./base":17}],24:[function(require,module,exports){
var NavView = require('./nav/main');
var WindowView = require('./window');
var ControlCollection = require('../models/control-collection');

module.exports = Backbone.View.extend({

  id: "csgModal",
  className: "csg",

  template: csg.template('modal'),

  events: {
    "click .csg-modal-close"           : "close" ,
    "click .csg-modal-toggle-advanced" : "toggleAdvanced" ,
    "click #btn-ok"                    : "insertShortcode" ,
  },

  controls: null,

  initialize: function( options ) {
    this.controller = options.controller;

    this.listenTo(this.collection, 'change:completed', this.render);
    this.listenTo(this.collection, 'reset', this.render);
    this.listenTo( this.collection, 'new_selection', this.setupControls );
    this.on('controls_ready', this.renderWindow);
    this.collection.fetch({reset: true});

    this.render();

  },

  render: function() {
    this.$el.html( this.template() );

    this.$('.csg-modal-sidebar').append( new NavView({ collection: this.collection }).render().$el );

    this.renderWindow();

    if (this.getAdvancedState()) {
      this.setAdvancedState( true );
    }
    jQuery( "body" ).append( this.$el ).css( { "overflow" : "hidden" } );
    this.$el.focus();

    jQuery( document ).on( "focusin" , function( e ){
      if ( this.$el[0] !== e.target && !this.$el.has( e.target ).length ) {
        this.$el.focus();
      }
    }.bind(this) );


  },

  setupControls: function() {

    this.controls = new ControlCollection( this.collection.selected.get( 'params' ) );
    this.trigger( 'controls_ready' );
  },

  renderWindow: function() {

    if(this.collection.selected !== null) {
      this.$('#btn-ok').prop( 'disabled',false );
    }

    this.$('.csg-modal-main').html( new WindowView({ collection: this.controls, shortcode: this.collection.selected }).render().$el );
  },

  close: function( e ) {

    if (e) e.preventDefault();

    this.collection.selected = null;
    this.undelegateEvents();

    jQuery( document ).off( "focusin", this.preserveFocus );
    jQuery( "body" ).css( { "overflow" : "auto" } );

    this.remove();

    this.controller.deleteModal();
  },

  toggleAdvanced: function( e ){

    if (e) e.preventDefault();
    this.setAdvancedState( !this.getAdvancedState());
  },

  getAdvancedState: function() {

    if(typeof(Storage) !== "undefined") {
      if (window.localStorage['csg-advanced-mode'] === undefined) {
        window.localStorage['csg-advanced-mode'] = false;
      }
      return (window.localStorage['csg-advanced-mode'] == "true" );

    }

    return (this.$el.hasClass('csg-advanced-enabled'));

  },

  setAdvancedState: function( state ) {

    this.$('.csg-modal-toggle-advanced').removeClass('active');
    this.$el.removeClass('csg-advanced-enabled');

    if (state) {
      this.$('.csg-modal-toggle-advanced').addClass('active');
      this.$el.addClass('csg-advanced-enabled');
    }

    window.localStorage['csg-advanced-mode'] = (state);

  },

  insertShortcode: function(){

    //
    // Summarize set controls
    //

    var atts = {};
    var content = '';
    var closeShortcode = this.collection.selected.get( 'container' ) || false;

    this.controls.each(function(control){

      var data = control.get('data'),
          name = control.get('param_name');

      if ( name == 'content' ) {
        closeShortcode = true;
      }

      if ( data !== undefined && data != '' ) {

        if ( name == 'content' ) {
          content = data;
          return;
        }

        atts[name] = data;

      }
    });

    //
    // Parse into shortcode syntax
    //

    var tag = this.collection.selected.get( 'id' );

    var output = '[' + tag;

    _( atts ).each(function(value, name){
      output += ' '+name + '="' + value +'"';
    });

    output += ']';

    if (content) output += content;

    if ( closeShortcode ) {
      output += '[/' + tag + ']';
    }

    console.log( "Inserting Shortcode: " + output );
    window.wp.media.editor.insert( output );
    this.close();
  }

});
},{"../models/control-collection":2,"./nav/main":26,"./window":29}],25:[function(require,module,exports){
module.exports = Backbone.View.extend({

  tagName: "li",
  events: {
    "click" : "click"
  },

  className: "csg-nav-item",

  render: function() {
    this.$el.html( jQuery('<a href="#">' + this.model.get('title') + '</a>') );
    return this;
  },

  click: function() { this.model.setSelected(); } //this.$el.addClass( 'active' ); }

});
},{}],26:[function(require,module,exports){
var NavSectionView = require('./section');

module.exports = Backbone.View.extend({

  className: "csg-navigation",

  events: {
    "click li.csg-nav-item a" : "click"
  },

  render: function(){
    _(window.csgData.sectionNames).each(function(sectionName){

      this.$el.append('<h3>' + sectionName + '</h3>');
      this.$el.append( new NavSectionView( { collection: this.collection.section( sectionName ) } ).render().$el );

    }.bind(this));

    this.$el.accordion({ heightStyle: "content" });
    //this.$('.ui-accordion-header').hoverIntent( function() { $(this).click(); });

    return this;

  },

  click: function( e ) {
    this.$('li.csg-nav-item a').removeClass( 'active' );
    this.$(e.target).addClass('active');
  }

});
},{"./section":27}],27:[function(require,module,exports){
var NavItemView = require('./item');
module.exports = Backbone.View.extend({

  className: "csg-nav-section",

  render: function(){
      this.$el.append(jQuery('<ul></ul>'));
      this.collection.each( function( shortcode ) {
        this.$('ul').append( new NavItemView({model: shortcode}).render().$el );
      }, this );
      return this;
  },

});
},{"./item":25}],28:[function(require,module,exports){
module.exports = Backbone.View.extend({

  className: "csg-preview",
  template: csg.template('preview'),

  render: function(){
    this.$el.html( this.template( _.extend( this.model.toJSON(), this.data ) ) );
    return this;
  }
});
},{}],29:[function(require,module,exports){
var ControlGroupView = require('./control-group');
var PreviewView = require('./preview');

module.exports = Backbone.View.extend({

  className: "csg-modal-window",
  template: csg.template('blank-state'),

  initialize: function( options ) {
    this.shortcode = options.shortcode;
  },

  render: function() {

    if (this.shortcode == null) {
      this.$el.html( this.template() );
      return this;
    }

    this.$el.append( new ControlGroupView({collection: this.collection }).render().$el );
    this.$el.append( new PreviewView( { model: this.shortcode } ).render().$el );

    return this;

  }
});
},{"./control-group":16,"./preview":28}]},{},[1])(1)
});
//# sourceMappingURL=generator.map
