KMHeaderFooterPropertyController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // KMHeaderFooterPropertyController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/27.
  6. //
  7. import Cocoa
  8. typealias KMHeaderFooterPropertyTemplateDidSelected = (_ model: KMHeaderFooterObject,_ isSelected: Bool)->()
  9. typealias KMHeaderFooterPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMHeaderFooterObject)->()
  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.headFooterObjects)
  34. }
  35. func deleteTemplateModel(_ templateModel: KMHeaderFooterObject) -> Bool {
  36. return KMHeaderFooterManager.defaultManager.removeHeaderFooter(templateModel)
  37. }
  38. func deleteTemplateModel(in controller: KMHeaderFooterListController, index: Int) {
  39. if (index >= controller.datas.count) {
  40. return
  41. }
  42. let templateModel: KMHeaderFooterObject = controller.datas[index] as! KMHeaderFooterObject
  43. if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据
  44. reloadDatas()
  45. }
  46. }
  47. func deleteAllTemplateModel(in controller: KMHeaderFooterListController) {
  48. KMHeaderFooterManager.defaultManager.removeAllHeaderFooter()
  49. reloadDatas()
  50. }
  51. func findTemplateModel(in controller: KMHeaderFooterListController, index: Int) -> KMHeaderFooterObject? {
  52. if (index >= controller.datas.count) {
  53. return nil
  54. }
  55. return (controller.datas[index] as! KMHeaderFooterObject)
  56. }
  57. }
  58. extension KMHeaderFooterPropertyController: KMWatermarkAdjectiveListControllerDelegate {
  59. func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool {
  60. return true
  61. }
  62. func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) {
  63. guard let callback = templateDidSelected else {
  64. return
  65. }
  66. let isSelected = controller.selectedTemplateID.isEmpty == false
  67. callback(findTemplateModel(in: controller as! KMHeaderFooterListController, index: didSelectAtRow)!, isSelected)
  68. }
  69. func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) {
  70. deleteTemplateModel(in: controller as! KMHeaderFooterListController, index: didRemoveAtRow)
  71. }
  72. func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) {
  73. guard let callback = menuItemClick else {
  74. return
  75. }
  76. callback(2, findTemplateModel(in: controller as! KMHeaderFooterListController, index: didEditAtRow)!)
  77. }
  78. func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) {
  79. if (itemIndex == 1) { /// 删除
  80. deleteTemplateModel(in: controller as! KMHeaderFooterListController, index: item.tag)
  81. return
  82. }
  83. if (itemIndex == 2) { /// 删除全部
  84. let alert = NSAlert()
  85. alert.messageText = "确认删除全部模板吗?"
  86. alert.addButton(withTitle: "删除")
  87. alert.addButton(withTitle: "取消")
  88. let result = alert.runModal()
  89. if (result == .alertSecondButtonReturn) { /// 取消
  90. return
  91. }
  92. deleteAllTemplateModel(in: controller as! KMHeaderFooterListController)
  93. return
  94. }
  95. /// 编辑
  96. guard let callback = menuItemClick else {
  97. return
  98. }
  99. callback(2, findTemplateModel(in: controller as! KMHeaderFooterListController, index: item.tag)!)
  100. }
  101. }