main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. var nfRadio = Backbone.Radio;
  2. var nfDebug = false;
  3. if( ! useServices ){
  4. /**
  5. * If the feature flag isn't set then filter out the "Services" tab.
  6. */
  7. nfDashItems = nfDashItems.filter(function(item){
  8. return 'services' !== item.slug;
  9. });
  10. }
  11. jQuery( document ).ready( function( $ ) {
  12. require( [ 'controllers/formsController', 'controllers/oauthController', 'controllers/servicesController', 'views/dashboardView' ], function( FormsController, OAuthController, ServicesController, DashboardView ) {
  13. var NinjaFormsDashboard = Marionette.Application.extend( {
  14. // The root element for the application.
  15. region: '#ninja-forms-dashboard',
  16. controllers: {},
  17. initialize: function( options ) {
  18. // Assign this instance to a named variable.
  19. var dashboard = this;
  20. // Override the Renderer for WordPress compatibility.
  21. Marionette.Renderer.render = function(template, data){
  22. var template = dashboard.template( template );
  23. return template( data );
  24. };
  25. /* DEBUG */ if( nfDebug ) console.log( 'Dashboard Initialized' );
  26. },
  27. onStart: function() {
  28. // Show the root view for the application.
  29. this.showView( new DashboardView() );
  30. this.controllers.forms = new FormsController();
  31. if( useServices ) this.controllers.oauth = new OAuthController();
  32. if( useServices ) this.controllers.services = new ServicesController();
  33. //var data = {id: 1, title: 'Contact Me', created: '10-23-2016'};
  34. //var form = new FormModel(data);
  35. // var formCollection = new FormCollection();
  36. // formCollection.fetch( {
  37. // success: function(response) { console.log(response); }
  38. // });
  39. /* DEBUG */ if( nfDebug ) console.log( 'Dashboard Started' );
  40. },
  41. /**
  42. * Custom render function for WordPress style templates.
  43. * @param template
  44. */
  45. template: function( template ) {
  46. return _.template( $( template ).html(), {
  47. evaluate: /<#([\s\S]+?)#>/g,
  48. interpolate: /\{\{\{([\s\S]+?)\}\}\}/g,
  49. escape: /\{\{([^\}]+?)\}\}(?!\})/g,
  50. variable: 'data'
  51. } );
  52. }
  53. } );
  54. var nfDashboard = new NinjaFormsDashboard();
  55. nfDashboard.start();
  56. } );
  57. } );
  58. /**
  59. * Submenu Routing
  60. */
  61. jQuery( 'a[href="admin.php?page=ninja-forms#new-form"]' ).on( 'click', function( event ){
  62. event.preventDefault();
  63. window.location.hash = 'new-form';
  64. nfRadio.channel( 'dashboard' ).request( 'show:widgets' );
  65. nfRadio.channel( 'widget-forms' ).request( 'show:newFormsGrid' );
  66. } );
  67. jQuery( 'a[href="admin.php?page=ninja-forms#apps"]' ).on( 'click', function( event ){
  68. event.preventDefault();
  69. window.location.hash = 'apps';
  70. nfRadio.channel( 'dashboard' ).request( 'show:apps' );
  71. } );
  72. jQuery( 'a[href="admin.php?page=ninja-forms"]' ).on( 'click', function( event ){
  73. event.preventDefault();
  74. window.location.hash = 'forms';
  75. nfRadio.channel( 'dashboard' ).request( 'show:widgets' );
  76. nfRadio.channel( 'widget-forms' ).request( 'show:formsTable' );
  77. } );
  78. /**
  79. * Hash Change Routing Fallback
  80. * To avoid the need to manually add listeners to views, this is a generic hash change listener.
  81. */
  82. jQuery(window).on('hashchange', function() {
  83. var hash = window.location.hash.substr(1);
  84. nfRadio.channel( 'dashboard' ).request( 'show:' + hash );
  85. });