From b973628d043a6f78b1935eb936511338d18a3b6f Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Tue, 12 Apr 2016 21:05:52 -0400 Subject: [PATCH 1/3] Add a function for parsing into url parameters. --- front/current/lib/url.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 front/current/lib/url.ts diff --git a/front/current/lib/url.ts b/front/current/lib/url.ts new file mode 100644 index 0000000..77bcc69 --- /dev/null +++ b/front/current/lib/url.ts @@ -0,0 +1,17 @@ +export function param(object) { + // Write an object to a url search string. + let url_components = []; + for (let key in object) { + let value = object[key]; + if (value instanceof Array) { + for (let i = 0; i < value.length; i++) { + url_components.push( + key + '[]' + '=' + encodeURIComponent(value[i]) + ) + } + } else { + url_components.push(key + '=' + encodeURIComponent(value)); + } + } + return '?' + url_components.join('&'); +} -- GitLab From 737b53d2ffe49704eb14e7730a2447a66cd9b60e Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Tue, 12 Apr 2016 21:06:12 -0400 Subject: [PATCH 2/3] Correctly set url parameters. --- front/current/services/rest.service.ts | 49 +++++++------------------- 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/front/current/services/rest.service.ts b/front/current/services/rest.service.ts index 7ee6cf2..68db133 100644 --- a/front/current/services/rest.service.ts +++ b/front/current/services/rest.service.ts @@ -4,6 +4,7 @@ import { Observable } from 'rxjs/Observable'; import { config } from '../config'; import { extend } from '../lib/object'; +import { param } from '../lib/url'; export class Model { constructor( @@ -32,25 +33,11 @@ export class Model { }; get url(): string { - let params = new URLSearchParams(''); - - for (let key in this.filters) { - let value = this.filters[key]; - - if (typeof value[Symbol.iterator] === 'function') { - for (let i = 0; i < value.length; i++) { - params.set(key + '[]', value[i]); - } - } else { - params.set(key, value); - } - } - return ( this.service.config.url + this.service.url + (this.data['id'] || '') + - '?' + params.toString()); + param(this.filters)); }; parse(response:Response):Model { @@ -69,16 +56,20 @@ export class Model { .do(self.parse.bind(self), self.setError.bind(self), () => { self.loading = false; }) .subscribe(observer)); }; - save():Observable { + save(observer?):Observable { // Save a model to the server. - let method = (this.data['id'] ? 'put' : 'post'); + let self = this; + let method = (self.data['id'] ? 'put' : 'post'); + self.loading = true; + self.error = undefined; return ( - this._http[method]( - this.url, - JSON.stringify(this.data), + self._http[method]( + self.url, + JSON.stringify(self.data), {headers: config.HEADERS} ) - .map(this.parse)); + .do(self.parse.bind(self), self.setError.bind(self), () => { self.loading = false; }) + .subscribe(observer)); }; destroy():Observable { // Delete a model on the server. @@ -121,21 +112,7 @@ export class Collection { }; public get url(): string { - let params = new URLSearchParams(''); - - for (let key in this.filters) { - let value = this.filters[key]; - - if (typeof value[Symbol.iterator] === 'function') { - for (let i = 0; i < value.length; i++) { - params.set(key + '[]', value[i]); - } - } else { - params.set(key, value); - } - } - - return this.service.config.url + this.service.url + '?' + params.toString(); + return this.service.config.url + this.service.url + param(this.filters); }; parse(response:Response):Collection { -- GitLab From d22366c95e42413f6931fd6425a9efdde7fcb05e Mon Sep 17 00:00:00 2001 From: astex <0astex@gmail.com> Date: Tue, 12 Apr 2016 21:06:22 -0400 Subject: [PATCH 3/3] Add document upload. --- .../assets/styles/components/_global.scss | 3 - .../styles/components/_project-list.scss | 1 + .../components/projects/list.component.html | 7 ++ .../components/projects/list.component.ts | 9 +-- .../projects/new-document-slot.component.html | 1 + .../projects/new-document-slot.component.ts | 64 +++++++++++++++++++ 6 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 front/current/components/projects/new-document-slot.component.html create mode 100644 front/current/components/projects/new-document-slot.component.ts diff --git a/front/current/assets/styles/components/_global.scss b/front/current/assets/styles/components/_global.scss index 91d80d8..c6175ec 100644 --- a/front/current/assets/styles/components/_global.scss +++ b/front/current/assets/styles/components/_global.scss @@ -38,9 +38,6 @@ p { ul, ol { margin: 0; padding: 0; - li { - line-height: $leading; - } ul, ol { margin-top: 0; margin-bottom: 0; diff --git a/front/current/assets/styles/components/_project-list.scss b/front/current/assets/styles/components/_project-list.scss index d686899..bdc2d8c 100644 --- a/front/current/assets/styles/components/_project-list.scss +++ b/front/current/assets/styles/components/_project-list.scss @@ -21,6 +21,7 @@ table.projects { } td { text-align: left; + vertical-align: top; } } diff --git a/front/current/components/projects/list.component.html b/front/current/components/projects/list.component.html index ccafc8d..74f427e 100644 --- a/front/current/components/projects/list.component.html +++ b/front/current/components/projects/list.component.html @@ -38,6 +38,13 @@ +
  • + + +
  • diff --git a/front/current/components/projects/list.component.ts b/front/current/components/projects/list.component.ts index 5895820..bce9521 100644 --- a/front/current/components/projects/list.component.ts +++ b/front/current/components/projects/list.component.ts @@ -8,11 +8,14 @@ import { ProjectService } from '../../services/project/project.service'; import { DocumentSlotService } from '../../services/project/document-slot.service'; import { DocumentService } from '../../services/document/document.service'; +import { NewDocumentSlotComponent } from './new-document-slot.component'; + @Component({ selector: 'project-list', - templateUrl: config.static_url + '/components/projects/list.component.html' + templateUrl: config.static_url + '/components/projects/list.component.html', + directives: [NewDocumentSlotComponent] }) -export class ProjectListComponent implements OnInit{ +export class ProjectListComponent implements OnInit { constructor( private _router:Router, private _restService:RestService, @@ -26,8 +29,6 @@ export class ProjectListComponent implements OnInit{ public documentSlots:Collection; public documents:Collection; - errorMessage:string; - ngOnInit() { this.fetch(); }; diff --git a/front/current/components/projects/new-document-slot.component.html b/front/current/components/projects/new-document-slot.component.html new file mode 100644 index 0000000..974682d --- /dev/null +++ b/front/current/components/projects/new-document-slot.component.html @@ -0,0 +1 @@ + diff --git a/front/current/components/projects/new-document-slot.component.ts b/front/current/components/projects/new-document-slot.component.ts new file mode 100644 index 0000000..8aa3ff0 --- /dev/null +++ b/front/current/components/projects/new-document-slot.component.ts @@ -0,0 +1,64 @@ +import { Component, OnInit } from 'angular2/core'; + +import { config } from '../../config'; +import { RestService, Model, Collection } from '../../services/rest.service'; + +import { DocumentSlotService } from '../../services/project/document-slot.service'; +import { DocumentService } from '../../services/document/document.service'; + +@Component({ + selector: 'new-document-slot', + inputs: ['project', 'documentSlots', 'documents'], + templateUrl: config.static_url + '/components/projects/new-document-slot.component.html' +}) +export class NewDocumentSlotComponent implements OnInit { + constructor( + private _restService:RestService, + + private _documentSlotService:DocumentSlotService, + private _documentService:DocumentService + ) {}; + + // Inputs + public project:Model; + public documentSlots:Collection; + public documents:Collection; + + public documentSlot:Model; + public document_:Model + + ngOnInit() { + this.documentSlot = this._restService.Model(this._documentSlotService, { + project_id: this.project.data['id'], + role: 'miscelaneous' + }); + this.document_ = this._restService.Model(this._documentService, {}); + }; + + public upload(e) { + let self = this; + + let el = e.currentTarget; + let file = el.files[0]; + let reader = new FileReader(); + + reader.addEventListener('load', () => { + self.document_ + .set({ + data: reader.result, + name: el.value.split(/(\\|\/)/g).pop() + }) + .save(() => { + self.documentSlot + .set({document_key: self.document_.data['key']}) + .save(() => { + self.documents.models.push(self.document_); + self.documentSlots.models.push(self.documentSlot); + self.ngOnInit(); + }); + }); + }, false); + if (file) + reader.readAsDataURL(file); + }; +}; -- GitLab