module Shockout { export class SpAttachment implements ISpAttachment { __metadata : ISpAttachmentMetadata ; FileName : string ; ServerRelativeUrl : string ; constructor ( rootUrl , siteUrl , listGuid , listName , itemId , fileName ) { siteUrl = Utils . formatSubsiteUrl ( siteUrl ) ; var uri = `${ rootUrl + siteUrl }_api/Web/Lists(guid'${ listGuid }')/Items(${ itemId })/AttachmentFiles('${ fileName }')` ; this . __metadata = { uri : uri , id : uri , type : "SP.Attachment" } ; this . FileName = fileName ; this . ServerRelativeUrl = `/${ siteUrl }Lists/${ listName }/Attachments/${ itemId }/${ fileName }` ; } } export interface ICafe { asyncFns : Array < Function > ; start ( msg ? ) : void ; complete ( fn ) : ICafe ; fail ( fn ) : ICafe ; finally ( fn ) : ICafe ; next ( success ? , msg ? , args ? ) : void ; } param { Array < Function > } asyncFns returns export class Cafe { private _complete : Function ; private _fail : Function ; private _finally : Function ; public asyncFns : Array < Function > ; public static create ( asyncFns = undefined ) { return new Cafe ( asyncFns ) ; } constructor ( asyncFns = undefined ) { if ( asyncFns ) { this . asyncFns = asyncFns ; } return this ; } public start ( msg = undefined ) { this . next ( true , msg ) ; } public complete ( fn ) { this . _complete = fn ; return this ; } ; public fail ( fn ) { this . _fail = fn ; return this ; } public finally ( fn ) { this . _finally = fn ; return this ; } public next ( success = true , msg = undefined , args = undefined ) { if ( ! this . asyncFns ) { throw "Error in Cafe: The required parameter `asyncFns` of type (Array<Function>) is undefined. Don't forget to instantiate Cafe with this parameter or set its value after instantiation." ; } if ( this . _complete ) { this . _complete ( msg , success , args ) ; } if ( ! success ) { if ( this . _fail ) { this . _fail ( msg , success , args ) ; } return ; } if ( this . asyncFns . length == 0 ) { if ( this . _finally ) { this . _finally ( msg , success , args ) ; } return ; } this . asyncFns . shift ( ) ( this , args ) ; } } param { string } fileName param { number } bytes export interface IFileUpload { label : KnockoutObservable < string > ; progress : KnockoutObservable < number > ; fileName : KnockoutObservable < string > ; kb : KnockoutObservable < number > ; className : KnockoutObservable < string > ; getProgress : KnockoutComputed < string > ; } param { string } fileName param { number } bytes export class FileUpload implements IFileUpload { public label : KnockoutObservable < string > ; public progress : KnockoutObservable < number > ; public fileName : KnockoutObservable < string > ; public kb : KnockoutObservable < number > ; public className : KnockoutObservable < string > ; public getProgress : KnockoutComputed < string > ; constructor ( fileName , bytes ) { var self = this ; this . label = ko . observable ( null ) ; this . progress = ko . observable ( 0 ) ; this . fileName = ko . observable ( fileName ) ; this . kb = ko . observable ( ( bytes / 1024 ) ) ; this . className = ko . observable ( 'progress-bar progress-bar-info progress-bar-striped active' ) ; this . getProgress = ko . pureComputed ( function ( ) { return self . fileName ( ) + ' ' + self . progress ( ) + '%' ; } , this ) ; } } /**
     * Date Time Model Interface
     */ export interface IDateTimeModel { $element : JQuery ; $parent : JQuery ; $hh : JQuery ; $mm : JQuery ; $tt : JQuery ; $display : JQuery ; $error : JQuery ; $reset : JQuery ; required : Boolean ; koName : string ; setModelValue : Function ; setDisplayValue : Function ; toString : Function ; } /**
     * Date Time Model Class
     */ export class DateTimeModel implements IDateTimeModel { public $element : JQuery ; public $parent : JQuery ; public $hh : JQuery ; public $mm : JQuery ; public $tt : JQuery ; public $display : JQuery ; public $error : JQuery ; public $reset : JQuery ; public required : Boolean ; public koName : string ; constructor ( element , modelValue ) { var self = this ; var date = ko . unwrap ( modelValue ) ; this . $element = $ ( element ) ; this . $parent = this . $element . parent ( ) ; if ( Utils . isJsonDateTicks ( date ) ) { modelValue ( Utils . parseJsonDate ( date ) ) ; } this . required = this . $element . hasClass ( 'required' ) || this . $element . attr ( 'required' ) != null ; this . $element . attr ( { 'placeholder' : 'MM/DD/YYYY' , 'maxlength' : 10 , 'class' : 'datepicker med form-control' } ) . css ( 'display' , 'inline-block' ) . on ( 'change' , onChange ) . datepicker ( { changeMonth : true , changeYear : true } ) ; if ( this . required ) { this . $element . attr ( 'required' , '' ) ; } this . $element . after ( Templates . getTimeControlsHtml ( ) ) ; this . $display = this . $parent . find ( '.so-datetime-display' ) ; this . $error = this . $parent . find ( '.error' ) ; this . $hh = this . $parent . find ( '.so-select-hours' ) . val ( '12' ) . on ( 'change' , onChange ) ; this . $mm = this . $parent . find ( '.so-select-minutes' ) . val ( '0' ) . on ( 'change' , onChange ) ; this . $tt = this . $parent . find ( '.so-select-tt' ) . val ( 'AM' ) . on ( 'change' , onChange ) ; this . $reset = this . $parent . find ( '.btn.reset' ) . on ( 'click' , function ( ) { try { self . $element . val ( '' ) ; self . $hh . val ( '12' ) ; self . $mm . val ( '0' ) ; self . $tt . val ( 'AM' ) ; self . $display . html ( '' ) ; modelValue ( null ) ; } catch ( e ) { console . warn ( e ) ; } return false ; } ) ; function onChange ( ) { self . setModelValue ( modelValue ) ; } } public setModelValue ( modelValue ) { try { var val = this . $element . val ( ) ; if ( ! ! ! $ . trim ( this . $element . val ( ) ) ) { return ; } var hrs = parseInt ( this . $hh . val ( ) ) ; var min = parseInt ( this . $mm . val ( ) ) ; var tt = this . $tt . val ( ) ; if ( tt == 'PM' && hrs < 12 ) { hrs += 12 ; } else if ( tt == 'AM' && hrs > 11 ) { hrs -= 12 ; } var curDateTime = new Date ( val ) ; curDateTime . setUTCHours ( hrs , min , 0 , 0 ) ; modelValue ( curDateTime ) ; } catch ( e ) { if ( SPForm . DEBUG ) { throw e ; } } } public setDisplayValue ( modelValue ) { try { var date = ko . unwrap ( modelValue ) ; if ( ! ! ! date || date . constructor != Date ) { return ; } var date = ko . unwrap ( modelValue ) ; var hrs = date . getUTCHours ( ) ; var min = date . getUTCMinutes ( ) ; this . $element . val ( ( date . getUTCMonth ( ) + 1 ) + '/' + date . getUTCDate ( ) + '/' + date . getUTCFullYear ( ) ) ; if ( hrs > 12 ) { hrs -= 12 ; this . $tt . val ( 'PM' ) ; } else if ( hrs == 0 ) { hrs = 12 ; this . $tt . val ( 'AM' ) ; } else if ( hrs == 12 ) { this . $tt . val ( 'PM' ) ; } else { this . $tt . val ( 'AM' ) ; } this . $hh . val ( hrs . toString ( ) ) ; this . $mm . val ( min . toString ( ) ) ; this . $display . html ( this . toString ( modelValue ) ) ; } catch ( e ) { if ( SPForm . DEBUG ) { throw e ; } } } public toString ( modelValue ) { return DateTimeModel . toString ( modelValue ) ; } public static toString ( modelValue ) { try { var date = ko . unwrap ( modelValue ) ; if ( date == null || date . constructor != Date ) { return ; } var dateTimeStr = date . toLocaleDateString ( ) + ' ' + date . toLocaleTimeString ( ) ; var timeZone = /\b\s\(\w+\s\w+\s\w+\)/i . exec ( date . toString ( ) ) ; if ( ! ! timeZone ) { dateTimeStr += ' ' + timeZone [ 0 ] . replace ( /\b\w+/g , function ( x ) { return x [ 0 ] ; } ) . replace ( /\s/g , '' ) ; } return dateTimeStr ; } catch ( e ) { if ( SPForm . DEBUG ) { throw e ; } } return null ; } } }