Add refreshing

This commit is contained in:
Shadowfacts 2020-02-09 21:13:22 -05:00
parent 3c4d07093d
commit feeb4e0370
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 60 additions and 17 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="16085" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="16085"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
@ -107,7 +107,8 @@
<action selector="saveDocumentAs:" target="-1" id="mDf-zr-I0C"/>
</connections>
</menuItem>
<menuItem title="Revert to Saved" keyEquivalent="r" id="KaW-ft-85H">
<menuItem title="Revert to Saved" id="KaW-ft-85H">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="revertDocumentToSaved:" target="-1" id="iJ3-Pv-kwq"/>
</connections>
@ -362,6 +363,11 @@
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Database" id="nD3-Xo-LUZ">
<items>
<menuItem title="Refresh" keyEquivalent="r" id="kpI-5J-eaC">
<connections>
<action selector="refresh:" target="-1" id="S5L-sX-Z2j"/>
</connections>
</menuItem>
<menuItem title="Run Query" id="RSP-nH-a1I">
<string key="keyEquivalent" base64-UTF8="YES">
DQ

View File

@ -34,4 +34,12 @@ class MongoController {
client = try! MongoClient(connectionString, using: group)
}
func db(for collection: DatabaseCollection) -> MongoDatabase {
return client.db(collection.database)
}
func collection(_ collection: DatabaseCollection) -> MongoCollection<Document> {
return db(for: collection).collection(collection.name)
}
}

View File

@ -150,10 +150,11 @@ class DatabaseViewController: NSViewController {
}
@IBAction func runQuery(_ sender: Any) {
guard let queryViewController = queryViewController else {
return
queryViewController?.runQuery()
}
queryViewController.runQuery()
@IBAction func refresh(_ sender: Any) {
queryViewController?.refresh()
}
}

View File

@ -27,6 +27,8 @@ class QueryViewController: NSViewController {
return queryTextView.string != defaultQuery
}
var mostRecentQuery: String? = nil
var rootNodes: [Node] = []
init(mongoController: MongoController, collection: DatabaseCollection) {
@ -43,10 +45,7 @@ class QueryViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let db = mongoController.client.db(collection.database)
let collection = db.collection(self.collection.name)
let documents = try! collection.find().all()
rootNodes = documents.map { Node(document: $0) }
refresh()
verticalSplitView.delegate = self
verticalSplitView.setHoldingPriority(.defaultHigh, forSubviewAt: 0)
@ -61,9 +60,6 @@ class QueryViewController: NSViewController {
outlineView.target = self
outlineView.doubleAction = #selector(outlineCellDoubleClicked)
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
}
override func viewWillAppear() {
@ -80,10 +76,33 @@ class QueryViewController: NSViewController {
view.window!.makeFirstResponder(outlineView)
}
func runQuery() {
let query = queryTextView.string
func refresh(reload: Bool = true) {
if let query = mostRecentQuery {
let connStr = "\(mongoController.connectionString)/\(collection.database)"
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(value: $0)
}
title = query
documentCountLabel.stringValue = "\(rootNodes.count) result\(rootNodes.count == 1 ? "" : "s")"
} else {
let documents = try! mongoController.collection(collection).find().all()
rootNodes = documents.map { Node(document: $0) }
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
}
if reload {
outlineView.reloadData()
}
}
func runQuery() {
mostRecentQuery = queryTextView.string
refresh()
}
rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
Node(value: $0)
}
@ -102,6 +121,15 @@ class QueryViewController: NSViewController {
}
extension QueryViewController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.action == #selector(deleteNode(_:)) {
return outlineView.clickedRow != -1
}
return true
}
}
extension QueryViewController: NSSplitViewDelegate {
func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat {
return max(80, min(splitView.bounds.height / 2, proposedPosition))