module Shockout { export interface IViewModelAttachments extends KnockoutObservable < Array < ISpAttachment > > { getViewModel : Function ; getSpForm : Function ; } export interface IViewModel { Id : KnockoutObservable < number > ; Created : KnockoutObservable < Date > ; CreatedBy : KnockoutObservable < ISpPerson > ; Modified : KnockoutObservable < Date > ; ModifiedBy : KnockoutObservable < ISpPerson > ; parent : Shockout . SPForm ; allowSave : KnockoutObservable < boolean > ; allowPrint : KnockoutObservable < boolean > ; allowDelete : KnockoutObservable < boolean > ; attachments : IViewModelAttachments ; currentUser : KnockoutObservable < any > ; historyItems : KnockoutObservableArray < IHistoryItem > ; isValid : KnockoutComputed < boolean > ; showUserProfiles : KnockoutObservable < boolean > ; navMenuItems : KnockoutObservableArray < any > ; isMember : KnockoutComputed < boolean > ; isAuthor ( ) : boolean ; deleteItem ( ) : void ; cancel ( ) : void ; print ( ) : void ; deleteAttachment ( obj , event ) : boolean ; getAttachments ( self , callback ) : void ; save ( model , btn ) : void ; submit ( model , btn ) : void ; } export class ViewModel implements IViewModel { public static isSubmittedKey : string ; public static parent : SPForm ; public Id : KnockoutObservable < number > = ko . observable ( null ) ; public Created : KnockoutObservable < Date > = ko . observable ( null ) ; public CreatedBy : KnockoutObservable < ISpPerson > = ko . observable ( null ) ; public Modified : KnockoutObservable < Date > = ko . observable ( null ) ; public ModifiedBy : KnockoutObservable < ISpPerson > = ko . observable ( null ) ; public parent : Shockout . SPForm ; public allowSave : KnockoutObservable < boolean > = ko . observable ( false ) ; public allowPrint : KnockoutObservable < boolean > = ko . observable ( false ) ; public allowDelete : KnockoutObservable < boolean > = ko . observable ( false ) ; public attachments : IViewModelAttachments = < any > ko . observableArray ( [ ] ) ; public currentUser : KnockoutObservable < ICurrentUser > ; public historyItems : KnockoutObservableArray < any > = ko . observableArray ( ) ; public isValid : KnockoutComputed < boolean > ; public showUserProfiles : KnockoutObservable < boolean > = ko . observable ( false ) ; public navMenuItems : KnockoutObservableArray < any > = ko . observableArray ( ) ; public isMember : KnockoutComputed < boolean > ; public deleteAttachment ; public getAttachments ; constructor ( instance ) { var self = this ; this . parent = instance ; ViewModel . parent = instance ; this . isValid = ko . pureComputed ( function ( ) : boolean { return self . parent . formIsValid ( self ) ; } ) ; this . deleteAttachment = instance . deleteAttachment ; this . getAttachments = instance . getAttachments ; this . currentUser = ko . observable ( instance . getCurrentUser ( ) ) ; this . attachments . getViewModel = function ( ) { return self ; } ; this . attachments . getSpForm = function ( ) { return self . parent ; } ; this . isMember = < KnockoutComputed < boolean > > ko . pureComputed ( < any > { read : function ( ) : boolean { return false ; } , write : function ( groups ) : boolean { return self . parent . currentUserIsMemberOfGroups ( groups ) ; } , owner : this } ) ; } public isAuthor ( ) { if ( ! ! ! this . CreatedBy ( ) ) { return true ; } return this . currentUser ( ) . id == this . CreatedBy ( ) . Id ; } public deleteItem ( ) { this . parent . deleteListItem ( this ) ; } public cancel ( ) { let spForm = this . parent ; if ( spForm . onCloseAction ) { spForm . onCloseAction ( ) ; return ; } var src = this . parent . getSourceUrl ( ) ; window . location . href = ! ! src ? src : this . parent . getRootUrl ( ) ; } public print ( ) { window . print ( ) ; } public save ( model , btn ) { this . parent . saveListItem ( model , false ) ; } public submit ( model , btn ) { this . parent . saveListItem ( model , true ) ; } } }