KMHeaderFooterPropertyController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // KMHeaderFooterPropertyController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/27.
  6. //
  7. import Cocoa
  8. typealias KMHeaderFooterPropertyTemplateDidSelected = (_ model: KMHeaderFooterModel,_ isSelected: Bool)->()
  9. typealias KMHeaderFooterPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMHeaderFooterModel)->()
  10. /// 页眉页脚 属性控制器
  11. class KMHeaderFooterPropertyController: KMWatermarkAdjectivePropertyBaseController {
  12. var templateDidSelected: KMHeaderFooterPropertyTemplateDidSelected!
  13. var menuItemClick: KMHeaderFooterPropertyTemplateMenuItemClick!
  14. var controller = KMHeaderFooterListController()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. self.showBack(show: false)
  18. self.titleLabel.stringValue = NSLocalizedString("HeaderAndFooter", comment: "")
  19. self.segementControl.isHidden = true
  20. self.segementControlHeightConst.constant = 0
  21. let item = NSTabViewItem(identifier: "tabID")
  22. item.label = "tab1";
  23. let controller = self.controller
  24. item.viewController = controller
  25. controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new header & footer.", comment: "")
  26. controller.delegate = self
  27. self.tabView.addTabViewItem(item)
  28. childViewController.append(controller)
  29. reloadDatas()
  30. }
  31. func reloadDatas () {
  32. let controller: KMHeaderFooterListController = childViewController[0] as! KMHeaderFooterListController
  33. controller.initDatas(datas: KMHeaderFooterManager.defaultManager.datas)
  34. }
  35. func deleteTemplateModel(_ templateModel: KMHeaderFooterModel) -> Bool {
  36. return KMHeaderFooterManager.defaultManager.deleteTemplate(templateModel)
  37. }
  38. func deleteTemplateModel(in controller: KMHeaderFooterListController, index: Int) {
  39. if (index >= controller.datas.count) {
  40. return
  41. }
  42. let templateModel: KMHeaderFooterModel = controller.datas[index] as! KMHeaderFooterModel
  43. if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据
  44. reloadDatas()
  45. }
  46. }
  47. func deleteAllTemplateModel(in controller: KMHeaderFooterListController) {
  48. if (KMHeaderFooterManager.defaultManager.deleteAllTemplate()) {
  49. reloadDatas()
  50. }
  51. }
  52. func findTemplateModel(in controller: KMHeaderFooterListController, index: Int) -> KMHeaderFooterModel? {
  53. if (index >= controller.datas.count) {
  54. return nil
  55. }
  56. return (controller.datas[index] as! KMHeaderFooterModel)
  57. }
  58. }
  59. extension KMHeaderFooterPropertyController: KMWatermarkAdjectiveListControllerDelegate {
  60. func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool {
  61. return true
  62. }
  63. func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) {
  64. guard let callback = templateDidSelected else {
  65. return
  66. }
  67. let isSelected = controller.selectedTemplateID.isEmpty == false
  68. callback(findTemplateModel(in: controller as! KMHeaderFooterListController, index: didSelectAtRow)!, isSelected)
  69. }
  70. func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) {
  71. deleteTemplateModel(in: controller as! KMHeaderFooterListController, index: didRemoveAtRow)
  72. }
  73. func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) {
  74. guard let callback = menuItemClick else {
  75. return
  76. }
  77. callback(2, findTemplateModel(in: controller as! KMHeaderFooterListController, index: didEditAtRow)!)
  78. }
  79. func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) {
  80. if (itemIndex == 1) { /// 删除
  81. deleteTemplateModel(in: controller as! KMHeaderFooterListController, index: item.tag)
  82. return
  83. }
  84. if (itemIndex == 2) { /// 删除全部
  85. let alert = NSAlert()
  86. alert.messageText = "确认删除全部模板吗?"
  87. alert.addButton(withTitle: "删除")
  88. alert.addButton(withTitle: "取消")
  89. let result = alert.runModal()
  90. if (result == .alertSecondButtonReturn) { /// 取消
  91. return
  92. }
  93. deleteAllTemplateModel(in: controller as! KMHeaderFooterListController)
  94. return
  95. }
  96. /// 编辑
  97. guard let callback = menuItemClick else {
  98. return
  99. }
  100. callback(2, findTemplateModel(in: controller as! KMHeaderFooterListController, index: item.tag)!)
  101. }
  102. }