PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/js_composer/assets/js/backend/composer-automapper.js

https://gitlab.com/furrutia1991/imosa_web
JavaScript | 724 lines | 692 code | 14 blank | 18 comment | 58 complexity | dbc8e6c60b5ac13faec76c01403d8b9a MD5 | raw file
  1. /* =========================================================
  2. * composer-automapper.js v0.2.1
  3. * =========================================================
  4. * Copyright 2013 Wpbakery
  5. *
  6. * Visual composer automapper for shortcodes
  7. * ========================================================= */
  8. var vc_am = {
  9. current_form: false
  10. };
  11. (function ( $ ) {
  12. "use strict";
  13. _.extend( wp.shortcode.prototype, {
  14. taggedString: function () {
  15. var text = '[<span class="vc_preview-tag">' + this.tag + '</span>';
  16. _.each( this.attrs.numeric, function ( value ) {
  17. if ( /\s/.test( value ) ) {
  18. text += ' <span class="vc_preview-param">"' + value + '"</span>';
  19. } else {
  20. text += ' <span class="vc_preview-param">' + value + '</span>';
  21. }
  22. } );
  23. _.each( this.attrs.named, function ( value, name ) {
  24. text += ' <span class="vc_preview-param">' + name + '="' + value + '"</span>';
  25. } );
  26. // If the tag is marked as `single` or `self-closing`, close the
  27. // tag and ignore any additional content.
  28. if ( 'single' === this.type ) {
  29. return text + ']';
  30. } else if ( 'self-closing' === this.type ) {
  31. return text + ' /]';
  32. }
  33. // Complete the opening tag.
  34. text += ']';
  35. if ( this.content ) {
  36. text += '<span class="vc_preview-content">' + this.content + '</span>';
  37. }
  38. // Add the closing tag.
  39. return text + '[/<span class="vc_preview-tag">' + this.tag + '</span>]';
  40. }
  41. } );
  42. wp.shortcode.atmPreview = function ( options ) {
  43. return new wp.shortcode( options ).taggedString();
  44. };
  45. function show_message( text, type ) {
  46. if ( message_timer ) {
  47. window.clearTimeout( message_timer );
  48. $( '.vc_settings-automapper' ).remove();
  49. message_timer = false;
  50. }
  51. var $message = $( '<div class="vc_atm-message updated' + (type ? ' vc_message-' + type : '') + '" style="display: none;"></div>' );
  52. $message.text( text );
  53. $message.prependTo( $( '#vc_settings-automapper' ) ).fadeIn( 500, function () {
  54. var $message = $( this );
  55. window.setTimeout( function () {
  56. $message.remove();
  57. }, 2000 );
  58. } );
  59. }
  60. var message_timer,
  61. template_options = {
  62. evaluate: /<#([\s\S]+?)#>/g,
  63. interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
  64. escape: /\{\{([^\}]+?)\}\}(?!\})/g
  65. },
  66. to_title = function ( string ) {
  67. string = string.replace( /_|-/, ' ' );
  68. return string.charAt( 0 ).toUpperCase() + string.slice( 1 );
  69. };
  70. function showMessageMore( text, typeClass, timeout, remove ) {
  71. if ( remove ) {
  72. $( '.vc_atm-message' ).remove();
  73. }
  74. var $message = $( '<div class="vc_atm-message ' + (typeClass ? typeClass : '') + '" style="display: none;"></div>' );
  75. $message.text( text );
  76. if ( ! _.isUndefined( timeout ) ) {
  77. window.setTimeout( function () {
  78. $message.remove();
  79. }, timeout );
  80. }
  81. return $message;
  82. }
  83. function showValidationError( text, $el ) {
  84. if ( _.isUndefined( $el ) || ! $el.length ) {
  85. $el = $( '.tab_intro' );
  86. }
  87. showMessageMore( text, 'error', undefined, true ).insertBefore( $el ).fadeIn( 500 );
  88. }
  89. var request_url = window.ajaxurl + '?vc_action=automapper',
  90. sync_callback = function ( method, model, options ) {
  91. var data;
  92. if ( 'create' === method ) {
  93. model.set( 'id', window.vc_guid() );
  94. data = {
  95. vc_action: 'create',
  96. data: model.toJSON()
  97. };
  98. } else if ( 'update' === method ) {
  99. data = {
  100. vc_action: 'update',
  101. id: model.get( 'id' ),
  102. data: model.toJSON()
  103. };
  104. } else if ( 'delete' === method ) {
  105. data = {
  106. vc_action: 'delete',
  107. id: model.get( 'id' )
  108. };
  109. } else {
  110. data = {
  111. vc_action: 'read'
  112. };
  113. }
  114. $.ajax( {
  115. method: 'POST',
  116. url: request_url,
  117. dataType: 'json',
  118. data: _.extend( data, { action: 'vc_automapper' } ),
  119. context: this
  120. } ).done( function ( data ) {
  121. var result = model;
  122. if ( data && 'read' === method ) {
  123. result = data;
  124. }
  125. // Response
  126. if ( result ) {
  127. ('read' === method) && options.success( result );
  128. } else {
  129. options.error( "Not found" );
  130. }
  131. } ).error( function ( data ) {
  132. } );
  133. };
  134. var ShortcodeModel = Backbone.Model.extend( {
  135. defaults: function () {
  136. return {
  137. tag: '',
  138. name: '',
  139. category: '',
  140. description: '',
  141. params: []
  142. };
  143. },
  144. sync: sync_callback
  145. } );
  146. var ShortcodesCollection = Backbone.Collection.extend( {
  147. model: ShortcodeModel,
  148. sync: sync_callback
  149. } );
  150. vc_am.shortcodes = new ShortcodesCollection;
  151. var ShortcodeView = Backbone.View.extend( {
  152. tagName: 'li',
  153. // className: 'vc_automapper-item',
  154. className: 'widget',
  155. events: {
  156. 'click .vc_automapper-edit-btn': 'edit',
  157. 'click h4, widget-action': 'edit',
  158. 'click .vc_automapper-delete-btn': 'clear'
  159. },
  160. template_html: $( '#vc_automapper-item-tpl' ).html() || '<span>{{ tag }}</span>',
  161. initialize: function () {
  162. this.listenTo( this.model, 'change', this.render );
  163. this.listenTo( this.model, 'destroy', this.removeView )
  164. },
  165. render: function () {
  166. this.$el.html( _.template( this.template_html,
  167. this.model.toJSON(),
  168. template_options ) ).attr( 'data-item-id', this.model.get( 'id' ) );
  169. return this;
  170. },
  171. edit: function ( e ) {
  172. e && e.preventDefault();
  173. new EditFormView( { model: this.model } ).render();
  174. },
  175. clear: function ( e ) {
  176. e && e.preventDefault();
  177. if ( confirm( window.i18nLocaleVcAutomapper.are_you_sure_delete ) ) {
  178. this.model.destroy();
  179. }
  180. },
  181. removeView: function () {
  182. this.$el.remove();
  183. }
  184. } );
  185. var FormView = Backbone.View.extend( {
  186. render: function () {
  187. vc_am.current_form && vc_am.current_form.close();
  188. vc_am.current_form = this;
  189. return this;
  190. },
  191. getType: function () {
  192. return 'form';
  193. },
  194. validate: function ( attrs ) {
  195. var result = false;
  196. if ( ! attrs.name ) {
  197. return window.i18nLocaleVcAutomapper.error_shortcode_name_is_required;
  198. }
  199. if ( ! attrs.tag || ! attrs.tag.match( /^\S+$/ ) ) {
  200. return window.i18nLocaleVcAutomapper.error_enter_valid_shortcode_tag;
  201. }
  202. var fields_required = [
  203. 'param_name',
  204. 'heading',
  205. 'type'
  206. ];
  207. _.each( attrs.params, function ( param ) {
  208. _.each( fields_required, function ( field ) {
  209. if ( '' === param[ field ] ) {
  210. result = window.i18nLocaleVcAutomapper.error_enter_required_fields // '';
  211. } else if ( 'param_name' === field && ! param[ field ].match( /^[a-z0-9_]+$/g ) ) {
  212. result = window.i18nLocaleVcAutomapper.error_enter_required_fields;
  213. }
  214. }, this );
  215. }, this );
  216. return result || null;
  217. },
  218. isValid: function ( data ) {
  219. this.validationError = this.validate( data );
  220. return this.validationError ? false : true;
  221. },
  222. close: function ( e ) {
  223. e && e.preventDefault();
  224. vc_am.current_form = false;
  225. this.remove();
  226. }
  227. } );
  228. var ComplexShortcodeView = Backbone.View.extend( {
  229. _$widget_title: false,
  230. _$form_view: false,
  231. edit_view: false,
  232. tagName: 'li',
  233. className: 'widget',
  234. events: {
  235. 'click .vc_automapper-edit-btn': 'edit',
  236. 'click h4, .widget-action': 'edit'
  237. },
  238. template_html: $( '#vc_automapper-item-complex-tpl' ).html() || '<span>{{ tag }}</span>',
  239. header_template_html: '<h4>{{ name }}<span class="in-widget-title"></span></h4>',
  240. initialize: function () {
  241. _.bindAll( this, 'removeEditForm' );
  242. this.listenTo( this.model, 'destroy', this.removeView );
  243. this.model.view = this;
  244. },
  245. render: function () {
  246. this.$el.html( _.template( this.template_html,
  247. this.model.toJSON(),
  248. template_options ) ).attr( 'data-item-id', this.model.get( 'id' ) );
  249. return this;
  250. },
  251. renderTitle: function () {
  252. this.$widgetTitle().html( _.template( this.header_template_html, this.model.toJSON(), template_options ) );
  253. },
  254. edit: function ( e ) {
  255. e && e.preventDefault();
  256. if ( this.$editForm().is( ':animated' ) ) {
  257. return false;
  258. }
  259. this.$el.addClass( 'vc_opened' );
  260. if ( this.edit_view ) {
  261. this.close();
  262. } else {
  263. this.edit_view = new EditFormInnerView( { model: this.model } ).render();
  264. }
  265. },
  266. $widgetTitle: function () {
  267. if ( ! this._$widget_title ) {
  268. this._$widget_title = this.$el.find( '.widget-title' );
  269. }
  270. return this._$widget_title;
  271. },
  272. $editForm: function () {
  273. if ( ! this._$edit_form ) {
  274. this._$edit_form = this.$el.find( '.widget-inside' );
  275. }
  276. return this._$edit_form;
  277. },
  278. removeEditForm: function () {
  279. this.edit_view && this.edit_view.remove();
  280. this.edit_view = false;
  281. },
  282. beforeSave: function () {
  283. this.$el.find( '#vc_atm-name' ).val( $( '#vc_atm-header-name' ).val() );
  284. },
  285. close: function () {
  286. vc_am.current_form = false;
  287. this.$el.removeClass( 'vc_opened' );
  288. this.renderTitle();
  289. this.$editForm().slideUp( 200 );
  290. this.removeEditForm(); // delayed call causes bug with empty params(remove called with delay)
  291. },
  292. clear: function ( e ) {
  293. e && e.preventDefault();
  294. this.model.destroy();
  295. },
  296. removeView: function () {
  297. this.remove();
  298. }
  299. } );
  300. var AddFormView = FormView.extend( {
  301. className: 'vc_add-form-atm',
  302. template_html: $( '#vc_automapper-add-form-tpl' ).html(),
  303. events: {
  304. 'click #vc_atm-parse-string': 'parseShortcode',
  305. 'click .vc_atm-cancel': 'close'
  306. },
  307. getType: function () {
  308. return 'create';
  309. },
  310. render: function () {
  311. AddFormView.__super__.render.call( this );
  312. this.$el.html( _.template( this.template_html, {}, template_options ) );
  313. this.$el.insertAfter( '.vc_automapper-toolbar' );
  314. return this;
  315. },
  316. parseShortcode: function ( e ) {
  317. var string, matches, data, params = [], attr;
  318. e && e.preventDefault && e.preventDefault();
  319. string = $( '#vc_atm-shortcode-string' ).val();
  320. if ( _.isEmpty( string ) ) {
  321. this.$el.addClass( 'form-invalid' );
  322. showValidationError( window.i18nLocaleVcAutomapper.error_enter_valid_shortcode, this.$el );
  323. return false;
  324. }
  325. matches = string.match( vc_regexp_shortcode() );
  326. if ( ! matches ) {
  327. this.$el.addClass( 'form-invalid' );
  328. showValidationError( window.i18nLocaleVcAutomapper.error_enter_valid_shortcode, this.$el );
  329. return false;
  330. }
  331. attr = wp.shortcode.attrs( matches[ 3 ] );
  332. _.each( attr.named, function ( value, key ) {
  333. params.push( {
  334. param_name: key,
  335. type: "textfield",
  336. heading: to_title( key ),
  337. description: 'Example: ' + value,
  338. value: value
  339. } );
  340. }, this );
  341. if ( matches[ 5 ] ) {
  342. params.push( {
  343. param_name: 'content',
  344. type: "textarea",
  345. heading: 'Content',
  346. description: '',
  347. value: matches[ 5 ]
  348. } );
  349. }
  350. data = {
  351. tag: matches[ 2 ],
  352. name: to_title( matches[ 2 ] ),
  353. category: window.i18nLocaleVcAutomapper.my_shortcodes_category,
  354. params: params
  355. };
  356. if ( this.isValid( data ) ) {
  357. vc_am.shortcodes.create( data );
  358. show_message( window.i18nLocaleVcAutomapper.new_shortcode_mapped, 'success' );
  359. $( '.vc_atm-message' ).remove();
  360. vc_am.shortcodes.last().view.edit();
  361. } else {
  362. this.$el.addClass( 'form-invalid' );
  363. showValidationError( this.validationError );
  364. }
  365. }
  366. } );
  367. var EditFormView = FormView.extend( {
  368. className: 'vc_edit-form',
  369. active_preview: false,
  370. events: {
  371. 'click #vc_atm-save': 'save',
  372. 'click .vc_atm-cancel': 'close',
  373. 'click .vc_atm-delete': 'clear',
  374. 'click #vc_atm-add-param': 'addParam',
  375. 'click .vc_delete-param': 'deleteParam',
  376. 'change #vc_atm-is-container': 'setContentParam',
  377. 'keyup .vc_param-name, .vc_param-value, #vc_atm-tag': 'setPreview',
  378. 'focus #vc_atm-tag': 'setTagFieldActive',
  379. 'focus .vc_params input, .vc_params textarea': 'setParamFieldActive',
  380. 'focus .vc_param.vc_content input, .vc_param.vc_content textarea': 'setContentParamFieldActive',
  381. 'blur #vc_atm-tag, vc_param input': 'unsetFieldActive',
  382. 'change .vc_param-field [name="type"]': 'changeParamType'
  383. },
  384. new: false,
  385. template_html: $( '#vc_automapper-form-tpl' ).html(),
  386. param_template_html: $( '#vc_atm-form-param-tpl' ).html(),
  387. getType: function () {
  388. return 'edit';
  389. },
  390. render: function () {
  391. EditFormView.__super__.render.call( this );
  392. this.$el.html( _.template( this.template_html, this.model.toJSON(), template_options ) );
  393. this.$el.insertAfter( $( '[data-item-id=' + this.model.id + ']' ).hide() );
  394. this.addAllParams();
  395. return this;
  396. },
  397. changeParamType: function ( e ) {
  398. var $this, $parent;
  399. $this = $( e.currentTarget );
  400. $parent = $this.parents( '.vc_fields' );
  401. if ( 'hidden' === $this.val() ) {
  402. $parent.find( '[name="heading"]' ).attr( 'disabled', true );
  403. $parent.find( '[name="description"]' ).attr( 'disabled', true );
  404. } else {
  405. $parent.find( '[name="heading"]' ).attr( 'disabled', false );
  406. $parent.find( '[name="description"]' ).attr( 'disabled', false );
  407. }
  408. },
  409. setTagFieldActive: function ( e ) {
  410. this.active_preview && $( this.active_preview ).removeClass( 'vc_active' );
  411. this.active_preview = '#vc_shortcode-preview .vc_preview-tag';
  412. $( this.active_preview ).addClass( 'vc_active' );
  413. },
  414. setParamFieldActive: function ( e ) {
  415. var $control = $( e.currentTarget ),
  416. index = $control.parents( '.vc_param:first' ).index();
  417. this.active_preview && $( this.active_preview ).removeClass( 'vc_active' );
  418. this.active_preview = '#vc_shortcode-preview .vc_preview-param:eq(' + index + ')';
  419. $( this.active_preview ).addClass( 'vc_active' );
  420. },
  421. setContentParamFieldActive: function ( e ) {
  422. this.active_preview && $( this.active_preview ).removeClass( 'vc_active' );
  423. this.active_preview = '#vc_shortcode-preview .vc_preview-content';
  424. $( this.active_preview ).addClass( 'vc_active' );
  425. },
  426. unsetFieldActive: function ( e ) {
  427. $( this.active_preview ).removeClass( 'vc_active' );
  428. this.active_preview = false;
  429. },
  430. /***
  431. * Escape double quotes in params value.
  432. * @param value
  433. * @return {*}
  434. */
  435. escapeParam: function ( value ) {
  436. return value && value.replace( /"/g, '``' );
  437. },
  438. getPreview: function ( data ) {
  439. var params = data.params,
  440. content = false,
  441. params_to_string = {};
  442. _.each( params, function ( value, key ) {
  443. if ( 'content' !== value.param_name ) {
  444. params_to_string[ value.param_name ] = this.escapeParam( value.value );
  445. } else {
  446. content = value.value;
  447. }
  448. }, this );
  449. return wp.shortcode.atmPreview( {
  450. tag: data.tag,
  451. attrs: params_to_string,
  452. content: content,
  453. type: false === content ? 'single' : ''
  454. } );
  455. },
  456. setPreview: function () {
  457. var data = {
  458. params: this.getParams(),
  459. tag: $( '#vc_atm-tag' ).val()
  460. };
  461. $( '#vc_shortcode-preview' ).html( this.getPreview( data ) );
  462. this.active_preview && $( this.active_preview ).addClass( 'vc_active' );
  463. },
  464. save: function ( e ) {
  465. e && e.preventDefault && e.preventDefault();
  466. this.$el.find( '.vc_error' ).removeClass( 'vc_error' );
  467. var data = {
  468. tag: $( '#vc_atm-tag' ).val(),
  469. name: $( '#vc_atm-name' ).val(),
  470. category: $( '#vc_atm-category' ).val(),
  471. description: $( '#vc_atm-description' ).val(),
  472. params: this.getParams()
  473. };
  474. if ( this.isValid( data ) ) {
  475. this.model.save( data );
  476. show_message( window.i18nLocaleVcAutomapper.shortcode_updated, 'success' );
  477. this.close();
  478. } else {
  479. showValidationError( this.validationError, this.$el.find( '#vc_atm-save' ) );
  480. }
  481. },
  482. validate: function ( attrs ) {
  483. $( '.vc_error,.form-invalid' ).removeClass( 'vc_error form-invalid' );
  484. var result = false, added_param_names = {};
  485. if ( ! attrs.name ) {
  486. $( '#vc_atm-name' ).addClass( 'vc_error' );
  487. $( '#vc_atm-header-name' ).parent().addClass( 'form-invalid' );
  488. return window.i18nLocaleVcAutomapper.error_shortcode_name_is_required;
  489. }
  490. if ( ! attrs.tag || ! attrs.tag.match( /^\S+$/ ) ) {
  491. $( '#vc_atm-tag' ).addClass( 'vc_error' ).parent().addClass( 'form-invalid' );
  492. return window.i18nLocaleVcAutomapper.error_enter_valid_shortcode_tag;
  493. }
  494. var fields_required = [
  495. 'param_name',
  496. 'heading',
  497. 'type'
  498. ];
  499. _.each( attrs.params, function ( param, index ) {
  500. var $field_el = $( '#vc_atm-params-list [name=param_name]:eq(' + index + ')' );
  501. if ( 'content' === param.param_name && ! $field_el.data( 'system' ) ) {
  502. result = window.i18nLocaleVcAutomapper.error_content_param_not_manually;
  503. $field_el.addClass( 'vc_error' );
  504. $field_el.closest( '.vc_param-field' ).addClass( 'form-invalid' );
  505. return;
  506. }
  507. if ( _.isBoolean( added_param_names[ param.param_name ] ) && true == added_param_names[ param.param_name ] ) {
  508. $field_el.addClass( 'vc_error' );
  509. $field_el.closest( '.vc_param-field' ).addClass( 'form-invalid' );
  510. if ( ! result ) {
  511. result = window.i18nLocaleVcAutomapper.error_param_already_exists.replace( /\%s/,
  512. param.param_name );
  513. }
  514. }
  515. added_param_names[ param.param_name ] = true;
  516. _.each( fields_required, function ( field ) {
  517. if ( ('hidden' !== param.type && '' === param[ field ]) || ('hidden' === param.type && 'heading' !== field && '' === param[ field ]) ) {
  518. $( '#vc_atm-params-list [name=' + field + ']:eq(' + index + ')' )
  519. .addClass( 'vc_error' )
  520. .closest( '.vc_param-field' )
  521. .addClass( 'form-invalid' );
  522. if ( ! result ) {
  523. result = window.i18nLocaleVcAutomapper.error_enter_required_fields;
  524. }
  525. } else if ( 'param_name' === field && ! param[ field ].match( /^[a-z0-9_]+$/g ) ) {
  526. $field_el
  527. .addClass( 'vc_error' )
  528. .closest( '.vc_param-field' )
  529. .addClass( 'form-invalid' );
  530. if ( ! result ) {
  531. result = window.i18nLocaleVcAutomapper.error_wrong_param_name;
  532. }
  533. }
  534. }, this );
  535. }, this );
  536. return result || null;
  537. },
  538. setContentParam: function ( e ) {
  539. var $control = $( e.currentTarget );
  540. if ( $control[0].checked ) {
  541. this.addParamField( {
  542. type: 'textarea',
  543. heading: 'Content',
  544. description: '',
  545. param_name: 'content',
  546. value: ''
  547. } );
  548. this.setParamSorting();
  549. } else {
  550. this.removeParamField( 'content' );
  551. }
  552. this.setPreview();
  553. },
  554. addAllParams: function () {
  555. $( '#vc_atm-params-list' ).empty();
  556. _.each( this.model.get( 'params' ), function ( param ) {
  557. this.addParamField( param );
  558. if ( 'content' === param.param_name ) {
  559. $( '#vc_atm-is-container' ).prop( 'checked', true );
  560. }
  561. }, this );
  562. this.setParamSorting();
  563. },
  564. getParams: function () {
  565. var params = [];
  566. _.each( $( '.vc_param' ), function ( param ) {
  567. var $param = $( param );
  568. params.push( {
  569. param_name: $param.find( '[name=param_name]' ).val(),
  570. type: $param.find( '[name=type]' ).val(),
  571. description: $param.find( '[name=description]' ).val(),
  572. heading: $param.find( '[name=heading]' ).val(),
  573. value: $param.find( '[name=value]' ).val()
  574. } );
  575. }, this );
  576. return params;
  577. },
  578. addParam: function ( e ) {
  579. e && e.preventDefault();
  580. this.addParamField( { type: '', heading: '', description: '', param_name: '', value: '' } );
  581. this.setPreview();
  582. },
  583. removeParamField: function ( name ) {
  584. $( '.vc_param-name[value="' + name + '"]' ).parents( '.vc_param' ).remove();
  585. },
  586. addParamField: function ( attr ) {
  587. var $block = $( '<div class="vc_param wpb_vc_row' + ( 'content' === attr.param_name ? ' vc_content' : '' ) + '"/>' ).appendTo( '#vc_atm-params-list' );
  588. $block.html( _.template( this.param_template_html, attr, template_options ) );
  589. },
  590. setParamSorting: function () {
  591. $( '#vc_atm-params-list' ).sortable( {
  592. items: '> .vc_param',
  593. tolerance: "pointer",
  594. handle: '.vc_move-param',
  595. update: this.setPreview,
  596. placeholder: "vc_sortable-placeholder"
  597. } );
  598. },
  599. deleteParam: function ( e ) {
  600. var $control;
  601. e && e.preventDefault();
  602. if ( confirm( window.i18nLocaleVcAutomapper.are_you_sure_delete_param ) ) {
  603. $control = $( e.currentTarget );
  604. $control.parents( '.vc_param' ).remove();
  605. this.setPreview();
  606. }
  607. },
  608. close: function ( e ) {
  609. e && e.preventDefault();
  610. this.model && $( '[data-item-id=' + this.model.get( 'id' ) + ']' ).show();
  611. vc_am.current_form = false;
  612. $( '.vc_atm-message' ).remove();
  613. this.remove();
  614. },
  615. clear: function ( e ) {
  616. e && e.preventDefault();
  617. if ( confirm( window.i18nLocaleVcAutomapper.are_you_sure_delete ) ) {
  618. this.model.destroy();
  619. this.close();
  620. }
  621. }
  622. } );
  623. var EditFormInnerView = EditFormView.extend( {
  624. template_html: $( '#vc_automapper-form-tpl' ).html(),
  625. getType: function () {
  626. return 'edit';
  627. },
  628. initialize: function () {
  629. _.bindAll( this, 'setPreview' );
  630. },
  631. render: function () {
  632. var params,
  633. parent = this.model.view;
  634. params = this.model.get( 'params' );
  635. EditFormView.__super__.render.call( this );
  636. this.$el.html( _.template( this.template_html,
  637. _.extend( { shortcode_preview: this.getPreview( this.model.toJSON() ) }, this.model.toJSON() ),
  638. template_options ) );
  639. this.$el.appendTo( parent.$editForm() );
  640. parent.$widgetTitle().html( '<span class="vc_atm-header"><input type="text" name="name" value="" id="vc_atm-header-name" class="vc_header-name"></span><span class="in-widget-title"></span>' );
  641. $( '#vc_atm-header-name' ).val( this.model.get( 'name' ) );
  642. this.addAllParams();
  643. parent.$editForm().slideDown();
  644. return this;
  645. },
  646. save: function ( e ) {
  647. e && e.preventDefault();
  648. this.model.view.beforeSave();
  649. EditFormInnerView.__super__.save.call( this );
  650. },
  651. close: function ( e ) {
  652. e && e.preventDefault();
  653. vc_am.current_form = false;
  654. this.model.view.close();
  655. },
  656. clear: function ( e ) {
  657. e && e.preventDefault();
  658. if ( confirm( window.i18nLocaleVcAutomapper.are_you_sure_delete ) ) {
  659. this.model.view.clear();
  660. this.remove();
  661. }
  662. }
  663. } );
  664. var AppView = Backbone.View.extend( {
  665. events: {
  666. 'click #vc_automapper-add-btn': 'create',
  667. 'submit': 'formSubmit'
  668. },
  669. className: 'vc_atm-form',
  670. addFormView: false,
  671. initialize: function () {
  672. this.listenTo( vc_am.shortcodes, 'add', this.addOne );
  673. this.listenTo( vc_am.shortcodes, 'reset', this.addAll );
  674. this.listenTo( vc_am.shortcodes, 'all', this.render );
  675. this.$list = $( '.vc_automapper-list' );
  676. vc_am.shortcodes.fetch();
  677. },
  678. formSubmit: function ( e ) {
  679. e && e.preventDefault && e.preventDefault();
  680. if ( _.isObject( e ) && this.addFormView && ! _.isEmpty( e.currentTarget ) && ! _.isEmpty( e.currentTarget[ 0 ] ) ) {
  681. var node, $el;
  682. node = e.currentTarget[ 0 ];
  683. $el = $( node );
  684. if ( $el.is( '#vc_atm-shortcode-string' ) ) {
  685. this.addFormView.parseShortcode();
  686. }
  687. }
  688. },
  689. addAll: function ( models ) {
  690. models.each( function ( model ) {
  691. this.addOne( model );
  692. }, this );
  693. },
  694. addOne: function ( model ) {
  695. var view = new ComplexShortcodeView( { model: model } );
  696. this.$list.append( view.render().el );
  697. },
  698. create: function ( e ) {
  699. e && e.preventDefault && e.preventDefault();
  700. if ( ! vc_am.current_form || 'create' !== vc_am.current_form.getType() ) {
  701. this.addFormView = new AddFormView().render();
  702. }
  703. },
  704. render: function () {
  705. }
  706. } );
  707. new AppView( { el: $( '#vc_settings-automapper' ) } );
  708. })( window.jQuery );