// // KMBatesPropertyController.swift // PDF Master // // Created by tangchao on 2022/12/28. // import Cocoa typealias KMBatesPropertyTemplateDidSelected = (_ model: KMBatesModel,_ isSelected: Bool)->() typealias KMBatesPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMBatesModel)->() class KMBatesPropertyController: KMWatermarkAdjectivePropertyBaseController { var templateDidSelected: KMBatesPropertyTemplateDidSelected! var menuItemClick: KMBatesPropertyTemplateMenuItemClick! var controller = KMHeaderFooterListController() override func viewDidLoad() { super.viewDidLoad() self.showBack(show: false) self.titleLabel.stringValue = NSLocalizedString("Bates Number", comment: "") self.segementControl.isHidden = true self.segementControlHeightConst.constant = 0 let item = NSTabViewItem(identifier: "tabID") item.label = "tab1"; let controller = self.controller item.viewController = controller controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new header & footer.", comment: "") controller.delegate = self self.tabView.addTabViewItem(item) childViewController.append(controller) reloadDatas() } func reloadDatas () { let controller: KMHeaderFooterListController = childViewController[0] as! KMHeaderFooterListController controller.initDatas(datas: KMBatesManager.defaultManager.datas) } func deleteTemplateModel(_ templateModel: KMBatesModel) -> Bool { return KMBatesManager.defaultManager.deleteTemplate(templateModel) } func deleteTemplateModel(in controller: KMHeaderFooterListController, index: Int) { if (index >= controller.datas.count) { return } let templateModel: KMBatesModel = controller.datas[index] as! KMBatesModel if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据 reloadDatas() } } func deleteAllTemplateModel(in controller: KMHeaderFooterListController) { if (KMBatesManager.defaultManager.deleteAllTemplate()) { reloadDatas() } } func findTemplateModel(in controller: KMHeaderFooterListController, index: Int) -> KMBatesModel? { if (index >= controller.datas.count) { return nil } return (controller.datas[index] as! KMBatesModel) } } extension KMBatesPropertyController: KMWatermarkAdjectiveListControllerDelegate { func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool { return true } func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) { guard let callback = templateDidSelected else { return } let isSelected = controller.selectedTemplateID.isEmpty == false callback(findTemplateModel(in: controller as! KMHeaderFooterListController, index: didSelectAtRow)!, isSelected) } func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) { deleteTemplateModel(in: controller as! KMHeaderFooterListController, index: didRemoveAtRow) } func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) { guard let callback = menuItemClick else { return } callback(2, findTemplateModel(in: controller as! KMHeaderFooterListController, index: didEditAtRow)!) } func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) { if (itemIndex == 1) { /// 删除 deleteTemplateModel(in: controller as! KMHeaderFooterListController, index: item.tag) return } if (itemIndex == 2) { /// 删除全部 let alert = NSAlert() alert.messageText = "确认删除全部模板吗?" alert.addButton(withTitle: "删除") alert.addButton(withTitle: "取消") let result = alert.runModal() if (result == .alertSecondButtonReturn) { /// 取消 return } deleteAllTemplateModel(in: controller as! KMHeaderFooterListController) return } /// 编辑 guard let callback = menuItemClick else { return } callback(2, findTemplateModel(in: controller as! KMHeaderFooterListController, index: item.tag)!) } }