All files / src/navigationcontrols Tree.ts

85.45% Statements 94/110
65.79% Branches 50/76
78.95% Functions 30/38
88.3% Lines 83/94
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260          1x 1x   1x                   1x     17x     1x 48x       17x           18x           18x             18x             1x           18x             1x           1x 4x 3x 1x   3x   1x       1x 54x             18x 4x 4x 4x   4x             2x 2x 2x 2x   2x   1x 1x                 1x             1x             1x       5x         5x       18x   18x 7x     11x 11x 11x 11x         1x 1x 1x         1x 1x 1x         1x 1x         5x   5x 1x 1x     4x 3x 3x 3x 3x 1x     1x 1x   1x     3x     1x       35x 4x   35x       17x       17x 1x 1x         17x                     17x           1x 1x 1x   1x         1x        
/**
 * @module NavigationControls
 * 
 */ /** */
 
import { bindable, computedFrom, autoinject } from 'aurelia-framework';
import { Content, Repository } from 'sn-client-js';
import { Subscription } from 'rxjs/Subscription';
import { Observable } from 'rxjs/Observable';
 
/**
 * A tree with bindable Root content and Selection.
 * Usage example: 
 * ``` html
 * <tree content.bind="RootContent" selection.two-way="SelectionContent"></tree>
 * ```
 */
@autoinject
export class Tree {
 
 
    Subscriptions: Subscription[] = [];
 
    @computedFrom('Content')
    get Repository(): Repository.BaseRepository {
        return this.Content.GetRepository();
    };
 
    @bindable
    public NestingLevel: number = 0;
 
    /**
     * Property that represents if the tree is expanded or not
     */
    @bindable
    IsExpanded: boolean = false;
 
    /**
     * Property that represents if the tree is currently loading it's child content
     */
    @bindable
    IsLoading: boolean = false;
 
    /**
     * Property that represents of the tree has children or not
     */
 
    @bindable
    HasChildren: boolean = true;
 
    /**
     * The root level Content instance of the tree
     */
 
    @bindable
    Content: Content;
 
    /**
     * List of the child content (will be reloaded on each expand)
     */
    @bindable
    Children: Content[] = [];
 
 
    /**
     * An optional Content that represents the current Selection for the Tree. Used for auto-expand and Selection binding
     */
    @bindable
    Selection: Content;
 
    /**
     * indicates if the current level Content is selected in the Tree. This getter is also used for auto-expand
     */
    @computedFrom('Selection', 'Content')
    get IsSelected() {
        if (this.Selection && this.Selection.Path && this.Content && this.Content.Path) {
            if (!this.IsExpanded && this.Selection.Path.indexOf(this.Content.Path + '/') === 0 && this.Selection.Path !== this.Content.Path) {
                this.Expand();
            }
            return this.Selection.Path === this.Content.Path;
        }
        return false;
    }
 
    @computedFrom('Content')
    get hasValidContent(): boolean {
        return this.Content && this.Content.SavedFields && Object.keys(this.Content.SavedFields).length > 0;
    }
 
    /**
     * Triggers an Expand on the current level (also used to load Children content)
     */
    @bindable
    Expand: () => Promise<void> = () => {
        return new Promise<void>((resolve, reject) => {
            this.IsExpanded = true;
            this.IsLoading = true;
 
            this.Content.Children({
                metadata: 'no',
                expand: ['Workspace'],
                select: ['Path', 'Id', 'Name', 'DisplayName', 'Icon', 'IsFolder'],
                orderby: ['DisplayName', 'Name'],
                filter: 'IsFolder'
            }).subscribe(children => {
                setTimeout(() => {
                    this.Children = children;
                    this.ReorderChildren();
                    resolve();
                }, 200)
                this.IsLoading = false;
            }, (err) => {
                this.IsLoading = false;
                reject(err);
            });
        });
    }
 
    /**
     * Triggers a Collapse operation (hide children)
     */
    Collapse() {
        this.IsExpanded = false;
    }
 
    /**
     * Toggles between Expand / Collapse
     */
    Toggle() {
        !this.IsLoading && (!this.IsExpanded ? this.Expand() : this.Collapse());
    }
 
    /**
     * Sets the Selection to the current level Content
     */
    Select() {
        this.Selection = this.Content;
    }
 
