From f34216213f2928f54a16519524694f845aba9de8 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Sun, 7 Oct 2018 07:36:07 -0700 Subject: [PATCH] Split out new-post modal and post-menu template and code --- src/app/app.module.ts | 4 +- src/pages/stream/new-post.html | 46 ++++- src/pages/stream/new-post.ts | 193 +++++++++++++++++++++ src/pages/stream/post-menu.ts | 69 ++++++++ src/pages/stream/stream.ts | 306 +-------------------------------- src/pages/thread/thread.ts | 3 +- 6 files changed, 306 insertions(+), 315 deletions(-) create mode 100644 src/pages/stream/new-post.ts create mode 100644 src/pages/stream/post-menu.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index d08249d..c4bff4b 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -4,7 +4,9 @@ import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; import { MyApp } from './app.component'; import { LoginPage } from '../pages/login/login'; -import { StreamPage, NewPostModal, PostMenu } from '../pages/stream/stream'; +import { StreamPage } from '../pages/stream/stream'; +import { PostMenu } from '../pages/stream/post-menu'; +import { NewPostModal } from '../pages/stream/new-post'; import { ThreadPage } from '../pages/thread/thread'; import { SettingsPage } from '../pages/settings/settings'; diff --git a/src/pages/stream/new-post.html b/src/pages/stream/new-post.html index 083daef..499085e 100644 --- a/src/pages/stream/new-post.html +++ b/src/pages/stream/new-post.html @@ -1,20 +1,48 @@ - New Post + - - + +
{{textCount()}}
+ + + + + + + + + + +

{{ f.name }}

