Browse Source

Add document deletion

master
Shadowfacts 2 weeks ago
parent
commit
e54c2a0570
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5

+ 5
- 3
MongoView/Node.swift View File

@@ -12,16 +12,17 @@ import MongoSwift
12 12
 class Node {
13 13
     let key: Key?
14 14
     let value: BSON
15
+    weak var parent: Node?
15 16
     
16 17
     lazy private(set) var children: [Node] = {
17 18
         switch value {
18 19
         case let .array(array):
19 20
             return array.enumerated().map { (index, val) in
20
-                Node(key: .index(index), value: val)
21
+                Node(key: .index(index), value: val, parent: self)
21 22
             }
22 23
         case let .document(doc):
23 24
             return doc.map { (key, val) in
24
-                Node(key: .name(key), value: val)
25
+                Node(key: .name(key), value: val, parent: self)
25 26
             }
26 27
         default:
27 28
             return []
@@ -36,8 +37,9 @@ class Node {
36 37
         numberOfChildren > 0
37 38
     }
38 39
 
39
-    init(key: Key? = nil, value: BSON) {
40
+    init(key: Key? = nil, value: BSON, parent: Node? = nil) {
40 41
         self.value = value
42
+        self.parent = parent
41 43
         
42 44
         if key == nil,
43 45
             case let .document(doc) = value,

+ 52
- 3
MongoView/View Controllers/QueryViewController.swift View File

@@ -102,11 +102,51 @@ class QueryViewController: NSViewController {
102 102
         mostRecentQuery = queryTextView.string
103 103
         refresh()
104 104
     }
105
+    
106
+    func deleteRootNode(_ node: Node) {
107
+        guard case let .document(doc) = node.value else { return }
108
+        let alert = NSAlert()
109
+        alert.alertStyle = .warning
110
+        alert.messageText = "Confirm deletion"
111
+        alert.informativeText = "Are you sure you want to delete the document"
112
+        let id: ObjectId?
113
+        if case let .objectId(docId) = doc["_id"] {
114
+            id = docId
115
+            alert.informativeText += " with id \(docId)"
116
+        } else {
117
+            id = nil
118
+        }
105 119
 
106
-        rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
107
-            Node(value: $0)
120
+        alert.addButton(withTitle: "Delete")
121
+        alert.addButton(withTitle: "Cancel")
122
+        
123
+        alert.beginSheetModal(for: view.window!) { (response) in
124
+            guard response == .alertFirstButtonReturn else { return }
125
+            
126
+            self.mongoController.collection(self.collection).deleteOne(doc).whenComplete { (result) in
127
+                DispatchQueue.main.async {
128
+                    switch result {
129
+                    case let .success(result):
130
+                        guard let result = result, result.deletedCount == 1 else {
131
+                            let alert = NSAlert()
132
+                            alert.alertStyle = .critical
133
+                            alert.messageText = "Error deleting document"
134
+                            if let id = id {
135
+                                alert.informativeText = "The document with id \(id) could not be deleted."
136
+                            } else {
137
+                                alert.informativeText = "The document could not be deleted."
138
+                            }
139
+                            alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
140
+                            return
141
+                        }
142
+                        self.refresh()
143
+                    case let .failure(error):
144
+                        let alert = NSAlert(error: error)
145
+                        alert.beginSheetModal(for: self.view.window!, completionHandler: nil)
146
+                    }
147
+                }
148
+            }
108 149
         }
109
-        outlineView.reloadData()
110 150
     }
111 151
     
112 152
     @objc func outlineCellDoubleClicked() {
@@ -118,6 +158,15 @@ class QueryViewController: NSViewController {
118 158
             }
119 159
         }
120 160
     }
161
+
162
+    @IBAction func deleteNode(_ sender: Any) {
163
+        guard let node = outlineView.item(atRow: outlineView.clickedRow) as? Node else {
164
+            return
165
+        }
166
+        if node.parent == nil {
167
+            deleteRootNode(node)
168
+        }
169
+    }
121 170
     
122 171
 }
123 172
 

+ 32
- 21
MongoView/View Controllers/QueryViewController.xib View File

@@ -1,7 +1,7 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
2
+<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16085" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
3 3
     <dependencies>
4
-        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
4
+        <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16085"/>
5 5
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
6 6
     </dependencies>
7 7
     <objects>
@@ -994,19 +994,18 @@
994 994
                             <rect key="frame" x="0.0" y="96" width="741" height="371"/>
995 995
                             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
996 996
                             <clipView key="contentView" id="eia-Pq-LIB">
997
-                                <rect key="frame" x="1" y="0.0" width="739" height="370"/>
997
+                                <rect key="frame" x="1" y="0.0" width="724" height="355"/>
998 998
                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
999 999
                                 <subviews>
1000 1000
                                     <outlineView verticalHuggingPriority="750" allowsExpansionToolTips="YES" alternatingRowBackgroundColors="YES" columnReordering="NO" multipleSelection="NO" autosaveName="QueryOutline" rowSizeStyle="automatic" headerView="jah-QY-QbI" viewBased="YES" indentationPerLevel="16" outlineTableColumn="emg-oX-7XY" id="uu9-9q-MWr">
1001
-                                        <rect key="frame" x="0.0" y="0.0" width="739" height="345"/>
1001
+                                        <rect key="frame" x="0.0" y="0.0" width="724" height="330"/>
1002 1002
                                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1003 1003
                                         <size key="intercellSpacing" width="3" height="2"/>
1004 1004
                                         <color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
1005 1005
                                         <color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
1006 1006
                                         <tableColumns>
1007
-                                            <tableColumn identifier="FieldNameCol" width="339" minWidth="40" maxWidth="1000" id="emg-oX-7XY">
1007
+                                            <tableColumn identifier="FieldNameCol" width="334" minWidth="40" maxWidth="1000" id="emg-oX-7XY">
1008 1008
                                                 <tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Field Name">
1009
-                                                    <font key="font" metaFont="controlContent" size="11"/>
1010 1009
                                                     <color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
1011 1010
                                                     <color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
1012 1011
                                                 </tableHeaderCell>
@@ -1018,11 +1017,11 @@
1018 1017
                                                 <tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
1019 1018
                                                 <prototypeCellViews>
1020 1019
                                                     <tableCellView identifier="FieldNameCell" id="8dc-a6-0k5">
1021
-                                                        <rect key="frame" x="1" y="1" width="339" height="17"/>
1020
+                                                        <rect key="frame" x="1" y="1" width="334" height="17"/>
1022 1021
                                                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1023 1022
                                                         <subviews>
1024 1023
                                                             <textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="qEd-h6-M2A">
1025
-                                                                <rect key="frame" x="0.0" y="0.0" width="339" height="17"/>
1024
+                                                                <rect key="frame" x="0.0" y="0.0" width="334" height="17"/>
1026 1025
                                                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
1027 1026
                                                                 <textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="8JQ-A4-jXh">
1028 1027
                                                                     <font key="font" metaFont="system"/>
@@ -1037,9 +1036,8 @@
1037 1036
                                                     </tableCellView>
1038 1037
                                                 </prototypeCellViews>
1039 1038
                                             </tableColumn>
1040
-                                            <tableColumn identifier="FieldValueCol" width="297" minWidth="40" maxWidth="1000" id="ggz-sp-EaO">
1039
+                                            <tableColumn identifier="FieldValueCol" width="292" minWidth="40" maxWidth="1000" id="ggz-sp-EaO">
1041 1040
                                                 <tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" title="Field Value">
1042
-                                                    <font key="font" metaFont="controlContent" size="11"/>
1043 1041
                                                     <color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
1044 1042
                                                     <color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
1045 1043
                                                 </tableHeaderCell>
@@ -1051,11 +1049,11 @@
1051 1049
                                                 <tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
1052 1050
                                                 <prototypeCellViews>
1053 1051
                                                     <tableCellView identifier="FieldValueCell" id="E6Z-9f-0gg">
1054
-                                                        <rect key="frame" x="343" y="1" width="297" height="17"/>
1052
+                                                        <rect key="frame" x="338" y="1" width="292" height="17"/>
1055 1053
                                                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1056 1054
                                                         <subviews>
1057 1055
                                                             <textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Px2-mb-c5m">
1058
-                                                                <rect key="frame" x="0.0" y="0.0" width="297" height="17"/>
1056
+                                                                <rect key="frame" x="0.0" y="0.0" width="292" height="17"/>
1059 1057
                                                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
1060 1058
                                                                 <textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="rED-77-VFk">
1061 1059
                                                                     <font key="font" metaFont="system"/>
@@ -1070,9 +1068,8 @@
1070 1068
                                                     </tableCellView>
1071 1069
                                                 </prototypeCellViews>
1072 1070
                                             </tableColumn>
1073
-                                            <tableColumn identifier="ValueTypeCol" width="94" minWidth="10" maxWidth="3.4028234663852886e+38" id="Tow-Y6-pc7">
1071
+                                            <tableColumn identifier="ValueTypeCol" width="89" minWidth="10" maxWidth="3.4028234663852886e+38" id="Tow-Y6-pc7">
1074 1072
                                                 <tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border" alignment="left" title="Value Type">
1075
-                                                    <font key="font" metaFont="controlContent" size="11"/>
1076 1073
                                                     <color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
1077 1074
                                                     <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
1078 1075
                                                 </tableHeaderCell>
@@ -1084,11 +1081,11 @@
1084 1081
                                                 <tableColumnResizingMask key="resizingMask" resizeWithTable="YES" userResizable="YES"/>
1085 1082
                                                 <prototypeCellViews>
1086 1083
                                                     <tableCellView identifier="ValueTypeCell" id="tUj-Jd-Rj1">
1087
-                                                        <rect key="frame" x="643" y="1" width="94" height="17"/>
1084
+                                                        <rect key="frame" x="633" y="1" width="89" height="17"/>
1088 1085
                                                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1089 1086
                                                         <subviews>
1090 1087
                                                             <textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="mBU-Qs-G4S">
1091
-                                                                <rect key="frame" x="0.0" y="0.0" width="94" height="17"/>
1088
+                                                                <rect key="frame" x="0.0" y="0.0" width="89" height="17"/>
1092 1089
                                                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
1093 1090
                                                                 <textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="tmE-QF-vUZ">
1094 1091
                                                                     <font key="font" metaFont="system"/>
@@ -1107,16 +1104,16 @@
1107 1104
                                     </outlineView>
1108 1105
                                 </subviews>
1109 1106
                             </clipView>
1110
-                            <scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="WBg-uK-bLa">
1111
-                                <rect key="frame" x="1" y="119" width="223" height="15"/>
1107
+                            <scroller key="horizontalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="WBg-uK-bLa">
1108
+                                <rect key="frame" x="1" y="355" width="724" height="15"/>
1112 1109
                                 <autoresizingMask key="autoresizingMask"/>
1113 1110
                             </scroller>
1114
-                            <scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="Sj7-M1-no8">
1115
-                                <rect key="frame" x="224" y="17" width="15" height="102"/>
1111
+                            <scroller key="verticalScroller" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="Sj7-M1-no8">
1112
+                                <rect key="frame" x="725" y="25" width="15" height="330"/>
1116 1113
                                 <autoresizingMask key="autoresizingMask"/>
1117 1114
                             </scroller>
1118 1115
                             <tableHeaderView key="headerView" id="jah-QY-QbI">
1119
-                                <rect key="frame" x="0.0" y="0.0" width="739" height="25"/>
1116
+                                <rect key="frame" x="0.0" y="0.0" width="724" height="25"/>
1120 1117
                                 <autoresizingMask key="autoresizingMask"/>
1121 1118
                             </tableHeaderView>
1122 1119
                         </scrollView>
@@ -1135,7 +1132,21 @@
1135 1132
                 <constraint firstAttribute="trailing" secondItem="E3D-CM-hk3" secondAttribute="trailing" id="pjR-Xa-8co"/>
1136 1133
                 <constraint firstItem="E3D-CM-hk3" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" id="sR0-qd-Vco"/>
1137 1134
             </constraints>
1135
+            <connections>
1136
+                <outlet property="menu" destination="nL5-kg-dty" id="KQ4-CJ-Hz4"/>
1137
+            </connections>
1138 1138
             <point key="canvasLocation" x="271.5" y="262.5"/>
1139 1139
         </customView>
1140
+        <menu id="nL5-kg-dty">
1141
+            <items>
1142
+                <menuItem title="Delete" id="nn0-ZF-eDZ">
1143
+                    <modifierMask key="keyEquivalentModifierMask"/>
1144
+                    <connections>
1145
+                        <action selector="deleteNode:" target="-2" id="dgm-SC-hn5"/>
1146
+                    </connections>
1147
+                </menuItem>
1148
+            </items>
1149
+            <point key="canvasLocation" x="887" y="210"/>
1150
+        </menu>
1140 1151
     </objects>
1141 1152
 </document>

Loading…
Cancel
Save