    ReorderChildren() {
        this.Children = this.Children.sort((a, b) => {
            let x: string = a['DisplayName'] || a['Name'] || '';
            let y: string = b['DisplayName'] || b['Name'] || '';
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
        this.HasChildren = this.Children.length > 0;
    }
 
    ContentChanged() {
        this.clearSubscriptions();
 
        if (!this.hasValidContent) {
            return;
        }
 
        this.Subscriptions.push(this.Repository.Events.OnContentCreated.subscribe(created => this.handleContentCreated(created)));
        this.Subscriptions.push(this.Repository.Events.OnContentDeleted.subscribe(deleted => this.handleContentDeleted(deleted)));
        this.Subscriptions.push(this.Repository.Events.OnContentModified.subscribe(mod => this.handleContentModified(mod)));
        this.Subscriptions.push(this.Repository.Events.OnContentMoved.subscribe(move => this.handleContentMoved(move)));
 
    }
 
    handleContentCreated(created: Repository.EventModels.Created) {
        Eif (this.IsExpanded && !this.IsLoading && created.Content.IsChildOf(this.Content)) {
            this.Children.push(created.Content)
            this.ReorderChildren();
        }
    }
 
    handleContentDeleted(deleted: Repository.EventModels.Deleted) {
        const child = this.Children.find(c => c.Id === deleted.ContentData.Id);
        Eif (this.IsExpanded && !this.IsLoading && child) {
            this.Children = this.Children.filter(c => c !== child);
        }
    }
 
    handleContentModified(modified: Repository.EventModels.Modified) {
        Eif (this.IsExpanded && !this.IsLoading && this.Children.find(c => c != null && c.Id === modified.Content.Id)) {
            this.ReorderChildren();
        }
    }
 
    handleContentMoved(moved: Repository.EventModels.ContentMoved): Observable<Content> {
        const child = this.Children.find(c => c.Id === moved.Content.Id);
 
        if (this.IsExpanded && child) {
            this.Children = this.Children.filter(c => c !== child);
            return Observable.of(child);
        }
 
        if (moved.To === this.Content.Path) {
            Eif (this.IsExpanded) {
                this.IsLoading = true;
                const request = this.Repository.Load(moved.Content.Id || moved.Content.Path || '', { select: 'all' });
                request.subscribe(c => {
                    Iif (c.IsFolder){
                        this.Children.push(c);
                    }
                    this.IsLoading = false;
                    this.ReorderChildren();
                }, err => {
                    this.IsLoading = false;
                })
 
                return request;
            }
        }
        return Observable.of(moved.Content);
    }
 
    clearSubscriptions() {
        this.Subscriptions.forEach(subscription => {
            subscription.unsubscribe();
        });
        this.Subscriptions = [];
    }
 
    detached() {
        this.clearSubscriptions();
    }
 
    @bindable 
    public OnDropContent: (params: {event: DragEvent, content: Content}) => void = (params: {content: Content}) => {
        Eif (this.Content.Path && this.Content.Path !== params.content.ParentPath && this.Content.Path !== params.content.Path && !params.content.IsAncestorOf(this.Content)) {
            params.content.MoveTo(this.Content.Path);
        }
    }
 
    @bindable
    public OnDropContentList: (params: {event: DragEvent, contentList: Content[]}) => void = (params) => {
        if (this.Content.Path){
            if (params.event.ctrlKey){
                this.Repository.CopyBatch(params.contentList, this.Content.Path);
            } else {
                this.Repository.MoveBatch(params.contentList, this.Content.Path);
            }
        }
    };
 
    @bindable 
    public OnDropFiles: (params: {event: DragEvent, files: FileList}) => void = (params) => {
 
    };    
 
    async dropContent(event: DragEvent, stringifiedContent: string, stringifiedContentList?: string[], files?: FileList) {
 
        Eif (stringifiedContent){
            const droppedContent = this.Repository.ParseContent(stringifiedContent);
            this.OnDropContent({event, content: droppedContent});
        }
        Iif (stringifiedContentList){
            const contentList = stringifiedContentList.map(c => this.Repository.ParseContent(c))
            this.OnDropContentList({event, contentList});
        }
 
        Iif (files){
            this.OnDropFiles({event, files});
        }
    }
}