Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //  main.js
  2.  
  3. import environment from './environment';
  4.  
  5. export function configure(aurelia) {
  6.   aurelia.use
  7.     .standardConfiguration()
  8.     .feature('resources')
  9.     .feature('validation');
  10.  
  11.   if (environment.debug) {
  12.     aurelia.use
  13.       .developmentLogging();
  14.   }
  15.  
  16.   if (environment.testing) {
  17.     aurelia.use
  18.       .plugin('aurelia-testing');
  19.   }
  20.  
  21.   aurelia.start()
  22.     .then(() => aurelia.setRoot());
  23. }
  24.  
  25. //  validation/index.js
  26.  
  27. export function configure(config) {
  28.   config
  29.     .plugin('aurelia-validation');
  30. }
  31.  
  32. //  contact-edition.html
  33.  
  34. <template>
  35.   <section class="container">
  36.  
  37.     <h1 if.bind="isNew">New contact</h1>
  38.     <h1 if.bind="!isNew">Contact #${contact.id}</h1>
  39.  
  40.     <form class="form-horizontal" submit.delegate="save()">
  41.  
  42.       <div class="form-group">
  43.         <label class="col-sm-3 control-label">First name</label>
  44.         <div class="col-sm-9">
  45.           <input type="text" class="form-control" value.bind="contact.firstName & validate">
  46.         </div>
  47.       </div>
  48.  
  49.       <div class="form-group">
  50.         <label class="col-sm-3 control-label">Last name</label>
  51.         <div class="col-sm-9">
  52.           <input type="text" class="form-control" value.bind="contact.lastName">
  53.         </div>
  54.       </div>
  55.  
  56.       <div class="form-group">
  57.         <label class="col-sm-3 control-label">Company</label>
  58.         <div class="col-sm-9">
  59.           <input type="text" class="form-control" value.bind="contact.company">
  60.         </div>
  61.       </div>
  62.  
  63.       <div class="form-group">
  64.         <label class="col-sm-3 control-label">Birthday</label>
  65.         <div class="col-sm-9">
  66.           <input type="date" class="form-control" value.bind="contact.birthday">
  67.         </div>
  68.       </div>
  69.  
  70.       <div class="form-group">
  71.         <label class="col-sm-3 control-label">Note</label>
  72.         <div class="col-sm-9">
  73.           <textarea class="form-control" value.bind="contact.note"></textarea>
  74.         </div>
  75.       </div>
  76.  
  77.       <hr>
  78.  
  79.       <div class="form-group" repeat.for="phoneNumber of contact.phoneNumbers">
  80.  
  81.         <div class="col-sm-2 col-sm-offset-1">
  82.           <select value.bind="phoneNumber.type" class="form-control">
  83.             <option value="Home">Home</option>
  84.             <option value="Office">Office</option>
  85.             <option value="Mobile">Mobile</option>
  86.             <option value="Other">Other</option>
  87.           </select>
  88.         </div>
  89.  
  90.         <div class="col-sm-8">
  91.           <input type="tel" class="form-control" placeholder="Phone number" value.bind="phoneNumber.number">
  92.         </div>
  93.  
  94.         <div class="col-sm-1">
  95.           <button type="button" class="btn btn-danger" click.delegate="contact.phoneNumbers.splice($index, 1)">
  96.             <i class="fa fa-times"></i>
  97.           </button>
  98.         </div>
  99.       </div>
  100.  
  101.       <div class="form-group">
  102.         <div class="col-sm-9 col-sm-offset-3">
  103.           <button type="button" class="btn btn-primary" click.delegate="contact.addPhoneNumber()">
  104.             <i class="fa fa-plus-square-o"></i> Add a phone number
  105.           </button>
  106.         </div>
  107.       </div>
  108.  
  109.       <hr>
  110.  
  111.       <div class="form-group" repeat.for="emailAddress of contact.emailAddresses">
  112.  
  113.         <div class="col-sm-2 col-sm-offset-1">
  114.           <select value.bind="emailAddress.type" class="form-control">
  115.             <option value="Home">Home</option>
  116.             <option value="Office">Office</option>
  117.             <option value="Other">Other</option>
  118.           </select>
  119.         </div>
  120.  
  121.         <div class="col-sm-8">
  122.           <input type="email" class="form-control" placeholder="Email address" value.bind="emailAddress.address">
  123.         </div>
  124.  
  125.         <div class="col-sm-1">
  126.           <button type="button" class="btn btn-danger" click.delegate="contact.emailAddresses.splice($index, 1)">
  127.             <i class="fa fa-times"></i>
  128.           </button>
  129.         </div>
  130.  
  131.       </div>
  132.  
  133.       <div class="form-group">
  134.  
  135.         <div class="col-sm-9 col-sm-offset-3">
  136.           <button type="button" class="btn btn-primary" click.delegate="contact.addEmailAddress()">
  137.             <i class="fa fa-plus-square-o"></i> Add an email address
  138.           </button>
  139.         </div>
  140.  
  141.       </div>
  142.  
  143.       <hr>
  144.  
  145.       <div class="form-group" repeat.for="address of contact.addresses">
  146.  
  147.         <div class="col-sm-2 col-sm-offset-1">
  148.           <select value.bind="address.type" class="form-control">
  149.             <option value="Home">Home</option>
  150.             <option value="Office">Office</option>
  151.             <option value="Other">Other</option>
  152.           </select>
  153.         </div>
  154.  
  155.         <div class="col-sm-8">
  156.  
  157.           <div class="row">
  158.             <div class="col-sm-4">
  159.               <input type="text" class="form-control" placeholder="Number" value.bind="address.number">
  160.             </div>
  161.  
  162.             <div class="col-sm-8">
  163.               <input type="text" class="form-control" placeholder="Street" value.bind="address.street">
  164.             </div>
  165.           </div>
  166.  
  167.           <div class="row">
  168.             <div class="col-sm-4">
  169.               <input type="text" class="form-control" placeholder="Postal code" value.bind="address.postalCode">
  170.             </div>
  171.  
  172.             <div class="col-sm-8">
  173.               <input type="text" class="form-control" placeholder="City" value.bind="address.city">
  174.             </div>
  175.           </div>
  176.  
  177.           <div class="row">
  178.             <div class="col-sm-4">
  179.               <input type="text" class="form-control" placeholder="State" value.bind="address.state">
  180.             </div>
  181.  
  182.             <div class="col-sm-8">
  183.               <input type="text" class="form-control" placeholder="Country" value.bind="address.country">
  184.             </div>
  185.           </div>
  186.  
  187.         </div>
  188.  
  189.         <div class="col-sm-1">
  190.           <button type="button" class="btn btn-danger" click.delegate="contact.addresses.splice($index, 1)">
  191.             <i class="fa fa-times"></i>
  192.           </button>
  193.         </div>
  194.  
  195.       </div>
  196.  
  197.       <div class="form-group">
  198.  
  199.         <div class="col-sm-9 col-sm-offset-3">
  200.           <button type="button" class="btn btn-primary" click.delegate="contact.addAddress()">
  201.             <i class="fa fa-plus-square-o"></i> Add an address
  202.           </button>
  203.         </div>
  204.  
  205.       </div>
  206.  
  207.       <hr>
  208.  
  209.       <div class="form-group" repeat.for="profile of contact.socialProfiles">
  210.  
  211.         <div class="col-sm-2 col-sm-offset-1">
  212.           <select value.bind="profile.type" class="form-control">
  213.             <option value="GitHub">GitHub</option>
  214.             <option value="Twitter">Twitter</option>
  215.           </select>
  216.         </div>
  217.  
  218.         <div class="col-sm-8">
  219.           <input type="text" class="form-control" placeholder="Username" value.bind="profile.username">
  220.         </div>
  221.  
  222.         <div class="col-sm-1">
  223.           <button type="button" class="btn btn-danger" click.delegate="contact.socialProfiles.splice($index, 1)">
  224.             <i class="fa fa-times"></i>
  225.           </button>
  226.         </div>
  227.  
  228.       </div>
  229.  
  230.       <div class="form-group">
  231.  
  232.         <div class="col-sm-9 col-sm-offset-3">
  233.           <button type="button" class="btn btn-primary" click.delegate="contact.addSocialProfile()">
  234.             <i class="fa fa-plus-square-o"></i> Add a social profile
  235.           </button>
  236.         </div>
  237.  
  238.       </div>
  239.  
  240.       <div class="form-group">
  241.  
  242.         <div class="col-sm-9 col-sm-offset-3">
  243.           <button type="submit" class="btn btn-success">Save</button>
  244.           <a if.bind="isNew" class="btn btn-danger" route-href="route: contacts">Cancel</a>
  245.           <a if.bind="!isNew" class="btn btn-danger" route-href="route: contact-details; params.bind: { id: contact.id }">Cancel</a>
  246.         </div>
  247.  
  248.       </div>
  249.  
  250.     </form>
  251.  
  252.   </section>
  253. </template>
  254.  
  255. //  contact-edition.js
  256.  
  257. import {inject, NewInstance} from 'aurelia-framework';
  258. import {ValidationController} from 'aurelia-validation';
  259. import {Router} from 'aurelia-router';
  260. import {ContactGateway} from './contact-gateway';
  261. import {Contact} from './models';
  262.  
  263. @inject(ContactGateway, NewInstance.of(ValidationController), Router)
  264. export class ContactEdition {
  265.  
  266.   constructor(contactGateway, validationController, router) {
  267.     this.contactGateway = contactGateway;
  268.     this.validationController = validationController;
  269.     this.router = router;
  270.   }
  271.  
  272.   activate(params, config) {
  273.     this.isNew = params.id === undefined;
  274.     if (this.isNew) {
  275.       this.contact = new Contact();
  276.     }
  277.     else {
  278.       return this.contactGateway.getById(params.id)
  279.         .then(contact => {
  280.           this.contact = contact;
  281.           config.navModel.setTitle(contact.fullName);
  282.         });
  283.     }
  284.   }
  285.  
  286.   save() {
  287.     this.validationController.validate().then(errors => {
  288.       if (errors.length > 0) {
  289.         return;
  290.       }
  291.  
  292.       if (this.isNew) {
  293.         this.contactGateway.create(this.contact)
  294.           .then(() => this.router.navigateToRoute('contacts'));
  295.       }
  296.       else {
  297.         this.contactGateway.update(this.contact.id, this.contact)
  298.           .then(() => this.router.navigateToRoute('contact-details',{ id: this.contact.id }));
  299.       }
  300.     });
  301.   }
  302. }
  303.  
  304. //  models.js
  305.  
  306. import {ValidationRules} from 'aurelia-validation';
  307.  
  308. export class PhoneNumber {
  309.  
  310.   static fromObject(src) {
  311.     return Object.assign(new PhoneNumber(), src);
  312.   }
  313.  
  314.   type = 'Home';
  315.   number = '';
  316.  
  317. }
  318.  
  319. export class EmailAddress {
  320.  
  321.   static fromObject(src) {
  322.     return Object.assign(new EmailAddress(), src);
  323.   }
  324.  
  325.   type = 'Home';
  326.   address = '';
  327.  
  328. }
  329.  
  330. export class Address {
  331.  
  332.   static fromObject(src) {
  333.     return Object.assign(new Address(), src);
  334.   }
  335.  
  336.   type = 'Home';
  337.   number = '';
  338.   street = '';
  339.   postalCode = '';
  340.   city = '';
  341.   state = '';
  342.   country = '';
  343.  
  344. }
  345.  
  346. export class SocialProfile {
  347.  
  348.   static fromObject(src) {
  349.     return Object.assign(new SocialProfile(), src);
  350.   }
  351.  
  352.   type = 'GitHub';
  353.   username = '';
  354.  
  355. }
  356.  
  357. export class Contact {
  358.  
  359.   static fromObject(src) {
  360.     const contact = Object.assign(new Contact(), src);
  361.     contact.phoneNumbers = contact.phoneNumbers.map(PhoneNumber.fromObject);
  362.     contact.emailAddresses = contact.emailAddresses.map(EmailAddress.fromObject);
  363.     contact.addresses = contact.addresses.map(Address.fromObject);
  364.     contact.socialProfiles = contact.socialProfiles.map(SocialProfile.fromObject);
  365.     return contact;
  366.   }
  367.  
  368.   firstName = '';
  369.   lastName = '';
  370.   company = '';
  371.   birthday = '';
  372.   phoneNumbers = [];
  373.   emailAddresses = [];
  374.   addresses = [];
  375.   socialProfiles = [];
  376.   note = '';
  377.  
  378.   constructor() {
  379.     ValidationRules
  380.       .ensure('firstName').minLength(5).required()
  381.       .on(this);
  382.   }
  383.  
  384.   static fromObject(src) {
  385.     return Object.assign(new Contact(), src);
  386.   }
  387.  
  388.   get isPerson() {
  389.     return this.firstName || this.lastName;
  390.   }
  391.  
  392.   get fullName() {
  393.     const fullName = this.isPerson ? `${this.firstName} ${this.lastName}` : this.company;
  394.     return fullName || '';
  395.   }
  396.  
  397.   get firstLetter() {
  398.     const name = this.lastName || this.firstName || this.company;
  399.     return name ? name[0].toUpperCase() : '?';
  400.   }
  401.  
  402.   addPhoneNumber() {
  403.     this.phoneNumbers.push(new PhoneNumber());
  404.   }
  405.  
  406.   addEmailAddress() {
  407.     this.emailAddresses.push(new EmailAddress());
  408.   }
  409.  
  410.   addAddress() {
  411.     this. addresses.push(new Address());
  412.   }
  413.  
  414.   addSocialProfile() {
  415.     this.socialProfiles.push(new SocialProfile());
  416.   }
  417.  
  418. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement