ionicx-wechat-like-album
Version:
a album is like the wechat for ionic
171 lines (153 loc) • 4.4 kB
text/typescript
import { PhotoDetailComponent } from './photo-detail/photo-detail.component';
import { Component, OnInit, Input, Output, EventEmitter, forwardRef } from '@angular/core';
import { ModalController, ViewController, ActionSheetController, IonicPage } from 'ionic-angular';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
export class PhotoViewComponent implements OnInit, ControlValueAccessor {
private propagateChange = (_: any) => { };
set opts(opts: any) {
this.myOpts = Object.assign(this.myOpts, opts);
}
imgs: string[];
outImgs: string[];
myOpts = {
addable: true,//是否能添加图片
scanable: true,//是否进入能浏览图片
removeable: true,//是否能移除图片
}
imgsChange = new EventEmitter<string[]>();
addOnePicture = new EventEmitter<Function>();
constructor(
public modalCtrl: ModalController,
private actionSheetCtrl: ActionSheetController
) { }
ngOnInit() {
if (this.imgs) {
} else {
this.imgs = [];
}
this.bindEventForArray(this.imgs)
}
/**
* 对能改变数组的方法添加钩子函数
*
* @param {any[]} array
*/
bindEventForArray(array: any[]) {
let fun = ['push', 'pop', 'sort', 'reverse', 'unshift', 'shift', 'splice'];
fun.forEach((item) => {
let _prototype = Array.prototype[item];
let that = this;
array[item] = function () {
let new_value = _prototype.apply(this, arguments);
that.outImgs = this.slice();
that.emitChange(that.outImgs)
return new_value;
}
})
}
/**
* 给外部formControl写入数据
*
* @param {*} value
*/
writeValue(value: any) {
if (value != undefined) {
if (value instanceof Array) {
this.outImgs = value;
} else {
this.outImgs = [value];
}
} else {
this.outImgs = [];
}
this.imgs = this.outImgs.slice();
this.bindEventForArray(this.imgs)
}
/**
* 把外面登记的监测change的函数赋值给this.propagateChange
* 当内部数据改变时,可使用this.propagateChange(this.imgs.slice())去触发传递出去
* @param {*} fn
*/
registerOnChange(fn: any) {
this.propagateChange = fn;
}
/**
* 也是一样注册然后调用当 touched
* @param {*} fn
*/
registerOnTouched(fn: any) { }
/**
* 添加图片
*/
addPhoto() {
let add = (img) => this.imgs.push(img);
this.addOnePicture.emit(add);
}
/**
* 选择图片,并用模态框展示
* @param {number} idx 当前图片的序号
*/
selectPhoto(idx: number) {
if (!this.myOpts.scanable) return
this.presentProfileModal(idx);
}
/**
* 初始化模态框
* @param {number} idx 当前图片的序号
*/
presentProfileModal(idx: number) {
let profileModal = this.modalCtrl.create(PhotoDetailComponent, { imgs: this.imgs, idx: idx, removeable: this.myOpts.removeable });
profileModal.present();
}
/**
* 把change冒泡到外部
*
*/
emitChange(val: any) {
this.imgsChange.emit(val);
this.propagateChange(val)
}
}