123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // KMHeaderFooterPropertyController.swift
- // PDF Master
- //
- // Created by tangchao on 2022/12/27.
- //
- import Cocoa
- typealias KMHeaderFooterPropertyTemplateDidSelected = (_ model: KMHeaderFooterModel,_ isSelected: Bool)->()
- typealias KMHeaderFooterPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMHeaderFooterModel)->()
- /// 页眉页脚 属性控制器
- class KMHeaderFooterPropertyController: KMWatermarkAdjectivePropertyBaseController {
-
- var templateDidSelected: KMHeaderFooterPropertyTemplateDidSelected!
- var menuItemClick: KMHeaderFooterPropertyTemplateMenuItemClick!
-
- var controller = KMHeaderFooterListController()
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self.showBack(show: false)
- self.titleLabel.stringValue = NSLocalizedString("HeaderAndFooter", 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: KMHeaderFooterManager.defaultManager.datas)
- }
-
- func deleteTemplateModel(_ templateModel: KMHeaderFooterModel) -> Bool {
- return KMHeaderFooterManager.defaultManager.deleteTemplate(templateModel)
- }
-
- func deleteTemplateModel(in controller: KMHeaderFooterListController, index: Int) {
- if (index >= controller.datas.count) {
- return
- }
-
- let templateModel: KMHeaderFooterModel = controller.datas[index] as! KMHeaderFooterModel
- if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据
- reloadDatas()
- }
- }
-
- func deleteAllTemplateModel(in controller: KMHeaderFooterListController) {
- if (KMHeaderFooterManager.defaultManager.deleteAllTemplate()) {
- reloadDatas()
- }
- }
-
- func findTemplateModel(in controller: KMHeaderFooterListController, index: Int) -> KMHeaderFooterModel? {
- if (index >= controller.datas.count) {
- return nil
- }
-
- return (controller.datas[index] as! KMHeaderFooterModel)
- }
- }
- extension KMHeaderFooterPropertyController: 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)!)
- }
- }
|