KMBatesPropertyController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // KMBatesPropertyController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/28.
  6. //
  7. import Cocoa
  8. typealias KMBatesPropertyTemplateDidSelected = (_ model: KMBatesModel,_ isSelected: Bool)->()
  9. typealias KMBatesPropertyTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMBatesModel)->()
  10. class KMBatesPropertyController: KMWatermarkAdjectivePropertyBaseController {
  11. var templateDidSelected: KMBatesPropertyTemplateDidSelected!
  12. var menuItemClick: KMBatesPropertyTemplateMenuItemClick!
  13. var controller = KMHeaderFooterListController()
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. self.showBack(show: false)
  17. self.titleLabel.stringValue = NSLocalizedString("Bates Number", comment: "")
  18. self.segementControl.isHidden = true
  19. self.segementControlHeightConst.constant = 0
  20. let item = NSTabViewItem(identifier: "tabID")
  21. item.label = "tab1";
  22. let controller = self.controller
  23. item.viewController = controller
  24. controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new header & footer.", comment: "")
  25. controller.delegate = self
  26. self.tabView.addTabViewItem(item)
  27. childViewController.append(controller)
  28. reloadDatas()
  29. }
  30. func reloadDatas () {
  31. let controller: KMHeaderFooterListController = childViewController[0] as! KMHeaderFooterListController
  32. controller.initDatas(datas: KMBatesManager.defaultManager.datas)
  33. }
  34. func deleteTemplateModel(_ templateModel: KMBatesModel) -> Bool {
  35. return KMBatesManager.defaultManager.deleteTemplate(templateModel)
  36. }
  37. func deleteTemplateModel(in controller: KMHeaderFooterListController, index: Int) {
  38. if (index >= controller.datas.count) {
  39. return
  40. }
  41. let templateModel: KMBatesModel = controller.datas[index] as! KMBatesModel
  42. if (deleteTemplateModel(templateModel)) { /// 删除成功,刷新数据
  43. reloadDatas()
  44. }
  45. }
  46. func deleteAllTemplateModel(in controller: KMHeaderFooterListController) {
  47. if (KMBatesManager.defaultManager.deleteAllTemplate()) {
  48. reloadDatas()
  49. }
  50. }
  51. func findTemplateModel(in controller: KMHeaderFooterListController, index: Int) -> KMBatesModel? {
  52. if (index >= controller.datas.count) {
  53. return nil
  54. }
  55. return (controller.datas[index] as! KMBatesModel)
  56. }
  57. }
  58. extension KMBatesPropertyController: 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. }