Scripts/New-FMCollectionExplorer.ps1

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

function New-FMCollectionExplorer {
    param(
        [Parameter(Position=0)]
        $Collection,
        [Parameter(Position=1)]
        $Pipeline,
        $BsonFile
    )
    New-Object PowerShellFar.ObjectExplorer -Property @{
        Data = @{
            Collection = $Collection
            Pipeline = $Pipeline
            BsonFile = $BsonFile
            Source = Get-FMSourceCollection $Collection
            ChangeCount = 0
        }
        FileComparer = [PowerShellFar.FileMetaComparer]'_id'
        AsCreateFile = {FMCollectionExplorer_AsCreateFile @args}
        AsCreatePanel = {FMCollectionExplorer_AsCreatePanel @args}
        AsDeleteFiles = {FMCollectionExplorer_AsDeleteFiles @args}
        AsGetContent = {FMCollectionExplorer_AsGetContent @args}
        AsGetData = {FMCollectionExplorer_AsGetData @args}
        AsSetText = {FMCollectionExplorer_AsSetText @args}
    }
}

function FMCollectionPanel_Escaping {
    # skip not changed
    if ($this.Explorer.Data.ChangeCount -eq 0) {
        return
    }

    # prompt to export
    $r = Show-FarMessage 'Export data to file?' -Caption Export -Buttons YesNoCancel
    if ($r -eq 0) {
        # save, let close
        Save-BsonFile -Collection $this.Explorer.Data.Source
    }
    elseif ($r -ne 1) {
        # do not close
        $_.Ignore = $true
    }
}

function FMCollectionPanel_MenuCreating {
    # save data and reset change counter
    $_.Menu.Items.Add((New-FarItem -Text 'Export data to file' -Click {
        Save-BsonFile -Collection $this.Explorer.Data.Source
        $this.Explorer.Data.ChangeCount = 0
    }))
}

function FMCollectionExplorer_AsCreatePanel($1) {
    $panel = [PowerShellFar.ObjectPanel]$1

    if ($1.Data.BsonFile) {
        $title = [System.IO.Path]::GetFileName($1.Data.BsonFile)
        $panel.add_Escaping({FMCollectionPanel_Escaping})
        $panel.add_MenuCreating({FMCollectionPanel_MenuCreating})
    }
    else {
        $title = $1.Data.Collection.CollectionNamespace.CollectionName
        if ($1.Data.Collection -ne $1.Data.Source) {
            $title = "$title ($($1.Data.Source.CollectionNamespace.CollectionName))"
        }
    }

    if ($1.Data.Pipeline) {
        $panel.Title = $title + ' (aggregate)'
    }
    else {
        $panel.Title = $title
        $panel.PageLimit = 1000
    }

    $1.Data.Panel = $panel
    $panel
}

function FMCollectionExplorer_EditorOpened {
    $this.add_KeyDown({
        if ($_.Key.KeyDown -and $_.Key.Is([FarNet.KeyCode]::F4)) {
            $_.Ignore = $true
            Edit-MongoJsonLine
        }
    })
}

function FMCollectionExplorer_AsCreateFile($1, $2) {
    # edit new json
    $json = ''
    for() {
        $arg = New-Object FarNet.EditTextArgs -Property @{
            Title = 'New document (JSON)'
            Extension = 'js'
            Text = $json
            EditorOpened = {FMCollectionExplorer_EditorOpened}
        }
        $json = $Far.AnyEditor.EditText($arg)
        if (!$json) {
            return
        }
        try {
            $new = [Mdbc.Dictionary]::FromJson($json)
            break
        }
        catch {
            Show-FarMessage $_
        }
    }

    # _id to post
    $new.EnsureId()

    # add document
    try {
        ++$1.Data.ChangeCount
        Add-MdbcData $new -Collection $1.Data.Source
    }
    catch {
        Show-FarMessage $_
        return
    }

    # post dummy file with _id
    $2.PostFile = New-FarFile -Data ([PSCustomObject]@{_id = $new._id})
    $1.Data.Panel.NeedsNewFiles = $true
}

function FMCollectionExplorer_AsDeleteFiles($1, $2) {
    # ask
    if ($2.UI) {
        $text = "$($2.Files.Count) documents(s)"
        if (Show-FarMessage $text Delete YesNo) {return}
    }
    # remove
    try {
        foreach($doc in $2.FilesData) {
            ++$1.Data.ChangeCount
            Remove-MdbcData @{_id = $doc._id} -Collection $1.Data.Source
        }
    }
    catch {
        $2.Result = 'Incomplete'
        if ($2.UI) {Show-FarMessage "$_"}
    }
    $1.Data.Panel.NeedsNewFiles = $true
}

function FMCollectionExplorer_AsGetContent($1, $2) {
    $id = $2.File.Data._id
    if ($null -eq $id) {
        $doc = New-MdbcData $2.File.Data
    }
    else {
        $doc = Get-MdbcData @{_id = $id} -Collection $1.Data.Source
    }

    $2.UseText = $doc.Print()
    $2.UseFileExtension = 'js'
    $2.CanSet = $true
    $2.EditorOpened = {FMCollectionExplorer_EditorOpened}
}

function FMCollectionExplorer_AsGetData($1, $2) {
    if ($2.NewFiles -or !$1.Cache) {
        if ($1.Data.Pipeline) {
            Invoke-MdbcAggregate $1.Data.Pipeline -Collection $1.Data.Collection -As PS
        }
        else {
            Get-MdbcData -Collection $1.Data.Collection -First $2.Limit -Skip $2.Offset -As PS
        }
    }
    else {
        , $1.Cache
    }
}

function FMCollectionExplorer_AsSetText($1, $2) {
    $new = [Mdbc.Dictionary]::FromJson($2.Text)
    $new.EnsureId()

    try {
        ++$1.Data.ChangeCount
        $new | Set-MdbcData -Add -Collection $1.Data.Source
    }
    catch {
        Show-FarMessage $_
        return
    }

    $1.Cache.Clear()
    $Far.Panel.Update($true)
}