import { Component, ElementRef } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <div class="container">
      <div class="page-header">
        <h1>Modals</h1>
      </div>

      <button class="btn btn-default" id="btn-toggle-modal">Launch demo modal</button>
      <button class="btn btn-primary" (click)="toggleModalTitle()">toggle modal title</button>
      <button class="btn btn-success" (click)="toggleModalBody()">toggle modal body</button>
      <button class="btn btn-info" (click)="toggleModalFooter()">toggle modal footer</button>
      <button class="btn btn-warning" (click)="toggleModalContent()"> toggle modal content</button>
      <pf-modal targetSelector="#btn-toggle-modal" backdrop keyboard>
        <pf-modal-dialog>
          <pf-modal-content *ngIf="showFormModal">
            <pf-modal-header [modalTitle]="showTitle1 ? title1 : title2"></pf-modal-header>
            <pf-modal-body>
              <form class="form-horizontal" *ngIf="showForm">
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="textInput">Field One</label>
                  <div class="col-sm-9">
                    <input type="text" id="textInput" class="form-control"/>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="textInput2">Field Two</label>
                  <div class="col-sm-9">
                    <input type="text" id="textInput2" class="form-control"/>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="textInput3">Field Three</label>
                  <div class="col-sm-9">
                    <input type="text" id="textInput3" class="form-control"/>
                  </div>
                </div>
              </form>
              <h1 *ngIf="!showForm">That's all. Thanks.</h1>
            </pf-modal-body>
            <pf-modal-footer [cancelCaption]="showBtns ? 'Cancel' : ''" [saveCaption]="showBtns ? 'Save' : ''">
              <p *ngIf="!showBtns">Made with<span class="love">♥</span> by the <a href="http://patternfly.org">Patternfly</a> team.</p>
            </pf-modal-footer>
          </pf-modal-content>
          <pf-modal-content *ngIf="!showFormModal">
            <pf-modal-header [modalTitle]="!showTitle1 ? title1 : title2"></pf-modal-header>
            <pf-modal-body>
              <form class="form-horizontal" *ngIf="!showForm">
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="textInput">Field One</label>
                  <div class="col-sm-9">
                    <input type="text" id="textInput" class="form-control"/>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="textInput2">Field Two</label>
                  <div class="col-sm-9">
                    <input type="text" id="textInput2" class="form-control"/>
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-3 control-label" for="textInput3">Field Three</label>
                  <div class="col-sm-9">
                    <input type="text" id="textInput3" class="form-control"/>
                  </div>
                </div>
              </form>
              <h1 *ngIf="showForm">That's all. Thanks.</h1>
            </pf-modal-body>
            <pf-modal-footer [cancelCaption]="!showBtns ? 'Cancel' : ''" [saveCaption]="!showBtns ? 'Save' : ''">
              <p *ngIf="showBtns">Made with<span class="love">♥</span> by the <a href="http://patternfly.org">Patternfly</a> team.</p>
            </pf-modal-footer>
          </pf-modal-content>
        </pf-modal-dialog>
      </pf-modal>
    </div>
  `
})
export class AppComponent {
  showTitle1 = true;
  title1 = 'Modal Title';
  title2 = 'Welcome to web components world. What a lucky guy!';
  showForm = true;
  showBtns = true;
  showFormModal = true;

  constructor(el: ElementRef) {
    this.self = el.nativeElement;
  }

  toggleModalTitle() {
    this.showTitle1 = !this.showTitle1;
    this.self.querySelector('pf-modal').show();
  }

  toggleModalBody() {
    this.showForm = !this.showForm;
    this.self.querySelector('pf-modal').show();
  }

  toggleModalFooter() {
    this.showBtns = !this.showBtns;
    this.self.querySelector('pf-modal').show();
  }

  toggleModalContent() {
    this.showFormModal = !this.showFormModal;
    this.self.querySelector('pf-modal').show();
  }
}


/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { NgModule, CUSTOM_ELEMENTS_SCHEMA }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
export class AppModule { }


/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);


/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!DOCTYPE html>
<html>
  <head>
    <title>Angular Quickstart</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.rawgit.com/patternfly-webcomponents/patternfly-webcomponents/master-dist/dist/css/patternfly.min.css">
    <link rel="stylesheet" href="https://cdn.rawgit.com/patternfly-webcomponents/patternfly-webcomponents/master-dist/dist/css/patternfly-additions.min.css">
    <link rel="stylesheet" href="https://rawgit.com/dabeng/patternfly-webcomponents/modal-dist/dist/css/patternfly-webcomponents.css">

    <!-- Polyfills -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>
    <script src="https://unpkg.com/zone.js@0.8.4?main=browser"></script>
    <script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
    <script src="systemjs.config.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/custom-elements/1.0.0/custom-elements.min.js"></script>
    <script src="//rawgit.com/webcomponents/custom-elements/master/src/native-shim.js"></script>
    <script src="https://rawgit.com/dabeng/patternfly-webcomponents/modal-dist/dist/js/patternfly.js"></script>
    <script>
      System.import('main.ts').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading AppComponent content here ...</my-app>
  </body>

</html>


<!-- 
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
/**
 * WEB ANGULAR VERSION
 * (based on systemjs.config.js in angular.io)
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    // DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
    transpiler: 'ts',
    typescriptOptions: {
      // Copy of compiler options in standard tsconfig.json
      "target": "es5",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "lib": ["es2015", "dom"],
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    paths: {
      // paths serve as alias
      'npm:': 'https://unpkg.com/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
      '@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',

      // other libraries
      'rxjs':                      'npm:rxjs@5.0.1',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
      'ts':                        'npm:plugin-typescript@5.2.7/lib/plugin.js',
      'typescript':                'npm:typescript@2.2.1/lib/typescript.js',

    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.ts',
        defaultExtension: 'ts',
        meta: {
          './*.ts': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });

})(this);

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;

module.exports.translate = function(load){
  if (load.source.indexOf('moduleId') != -1) return load;

  var url = document.createElement('a');
  url.href = load.address;

  var basePathParts = url.pathname.split('/');

  basePathParts.pop();
  var basePath = basePathParts.join('/');

  var baseHref = document.createElement('a');
  baseHref.href = this.baseURL;
  baseHref = baseHref.pathname;

  if (!baseHref.startsWith('/base/')) { // it is not karma
    basePath = basePath.replace(baseHref, '');
  }

  load.source = load.source
    .replace(templateUrlRegex, function(match, quote, url){
      let resolvedUrl = url;

      if (url.startsWith('.')) {
        resolvedUrl = basePath + url.substr(1);
      }

      return 'templateUrl: "' + resolvedUrl + '"';
    })
    .replace(stylesRegex, function(match, relativeUrls) {
      var urls = [];

      while ((match = stringRegex.exec(relativeUrls)) !== null) {
        if (match[2].startsWith('.')) {
          urls.push('"' + basePath + match[2].substr(1) + '"');
        } else {
          urls.push('"' + match[2] + '"');
        }
      }

      return "styleUrls: [" + urls.join(', ') + "]";
    });

  return load;
};