+ +
+
+ diff --git a/src/pages/stream/new-post.ts b/src/pages/stream/new-post.ts new file mode 100644 index 0000000..ebb7ad3 --- /dev/null +++ b/src/pages/stream/new-post.ts @@ -0,0 +1,193 @@ +import { Component, ViewChild, ChangeDetectorRef } from '@angular/core'; +import { ViewController, NavController, NavParams, ModalController, Content, ToastController, PopoverController, Events } from 'ionic-angular'; +import { Storage } from '@ionic/storage'; +import { FileChooser } from '@ionic-native/file-chooser'; +import { FilePath } from '@ionic-native/file-path'; +import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; + +@Component({ + selector: 'modal-newpost', + templateUrl: 'new-post.html', +}) +export class NewPostModal { + title: string; + replyid: string; + ptext: string = ""; + showProgress: boolean = false; + files: Array = []; + fname: string = ""; + fpath: string = ""; + options: Object = {}; + myUsername: string; + authToken: string; + longpost: Object = {}; + raw: {type: string, value: Object}[] = []; + + constructor(public navParams: NavParams, public viewCtrl: ViewController, public toastCtrl: ToastController, + private fileChooser: FileChooser, private storage: Storage, public events: Events, private filePath: FilePath, + private transfer: FileTransfer) { + console.log(JSON.stringify(this.navParams)); + this.myUsername = navParams.data.me; + if (navParams.data.type === 'reply') { + this.replyid = navParams.data.post.id; + this.options = {replyTo: this.replyid}; + if (navParams.data.post.user.username !== this.myUsername) { + this.ptext = "@" + navParams.data.post.user.username + " "; + } else { + this.ptext = "" + } + if (navParams.data.post.content.entities) { + if (navParams.data.post.content.entities.mentions.length > 0) { + this.ptext = this.ptext + this.parseMentions(navParams.data.post.content.entities.mentions); + } + } + this.title = "Reply to " + navParams.data.post.user.username + } else if (navParams.data.type === 'quote') { + this.ptext = " >> @" + navParams.data.post.user.username + ": " + navParams.data.post.content.text; + this.title = "New Post"; + } else { + this.title = "New Post"; + } + this.storage.get('token').then((val) => { + this.authToken = val; + }); + } + + dismiss() { + this.viewCtrl.dismiss(); + } + + send() { + + if (this.ptext.length > 254) { + this.longpost = { + 'title': '', + 'body': this.ptext, + 'tstamp': new Date().valueOf() + } + this.ptext = this.ptext.substr(0, 40) + "... - http://chimpnut.nl/u/"; + this.ptext = this.ptext + this.navParams.data.me + "/lp/{object_id} - #longpost"; + this.raw.push({ + type: "nl.chimpnut.blog.post", + value: this.longpost + }); + } + + this.options['raw'] = this.raw; + pnut.createPost(this.ptext, this.options).then(res => { + console.log('-success-'); + console.log(JSON.stringify(res)); + this.presentToast("Status posted."); + this.events.publish('stream:reload', {}); + }).catch(err => { + console.log('-error posting-'); + console.log(JSON.stringify(err)); + }); + this.viewCtrl.dismiss(); + } + + presentToast(text) { + let toast = this.toastCtrl.create({ + position: 'top', + message: text, + duration: 2000 + }); + toast.present(); + } + + parseMentions(mentions) { + let mtext = "" + for(var i = 0; i < mentions.length; i++) { + let mu = mentions[i].text; + if (mu !== this.myUsername) { + mtext += "@" + mu + " "; + } + } + if (mtext.length > 0) { + if (this.navParams.data.cc) { + mtext = "\n/" + mtext; + } + return mtext; + } else { + return ""; + } + } + + textCount() { + let counttext = "" + let count = 254 - this.ptext.length + if (count < 1) { + counttext = "longpost" + } else { + counttext = String(count) + } + return counttext + } + + attachImage() { + // console.log('file chooser'); + const fileTransfer: FileTransferObject = this.transfer.create(); + + this.fileChooser.open().then(uri => { + console.log('File URI: ' + uri); + this.filePath.resolveNativePath(uri).then(filePath => { + this.fpath = filePath; + this.fname = filePath.split('/').pop(); + + let options: FileUploadOptions = { + fileKey: 'content', + fileName: this.fname, + params: { + name: this.fname, + type: 'com.monkeystew.goober_m', + is_public: true + }, + headers: {'Authorization': 'Bearer ' + this.authToken} + } + + this.showProgress = true; + fileTransfer.upload(this.fpath, 'https://api.pnut.io/v0/files', options).then((response) => { + + let rdata = JSON.parse(response.response); + let oembed = { + '+io.pnut.core.file': { + file_id: rdata.data.id, + file_token: rdata.data.file_token, + format: 'oembed' + } + } + // console.log(JSON.stringify(oembed)); + this.raw.push({ + type: "io.pnut.core.oembed", + value: oembed + }); + this.files.push({ + name: this.fname, + link: rdata.data.link, + id: this.raw.length - 1 + }); + this.showProgress = false; + }).catch((err) => { + + this.showProgress = false; + let edata = JSON.parse(err.body); + this.presentToast(edata.meta.error_message); + }); + + }).catch(err => { + console.log('-error getting filepath-'); + console.log(err); + }); + + }).catch(err => { + console.log(err); + }); + + } + + unattach(id) { + console.log('removing item ' + id); + this.raw.splice(id, 1); + this.files.splice(id, 1); + } +} diff --git a/src/pages/stream/post-menu.ts b/src/pages/stream/post-menu.ts new file mode 100644 index 0000000..718c015 --- /dev/null +++ b/src/pages/stream/post-menu.ts @@ -0,0 +1,69 @@ +import { Component, ViewChild, ChangeDetectorRef } from '@angular/core'; +import { ViewController, NavController, NavParams, ModalController, Content, ToastController, PopoverController } from 'ionic-angular'; +import { Events } from 'ionic-angular'; + +import * as pnut from 'pnut-butter'; + +@Component({ + template: ` + + + + + +` +}) +export class PostMenu { + + showDelBtn: boolean = false; + + constructor(public navParams: NavParams, public viewCtrl: ViewController, public toastCtrl: ToastController, + public events: Events) { + if (navParams.data.me == navParams.data.post.user.username) { + this.showDelBtn = true; + } else { + this.showDelBtn = false; + } + } + + browse() { + window.open('https://posts.pnut.io/' + this.navParams.data.post.id, '_system'); + this.close(); + } + + share() { + (window).shareContentPlugin.share(this.navParams.data.post.content.text, function(e) { + console.log('sharing post:'); + console.log(JSON.stringify(e)); + }, function(e) { + console.log('sharing failed:'); + console.log(JSON.stringify(e)); + }); + this.close(); + } + + delete() { + pnut.deletePost(this.navParams.data.post.id).then(res => { + console.log(res); + this.presentToast('Post Deleted'); + this.events.publish('stream:reload', {}); + }).catch( err => { + console.log('-error-'); + console.log(err); + }); + this.close() + } + + presentToast(text) { + let toast = this.toastCtrl.create({ + position: 'top', + message: text, + duration: 2000 + }); + toast.present(); + } + + close() { + this.viewCtrl.dismiss(); + } +} diff --git a/src/pages/stream/stream.ts b/src/pages/stream/stream.ts index b71260f..3cab72a 100644 --- a/src/pages/stream/stream.ts +++ b/src/pages/stream/stream.ts @@ -2,11 +2,10 @@ import { Component, ViewChild, ChangeDetectorRef } from '@angular/core'; import { ViewController, NavController, NavParams, ModalController, Content, ToastController, PopoverController } from 'ionic-angular'; import { ThreadPage } from '../thread/thread'; import { Storage } from '@ionic/storage'; -import { FileChooser } from '@ionic-native/file-chooser'; -import { FilePath } from '@ionic-native/file-path'; -import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; import { Events } from 'ionic-angular'; import { LoginPage } from '../login/login'; +import { PostMenu } from '../stream/post-menu'; +import { NewPostModal } from '../stream/new-post'; import * as pnut from 'pnut-butter'; @@ -326,304 +325,3 @@ export class StreamPage { } } - -@Component({ - // templateUrl: 'new-post.html' - selector: 'modal-newpost', - template: ` - - - - - - - - {{ title }} - - - - - - - - - - - - - - - -
{{textCount()}}
- - - -
- - - - - - -

{{ f.name }}

- -
-
-
-
-` -}) -export class NewPostModal { - title: string; - replyid: string; - ptext: string = ""; - showProgress: boolean = false; - files: Array = []; - fname: string = ""; - fpath: string = ""; - options: Object = {}; - myUsername: string; - authToken: string; - longpost: Object = {}; - raw: {type: string, value: Object}[] = []; - - constructor(public navParams: NavParams, public viewCtrl: ViewController, public toastCtrl: ToastController, - private fileChooser: FileChooser, private storage: Storage, public events: Events, private filePath: FilePath, - private transfer: FileTransfer) { - console.log(JSON.stringify(this.navParams)); - this.myUsername = navParams.data.me; - if (navParams.data.type === 'reply') { - this.replyid = navParams.data.post.id; - this.options = {replyTo: this.replyid}; - if (navParams.data.post.user.username !== this.myUsername) { - this.ptext = "@" + navParams.data.post.user.username + " "; - } else { - this.ptext = "" - } - if (navParams.data.post.content.entities) { - if (navParams.data.post.content.entities.mentions.length > 0) { - this.ptext = this.ptext + this.parseMentions(navParams.data.post.content.entities.mentions); - } - } - this.title = "Reply to " + navParams.data.post.user.username - } else if (navParams.data.type === 'quote') { - this.ptext = " >> @" + navParams.data.post.user.username + ": " + navParams.data.post.content.text; - this.title = "New Post"; - } else { - this.title = "New Post"; - } - this.storage.get('token').then((val) => { - this.authToken = val; - }); - } - - dismiss() { - this.viewCtrl.dismiss(); - } - - send() { - - if (this.ptext.length > 254) { - this.longpost = { - 'title': '', - 'body': this.ptext, - 'tstamp': new Date().valueOf() - } - this.ptext = this.ptext.substr(0, 40) + "... - http://chimpnut.nl/u/"; - this.ptext = this.ptext + this.navParams.data.me + "/lp/{object_id} - #longpost"; - this.raw.push({ - type: "nl.chimpnut.blog.post", - value: this.longpost - }); - } - - this.options['raw'] = this.raw; - pnut.createPost(this.ptext, this.options).then(res => { - console.log('-success-'); - console.log(JSON.stringify(res)); - this.presentToast("Status posted."); - this.events.publish('stream:reload', {}); - }).catch(err => { - console.log('-error posting-'); - console.log(JSON.stringify(err)); - }); - this.viewCtrl.dismiss(); - } - - presentToast(text) { - let toast = this.toastCtrl.create({ - position: 'top', - message: text, - duration: 2000 - }); - toast.present(); - } - - parseMentions(mentions) { - let mtext = "" - for(var i = 0; i < mentions.length; i++) { - let mu = mentions[i].text; - if (mu !== this.myUsername) { - mtext += "@" + mu + " "; - } - } - if (mtext.length > 0) { - if (this.navParams.data.cc) { - mtext = "\n/" + mtext; - } - return mtext; - } else { - return ""; - } - } - - textCount() { - let counttext = "" - let count = 254 - this.ptext.length - if (count < 1) { - counttext = "longpost" - } else { - counttext = String(count) - } - return counttext - } - - attachImage() { - // console.log('file chooser'); - const fileTransfer: FileTransferObject = this.transfer.create(); - - this.fileChooser.open().then(uri => { - console.log('File URI: ' + uri); - this.filePath.resolveNativePath(uri).then(filePath => { - this.fpath = filePath; - this.fname = filePath.split('/').pop(); - - let options: FileUploadOptions = { - fileKey: 'content', - fileName: this.fname, - params: { - name: this.fname, - type: 'com.monkeystew.goober_m', - is_public: true - }, - headers: {'Authorization': 'Bearer ' + this.authToken} - } - - this.showProgress = true; - fileTransfer.upload(this.fpath, 'https://api.pnut.io/v0/files', options).then((response) => { - - let rdata = JSON.parse(response.response); - let oembed = { - '+io.pnut.core.file': { - file_id: rdata.data.id, - file_token: rdata.data.file_token, - format: 'oembed' - } - } - // console.log(JSON.stringify(oembed)); - this.raw.push({ - type: "io.pnut.core.oembed", - value: oembed - }); - this.files.push({ - name: this.fname, - link: rdata.data.link, - id: this.raw.length - 1 - }); - this.showProgress = false; - }).catch((err) => { - - this.showProgress = false; - let edata = JSON.parse(err.body); - this.presentToast(edata.meta.error_message); - }); - - }).catch(err => { - console.log('-error getting filepath-'); - console.log(err); - }); - - }).catch(err => { - console.log(err); - }); - - } - - unattach(id) { - console.log('removing item ' + id); - this.raw.splice(id, 1); - this.files.splice(id, 1); - } -} - -@Component({ - template: ` - - - - - -` -}) -export class PostMenu { - - showDelBtn: boolean = false; - - constructor(public navParams: NavParams, public viewCtrl: ViewController, public toastCtrl: ToastController, - public events: Events) { - if (navParams.data.me == navParams.data.post.user.username) { - this.showDelBtn = true; - } else { - this.showDelBtn = false; - } - } - - browse() { - window.open('https://posts.pnut.io/' + this.navParams.data.post.id, '_system'); - this.close(); - } - - share() { - (window).shareContentPlugin.share(this.navParams.data.post.content.text, function(e) { - console.log('sharing post:'); - console.log(JSON.stringify(e)); - }, function(e) { - console.log('sharing failed:'); - console.log(JSON.stringify(e)); - }); - this.close(); - } - - delete() { - pnut.deletePost(this.navParams.data.post.id).then(res => { - console.log(res); - this.presentToast('Post Deleted'); - this.events.publish('stream:reload', {}); - }).catch( err => { - console.log('-error-'); - console.log(err); - }); - this.close() - } - - presentToast(text) { - let toast = this.toastCtrl.create({ - position: 'top', - message: text, - duration: 2000 - }); - toast.present(); - } - - close() { - this.viewCtrl.dismiss(); - } -} diff --git a/src/pages/thread/thread.ts b/src/pages/thread/thread.ts index c5fcb72..17c93b3 100644 --- a/src/pages/thread/thread.ts +++ b/src/pages/thread/thread.ts @@ -1,6 +1,7 @@ import { Component } from '@angular/core'; import { NavController, NavParams, ModalController, ToastController, PopoverController } from 'ionic-angular'; -import { NewPostModal, PostMenu } from '../stream/stream'; +import { NewPostModal } from '../stream/stream'; +import { PostMenu } from '../stream/post-menu'; import * as pnut from 'pnut-butter';