KMOutlineViewController.swift 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. //
  2. // KMOutlineViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/10/10.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMOutlineViewController: KMNBaseViewController {
  10. @IBOutlet var contendView: NSView!
  11. @IBOutlet weak var topView: NSView!
  12. @IBOutlet weak var titleLabel: NSTextField!
  13. @IBOutlet weak var lineView: NSView!
  14. @IBOutlet weak var addButton: NSButton!
  15. @IBOutlet weak var moreButton: NSButton!
  16. @IBOutlet var topSepline: NSView!
  17. @IBOutlet weak var emptyView: NSView!
  18. @IBOutlet weak var bigTipLabel: NSTextField!
  19. @IBOutlet weak var tipLabel: NSTextField!
  20. @IBOutlet weak var BOTAOutlineView: KMBOTAOutlineView!
  21. var dragPDFOutline : KMBOTAOutlineItem!
  22. var renameTextField : NSTextField!
  23. // var listView : CPDFListView!
  24. var renamePDFOutline : KMBOTAOutlineItem!
  25. let moreMenu = NSMenu()
  26. var isLocalEvent = false
  27. var handdler = KMNOutlineHanddler()
  28. private weak var popover_: NSPopover?
  29. private var ignoreCase_ = false
  30. private lazy var searchButton_: ComponentButton = {
  31. let view = ComponentButton()
  32. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, state: .normal, isDisable: false, onlyIcon: true)
  33. return view
  34. }()
  35. private lazy var addButton_: ComponentButton = {
  36. let view = ComponentButton()
  37. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, state: .normal, isDisable: false, onlyIcon: true, keepPressState: false)
  38. return view
  39. }()
  40. private lazy var moreDropdown_: ComponentButton = {
  41. let view = ComponentButton()
  42. // view.properties = ComponentDropdownProperty(type: .split_primary, size: .xxs, state: .normal, split: false, disabled: false)
  43. view.properties = ComponentButtonProperty(type: .text_gray, size: .xxs, state: .normal, isDisable: false, onlyIcon: true, icon: NSImage(named: "KMImageNameOutlineMore"))
  44. return view
  45. }()
  46. private lazy var headerSearchView_: KMNBotaHeaderSearchView? = {
  47. let view = KMNBotaHeaderSearchView.createFromNib()
  48. return view
  49. }()
  50. private var emptyView_: ComponentEmpty = {
  51. let view = ComponentEmpty()
  52. view.properties = ComponentEmptyProperty(emptyType: .noOutline, state: .normal, image: NSImage(named: "KMImageNameOutlineEmpty"), text: KMLocalizedString("No Outline"), subText: KMLocalizedString("Here is the description."))
  53. return view
  54. }()
  55. private var groupView_: ComponentGroup?
  56. private var menuGroupView_: ComponentGroup?
  57. deinit {
  58. self.BOTAOutlineView.delegate = nil
  59. }
  60. override func viewWillDisappear() {
  61. super.viewWillDisappear()
  62. self.cancelSelect()
  63. }
  64. override func viewDidLoad() {
  65. super.viewDidLoad()
  66. handdler.delegate = self
  67. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-m-bold")
  68. self.topView.wantsLayer = true
  69. addButton.image = nil
  70. topView.addSubview(searchButton_)
  71. searchButton_.km_add_size_constraint(size: NSMakeSize(24, 24))
  72. searchButton_.km_add_centerY_constraint(constant: 1)
  73. searchButton_.km_add_trailing_constraint(equalTo: addButton, attribute: .leading, constant: -4)
  74. searchButton_.setTarget(self, action: #selector(_searchAction))
  75. addButton.addSubview(addButton_)
  76. addButton_.km_add_size_constraint(size: NSMakeSize(24, 24))
  77. addButton_.km_add_centerX_constraint()
  78. addButton_.km_add_centerY_constraint()
  79. addButton_.setTarget(self, action: #selector(addNewOutline))
  80. moreButton.image = nil
  81. moreButton.addSubview(moreDropdown_)
  82. moreDropdown_.km_add_size_constraint(size: NSMakeSize(24, 24))
  83. moreDropdown_.km_add_centerX_constraint()
  84. moreDropdown_.km_add_centerY_constraint()
  85. moreDropdown_.setTarget(self, action: #selector(_moreAction))
  86. if let data = headerSearchView_ {
  87. topView.addSubview(data)
  88. headerSearchView_?.frame = topView.bounds
  89. headerSearchView_?.autoresizingMask = [.width, .height]
  90. }
  91. _hideHeaderSearch()
  92. headerSearchView_?.itemClick = { [weak self] idx, _ in
  93. if idx == 1 { // 忽略大小写
  94. let ignoreCase = self?.ignoreCase_ ?? false
  95. self?.ignoreCase_ = !ignoreCase
  96. } else if idx == 2 { // 关闭搜索
  97. self?._hideHeaderSearch()
  98. self?.reloadData()
  99. }
  100. }
  101. headerSearchView_?.valueDidChange = { [weak self] sender, info in
  102. let value = info?[.newKey] as? String ?? ""
  103. self?.BOTAOutlineView.searchKey = value
  104. self?.reloadData()
  105. }
  106. emptyView.wantsLayer = true
  107. bigTipLabel.stringValue = ""
  108. tipLabel.stringValue = ""
  109. emptyView.addSubview(emptyView_)
  110. emptyView_.km_add_top_constraint(constant: 232)
  111. emptyView_.km_add_bottom_constraint()
  112. emptyView_.km_add_leading_constraint()
  113. emptyView_.km_add_trailing_constraint()
  114. self.BOTAOutlineView.delegate = self
  115. self.BOTAOutlineView.inputData = self.handdler.outlineRoot()
  116. self.BOTAOutlineView.outlineView.doubleAction = #selector(outlineViewDoubleAction)
  117. }
  118. // To create an outline, please right-click on the selected page and choose \"Add Outline\", or click \"Add\" in the upper right corner.
  119. override func updateUILanguage() {
  120. super.updateUILanguage()
  121. KMMainThreadExecute {
  122. self.titleLabel.stringValue = KMLocalizedString("Outline")
  123. self.moreButton.toolTip = KMLocalizedString("More")
  124. self.addButton.toolTip = KMLocalizedString("Add Outline")
  125. }
  126. }
  127. override func updateUIThemeColor() {
  128. super.updateUIThemeColor()
  129. KMMainThreadExecute {
  130. self.contendView.wantsLayer = true
  131. let color = KMNColorTools.colorBg_layoutMiddle()
  132. self.contendView.layer?.backgroundColor = color.cgColor
  133. self.headerSearchView_?.wantsLayer = true
  134. self.headerSearchView_?.layer?.backgroundColor = color.cgColor
  135. self.headerSearchView_?.bottomLine.wantsLayer = true
  136. self.headerSearchView_?.bottomLine.layer?.backgroundColor = KMNColorTools.colorPrimary_border1().cgColor
  137. self.titleLabel.textColor = KMNColorTools.colorText_2()
  138. self.addButton_.properties.icon = NSImage(named: "KMBookmarkAdd")
  139. self.addButton_.reloadData()
  140. self.searchButton_.properties.icon = NSImage(named: "KMImageNameOutlineSearch")
  141. self.searchButton_.reloadData()
  142. let dividerColor = KMNColorTools.colorBorder_divider()
  143. self.topSepline.wantsLayer = true
  144. self.topSepline.layer?.backgroundColor = dividerColor.cgColor
  145. self.lineView.backgroundColor(dividerColor)
  146. }
  147. }
  148. override func addNotifations() {
  149. super.addNotifations()
  150. NotificationCenter.default.addObserver(self, selector: #selector(KMPDFViewCurrentPageDidChangedNotification), name: NSNotification.Name.init(rawValue: "KMPDFViewCurrentPageDidChanged"), object: nil)
  151. }
  152. func addRightMenu(view: NSView, event: NSEvent) {
  153. let point = event.locationInWindow
  154. let tempView = view
  155. var viewHeight: CGFloat = 0
  156. let items: [String] = ["Add Item", "Add Sub-Item", "Add A Higher Level","", "Delete","", "Edit", "Rename", "Change Destination","", "Promote", "Demote"]
  157. var menuItemArr: [ComponentMenuitemProperty] = []
  158. for value in items {
  159. if value.count == 0 {
  160. let property: ComponentMenuitemProperty = ComponentMenuitemProperty.divider()
  161. menuItemArr.append(property)
  162. viewHeight += 8
  163. } else {
  164. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  165. itemSelected: false,
  166. isDisabled: false,
  167. keyEquivalent: nil,
  168. text: KMLocalizedString(value),
  169. identifier: value)
  170. menuItemArr.append(properties_Menuitem)
  171. viewHeight += 36
  172. }
  173. }
  174. if self.BOTAOutlineView.outlineView.selectedRowIndexes.count > 1 {
  175. for data in menuItemArr {
  176. if data.text == KMLocalizedString("Delete") {
  177. data.isDisabled = false
  178. } else {
  179. data.isDisabled = true
  180. }
  181. }
  182. if menuGroupView_ == nil {
  183. menuGroupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  184. }
  185. if menuGroupView_ != nil {
  186. menuGroupView_?.clickedAutoHide = false
  187. menuGroupView_?.groupDelegate = self
  188. menuGroupView_?.frame = CGRectMake(0, 0, 180, viewHeight)
  189. menuGroupView_?.updateGroupInfo(menuItemArr)
  190. menuGroupView_?.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: tempView)
  191. }
  192. return
  193. } else if self.BOTAOutlineView.outlineView.selectedRowIndexes.count > 0 {
  194. for data in menuItemArr {
  195. if data.text == KMLocalizedString("Add Sub-Item") || data.text == KMLocalizedString("Change Destination") {
  196. data.isDisabled = false
  197. } else {
  198. // data.isDisabled = true
  199. }
  200. }
  201. }
  202. if self.BOTAOutlineView.outlineView.clickedRow == -1 {
  203. for data in menuItemArr {
  204. if data.text == KMLocalizedString("Add Item") {
  205. data.isDisabled = false
  206. } else {
  207. data.isDisabled = true
  208. }
  209. }
  210. } else {
  211. let outlineItem : KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  212. if outlineItem.outline.index > 0 {
  213. for data in menuItemArr {
  214. if data.text == KMLocalizedString("Demote") {
  215. data.isDisabled = false
  216. } else {
  217. // data.isDisabled = true
  218. }
  219. }
  220. } else {
  221. for data in menuItemArr {
  222. if data.text == KMLocalizedString("Demote") {
  223. data.isDisabled = true
  224. } else {
  225. // data.isDisabled = true
  226. }
  227. }
  228. }
  229. let parentOutline = outlineItem.outline.parent
  230. let grandparentOutline = parentOutline?.parent
  231. if grandparentOutline != nil {
  232. for data in menuItemArr {
  233. if data.text == KMLocalizedString("Add A Higher Level") || data.text == KMLocalizedString("Promote") {
  234. data.isDisabled = false
  235. } else {
  236. // data.isDisabled = true
  237. }
  238. }
  239. } else {
  240. for data in menuItemArr {
  241. if data.text == KMLocalizedString("Add A Higher Level") || data.text == KMLocalizedString("Promote") {
  242. data.isDisabled = true
  243. } else {
  244. // data.isDisabled = true
  245. }
  246. }
  247. }
  248. }
  249. if menuGroupView_ == nil {
  250. menuGroupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  251. }
  252. if menuGroupView_ != nil {
  253. menuGroupView_?.clickedAutoHide = false
  254. menuGroupView_?.groupDelegate = self
  255. menuGroupView_?.frame = CGRectMake(0, 0, 180, viewHeight)
  256. menuGroupView_?.updateGroupInfo(menuItemArr)
  257. menuGroupView_?.showWithPoint(CGPoint(x: point.x, y: point.y - viewHeight), relativeTo: tempView)
  258. }
  259. }
  260. func reloadData() {
  261. self.BOTAOutlineView.reloadData(expandItemType: .none)
  262. }
  263. func editOutlineUI(editVC : KMOutlineEditViewController!) {
  264. if editVC.pageRadio.properties.checkboxType == .selected {
  265. let index = Int(editVC.pageInput.properties.text) ?? 0
  266. let pageIndex = max(0, index-1)
  267. if editVC.originalDestination?.pageIndex != pageIndex {
  268. let page = editVC.pdfView?.document.page(at: UInt(pageIndex))
  269. if page != nil {
  270. let destination = CPDFDestination.init(document: editVC.pdfView!.document, pageIndex: pageIndex)
  271. editVC.outline?.destination = destination
  272. } else {
  273. __NSBeep()
  274. }
  275. }
  276. } else if editVC.webRaido.properties.checkboxType == .selected {
  277. if editVC.originalURLString != editVC.webInput.properties.text {
  278. var urlString = editVC.webInput.properties.text
  279. let tLowerUrl = urlString.lowercased()
  280. if !tLowerUrl.hasPrefix("https://") && !tLowerUrl.hasPrefix("pf]://") && !urlString.hasPrefix("https://") &&
  281. urlString.lengthOfBytes(using: String.Encoding(rawValue: String.Encoding.utf16.rawValue)) > 0 {
  282. urlString = "http://\(urlString)"
  283. }
  284. let action = CPDFURLAction.init(url: urlString)
  285. editVC.outline?.action = action
  286. }
  287. } else if editVC.emailRadio.properties.checkboxType == .selected {
  288. var mailString = editVC.emailInput.properties.text
  289. let tLowerStr = mailString.lowercased()
  290. if !tLowerStr.hasPrefix("mailto:") {
  291. mailString = "mailto:\(mailString)"
  292. }
  293. if mailString != editVC.originalURLString {
  294. var action = CPDFURLAction.init(url: mailString)
  295. if action?.url == nil {
  296. action = CPDFURLAction.init(url: "mailto:")
  297. }
  298. editVC.outline?.action = action
  299. }
  300. }
  301. }
  302. // MARK: - Private Methods
  303. private func _showAlert(style: NSAlert.Style, message: String, info: String, buttons: [String]) -> NSApplication.ModalResponse {
  304. let alert = NSAlert()
  305. alert.alertStyle = style
  306. alert.messageText = message
  307. alert.informativeText = info
  308. for button in buttons {
  309. alert.addButton(withTitle: button)
  310. }
  311. return alert.runModal()
  312. }
  313. private func _showHeaderSearch() {
  314. BOTAOutlineView.isSearchMode = true
  315. headerSearchView_?.ignoreCase = ignoreCase_
  316. headerSearchView_?.isHidden = false
  317. }
  318. private func _hideHeaderSearch() {
  319. BOTAOutlineView.isSearchMode = false
  320. headerSearchView_?.isHidden = true
  321. }
  322. @objc private func _moreAction() {
  323. self.showGroupView()
  324. }
  325. //MARK: - GroupView
  326. func showGroupView() {
  327. var viewHeight: CGFloat = 8
  328. var menuItemArr: [ComponentMenuitemProperty] = []
  329. for i in ["Expand All", "Collapse All", "Remove All Outlines"] {
  330. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  331. itemSelected: false,
  332. isDisabled: false,
  333. keyEquivalent: nil,
  334. text: KMLocalizedString(i))
  335. menuItemArr.append(properties_Menuitem)
  336. viewHeight += 36
  337. }
  338. if let data = menuItemArr.first {
  339. var canExpand = false
  340. for row in 0..<self.BOTAOutlineView.outlineView.numberOfRows {
  341. // 检查当前项目是否可以展开
  342. let item = self.BOTAOutlineView.outlineView.item(atRow: row)
  343. if self.BOTAOutlineView.outlineView.isExpandable(item) {
  344. if !self.BOTAOutlineView.outlineView.isItemExpanded(item) {
  345. canExpand = true
  346. break
  347. }
  348. }
  349. }
  350. data.isDisabled = !canExpand
  351. }
  352. if let data = menuItemArr.safe_element(for: 1) as? ComponentMenuitemProperty {
  353. var canCollapse = false
  354. for row in 0..<self.BOTAOutlineView.outlineView.numberOfRows {
  355. let item = self.BOTAOutlineView.outlineView.item(atRow: row)
  356. if self.BOTAOutlineView.outlineView.isExpandable(item) {
  357. if self.BOTAOutlineView.outlineView.isItemExpanded(item) {
  358. canCollapse = true
  359. break
  360. }
  361. }
  362. }
  363. data.isDisabled = !canCollapse
  364. }
  365. if let data = menuItemArr.last {
  366. if self.BOTAOutlineView.outlineView.item(atRow: 0) != nil {
  367. data.isDisabled = false
  368. } else {
  369. data.isDisabled = true
  370. }
  371. }
  372. if groupView_ == nil {
  373. groupView_ = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  374. }
  375. groupView_?.groupDelegate = self
  376. groupView_?.frame = CGRectMake(310, 0, 200, viewHeight)
  377. groupView_?.updateGroupInfo(menuItemArr)
  378. var point = moreDropdown_.convert(moreDropdown_.frame.origin, to: nil)
  379. point.y -= viewHeight
  380. groupView_?.showWithPoint(point, relativeTo: moreDropdown_)
  381. moreDropdown_.properties.state = .pressed
  382. moreDropdown_.reloadData()
  383. }
  384. func removeGroupView() {
  385. if groupView_ != nil {
  386. groupView_?.removeFromSuperview()
  387. }
  388. moreDropdown_.properties.state = .normal
  389. moreDropdown_.reloadData()
  390. }
  391. func updateExtempViewState() {
  392. if(self.handdler.outlineRoot() == nil || self.handdler.outlineRoot()?.numberOfChildren == 0) { //无数据时的图
  393. self.emptyView.isHidden = false
  394. } else {
  395. self.emptyView.isHidden = true
  396. }
  397. }
  398. }
  399. //MARK: - Notification
  400. extension KMOutlineViewController {
  401. @objc func KMPDFViewCurrentPageDidChangedNotification(notification: NSNotification) {
  402. if notification.object is CPDFDocument {
  403. let pdfdocument : CPDFDocument = notification.object as! CPDFDocument
  404. if pdfdocument.isEqual(self.handdler.document) {
  405. if !isLocalEvent {
  406. self.updateOutlineSelection()
  407. }
  408. self.isLocalEvent = false
  409. }
  410. }
  411. }
  412. }
  413. //MARK: - Menu 右键菜单
  414. extension KMOutlineViewController {
  415. @objc func outlineViewDoubleAction() {
  416. if(self.BOTAOutlineView.outlineView.clickedRow >= 0) {
  417. self.renameItemAction()
  418. }
  419. }
  420. @objc func addItemAction() {
  421. guard let outlineView = BOTAOutlineView.outlineView else {
  422. return
  423. }
  424. let selectRowIndexs = outlineView.selectedRowIndexes
  425. let dataCount = BOTAOutlineView.data?.children.count ?? 0
  426. var index: Int = 0
  427. var parent: KMBOTAOutlineItem?
  428. var outlineItem: KMBOTAOutlineItem?
  429. if selectRowIndexs.count == 0 {
  430. var lastOulineItem: KMBOTAOutlineItem?
  431. if dataCount == 0 {
  432. let item = KMBOTAOutlineItem()
  433. item.outline = self.handdler.document!.setNewOutlineRoot()
  434. item.parent = nil
  435. parent = item
  436. lastOulineItem = item
  437. } else {
  438. outlineItem = outlineView.item(atRow: outlineView.numberOfRows - 1) as? KMBOTAOutlineItem
  439. lastOulineItem = outlineItem
  440. while lastOulineItem?.parent != nil {
  441. lastOulineItem = lastOulineItem?.parent
  442. }
  443. parent = lastOulineItem
  444. }
  445. index = Int(lastOulineItem?.outline.numberOfChildren ?? 0)
  446. } else {
  447. outlineItem = outlineView.item(atRow: selectRowIndexs.last ?? 0) as? KMBOTAOutlineItem
  448. parent = outlineItem?.parent ?? KMBOTAOutlineItem()
  449. index = Int((outlineItem?.outline.index) ?? 0 + 1)
  450. }
  451. self.addOutlineToIndex(index: index, parent: parent)
  452. }
  453. @objc func addChildItemAction() {
  454. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  455. let selectRowIndexs = outlineView.selectedRowIndexes
  456. if selectRowIndexs.count != 0 {
  457. let outlineItem: KMBOTAOutlineItem = outlineView.item(atRow: selectRowIndexs.last!) as! KMBOTAOutlineItem
  458. let index = outlineItem.outline.numberOfChildren
  459. self.addOutlineToIndex(index: NSInteger(index), parent: outlineItem)
  460. }
  461. }
  462. @objc func addHigherItemAction() {
  463. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  464. let selectRowIndexs = outlineView.selectedRowIndexes
  465. if selectRowIndexs.count != 0 {
  466. let outlineItem: KMBOTAOutlineItem = outlineView.item(atRow: selectRowIndexs.last!) as! KMBOTAOutlineItem
  467. var parent = outlineItem.parent
  468. let index = NSInteger(parent!.outline.index) + 1
  469. parent = parent?.parent
  470. if parent != nil {
  471. self.addOutlineToIndex(index: index, parent: parent!)
  472. }
  473. }
  474. }
  475. @objc func deleteItemAction() {
  476. let outlineView: KMOutlineView = self.BOTAOutlineView.outlineView
  477. let selectRowIndexs = outlineView.selectedRowIndexes
  478. if selectRowIndexs.count != 0 {
  479. var outlineItems: [KMBOTAOutlineItem] = []
  480. for index in selectRowIndexs {
  481. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: index) as! KMBOTAOutlineItem
  482. outlineItem.toIndex = index
  483. outlineItem.parent = outlineItem.parent ?? KMBOTAOutlineItem()
  484. outlineItems.append(outlineItem)
  485. }
  486. self.deleteOutline(outlineItems: outlineItems)
  487. }
  488. }
  489. @objc func editItemAction() {
  490. let clickedRow = BOTAOutlineView.outlineView.clickedRow
  491. if clickedRow < 0 {
  492. NSSound.beep()
  493. return
  494. }
  495. if let rowView = self.BOTAOutlineView.outlineView.rowView(atRow: clickedRow, makeIfNecessary: true) {
  496. let item = self.BOTAOutlineView.outlineView.item(atRow: clickedRow) as? KMBOTAOutlineItem
  497. let vc = KMOutlineEditViewController.init(outline: item?.outline, document: self.handdler.pdfView)
  498. vc.pageCount = handdler.pageCount()
  499. vc.itemClick = { [weak self] idx, params in
  500. if idx == 1 {
  501. self?.popover_?.close()
  502. } else if idx == 2 {
  503. self?.popover_?.close()
  504. if let viewC = params.first as? KMOutlineEditViewController {
  505. let resp = self?._showAlert(style: .informational, message: KMLocalizedString("Are you sure you want to apply edits to this outline?"), info: "", buttons: [KMLocalizedString("Apply"), KMLocalizedString("Cancel")])
  506. if resp == .alertFirstButtonReturn {
  507. self?.editOutlineUI(editVC: viewC)
  508. }
  509. }
  510. }
  511. }
  512. let popover = NSPopover()
  513. popover_ = popover
  514. popover.delegate = self
  515. popover.contentViewController = vc
  516. popover.animates = true
  517. popover.behavior = .transient
  518. popover.setValue(true, forKey: "shouldHideAnchor")
  519. popover.show(relativeTo: rowView.bounds, of: rowView, preferredEdge: .minX)
  520. }
  521. }
  522. @objc func renameItemAction() {
  523. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  524. self.renameOutlineWithRow(row: self.BOTAOutlineView.outlineView.clickedRow)
  525. } else {
  526. __NSBeep()
  527. }
  528. }
  529. @objc func changeItemAction() {
  530. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  531. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  532. let resp = _showAlert(style: .informational, message: NSLocalizedString("Are you sure you want to set the destination as the current location?", comment: ""), info: "", buttons: [NSLocalizedString("Yes", comment:""), NSLocalizedString("No", comment:"")])
  533. if resp == .alertFirstButtonReturn {
  534. // self.changeLocation(outlineItem: outlineItem, destination: self.fetchCurrentDestination())
  535. }
  536. } else {
  537. __NSBeep()
  538. }
  539. }
  540. @objc func promoteItemAction() {
  541. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  542. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  543. var parent = outlineItem.parent
  544. let index = NSInteger(parent!.outline.index) + 1
  545. parent = parent?.parent
  546. if parent != nil {
  547. self.moveOutline(outlineItem: outlineItem, index: index, parent: parent)
  548. }
  549. }
  550. }
  551. @objc func demoteItemAction() {
  552. if self.BOTAOutlineView.outlineView.clickedRow >= 0 {
  553. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: self.BOTAOutlineView.outlineView.clickedRow) as! KMBOTAOutlineItem
  554. let parent = outlineItem.parent
  555. let newParent = parent?.children[Int(outlineItem.outline.index) - 1]
  556. let index = newParent?.children.count
  557. if (index != nil) {
  558. self.moveOutline(outlineItem: outlineItem, index: NSInteger(index ?? 0), parent: newParent)
  559. }
  560. }
  561. }
  562. @objc private func expandAllComments(item: NSMenuItem) {
  563. self.BOTAOutlineView.expandAllComments(item: item)
  564. }
  565. @objc private func collapseAllComments(item: NSMenuItem) {
  566. self.BOTAOutlineView.collapseAllComments(item: item)
  567. }
  568. @objc private func removeAllOutlineItem(item: NSMenuItem) {
  569. let alter = NSAlert()
  570. alter.alertStyle = NSAlert.Style.informational
  571. alter.messageText = NSLocalizedString("This will permanently remove all outlines. Are you sure to continue?", comment: "")
  572. alter.addButton(withTitle: NSLocalizedString("Yes", comment:""))
  573. alter.addButton(withTitle: NSLocalizedString("No", comment:""))
  574. let modlres = alter.runModal()
  575. if modlres == NSApplication.ModalResponse.alertFirstButtonReturn {
  576. self.removeAllOutline()
  577. }
  578. }
  579. @objc private func removeAllOutline() {
  580. guard let data = self.BOTAOutlineView.data else { return }
  581. for item in data.children {
  582. item.toIndex = Int(item.outline.index)
  583. }
  584. self.deleteOutline(outlineItems: data.children)
  585. self.BOTAOutlineView.reloadData(expandItemType: .none)
  586. }
  587. @objc private func _searchAction() {
  588. _showHeaderSearch()
  589. }
  590. }
  591. //MARK: - Action
  592. extension KMOutlineViewController {
  593. @IBAction func addNewOutline(_ sender: Any) {
  594. self.addItemAction()
  595. }
  596. @IBAction func escButtonAction(_ sender: Any) {
  597. self.cancelSelect()
  598. }
  599. func cancelSelect() {
  600. self.BOTAOutlineView.cancelSelect()
  601. }
  602. func renameOutlineWithRow(row: NSInteger) {
  603. DispatchQueue.main.async {
  604. self.renamePDFOutline = self.BOTAOutlineView.outlineView.item(atRow: row) as? KMBOTAOutlineItem
  605. let cell : KMBOTAOutlineCellView = self.BOTAOutlineView.outlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as! KMBOTAOutlineCellView
  606. self.renameTextField = cell.titleLabel
  607. self.renameTextField.delegate = self
  608. self.renameTextField.isEditable = true
  609. self.renameTextField.becomeFirstResponder()
  610. }
  611. }
  612. func addOutlineToIndex(index: Int, parent: KMBOTAOutlineItem?) {
  613. let pageIndex = self.handdler.currentPageIndex
  614. let label = self.fetchCurrentLabel(pageIndex: pageIndex)
  615. let destination = self.handdler.currentDestination()
  616. self.addOutlineToIndex(index: index, pageIndex: pageIndex, destination: destination, lable: label, parent: parent)
  617. }
  618. func addOutlineToIndex(index: Int, pageIndex: Int, destination: CPDFDestination?, lable: String, parent: KMBOTAOutlineItem?) {
  619. let outlineItem = KMBOTAOutlineItem()
  620. outlineItem.destination = destination
  621. outlineItem.label = lable
  622. outlineItem.parent = parent
  623. outlineItem.toIndex = index
  624. self.addOutline(outlineItems: [outlineItem])
  625. let tempOutlineView = self.BOTAOutlineView!
  626. var index = -1
  627. if tempOutlineView.outlineView.numberOfRows == 1 || tempOutlineView.data == nil {
  628. index = 0
  629. } else {
  630. index = tempOutlineView.outlineView.row(forItem: outlineItem)
  631. }
  632. tempOutlineView.selectIndex(index: index)
  633. //滑动到指定位置
  634. if(tempOutlineView.outlineView.selectedRow >= 0) {
  635. self.renameOutlineWithRow(row: tempOutlineView.outlineView.selectedRow)
  636. }
  637. let row = tempOutlineView.outlineView.row(forItem: outlineItem)
  638. if Thread.current.isMainThread {
  639. tempOutlineView.outlineView.scrollToVisible(tempOutlineView.outlineView.rect(ofRow: row))
  640. } else {
  641. DispatchQueue.main.async {
  642. tempOutlineView.outlineView.scrollToVisible(tempOutlineView.outlineView.rect(ofRow: row))
  643. }
  644. }
  645. }
  646. func updateOutlineSelection() {
  647. let currentPageIndex = self.handdler.currentPageIndex
  648. let numRows = self.BOTAOutlineView.outlineView.numberOfRows
  649. if numRows > 0 {
  650. for i in 0...numRows - 1 {
  651. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow: i) as! KMBOTAOutlineItem
  652. if (outlineItem.outline.destination == nil) {
  653. continue
  654. }
  655. if outlineItem.outline.destination.pageIndex == currentPageIndex {
  656. self.BOTAOutlineView.selectIndex(index: currentPageIndex)
  657. break
  658. }
  659. }
  660. }
  661. }
  662. // func fetchCurrentDestination() -> CPDFDestination {
  663. // //destination
  664. // var destination: CPDFDestination
  665. // let pageIndex: Int
  666. // if self.listView.currentSelection != nil {
  667. // let des :CPDFDestination = CPDFDestination.init(document: self.listView.document, pageIndex: Int(self.listView.currentSelection.page.pageIndex()), at: CGPoint(x: self.listView.currentSelection.bounds.origin.x, y: self.listView.currentSelection.bounds.origin.y + self.listView.currentSelection.bounds.size.height), zoom: self.listView.scaleFactor)
  668. // destination = des
  669. // pageIndex = Int(self.listView.currentSelection.page.pageIndex())
  670. // } else {
  671. // let des :CPDFDestination = CPDFDestination.init(document: self.listView.document, pageIndex: Int(self.listView.currentPageIndex), at: CGPoint(x: 0, y: self.listView.currentPage().size.height), zoom: self.listView.scaleFactor)
  672. // destination = des
  673. // pageIndex = Int(self.listView.currentPageIndex)
  674. // }
  675. //
  676. // if "\(destination.point.x )" == "nan" {
  677. // destination = CPDFDestination(document: self.listView.document, pageIndex: pageIndex, at: CGPoint(x: 0, y: 0), zoom: self.listView.scaleFactor)
  678. // }
  679. // return destination
  680. // }
  681. func fetchCurrentLabel(pageIndex: Int) -> String {
  682. var label = "\(NSLocalizedString("Page", comment: ""))\(pageIndex + 1)"
  683. let currentSelection = self.handdler.currentSelection()
  684. if currentSelection != nil && currentSelection?.selectionsByLine != nil {
  685. for data in currentSelection?.selectionsByLine ?? [] {
  686. label = data.string() ?? ""
  687. }
  688. }
  689. return label
  690. }
  691. }
  692. //MARK: - KMBOTAOutlineViewDelegate
  693. extension KMOutlineViewController: KMBOTAOutlineViewDelegate {
  694. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, rightDidMoseDown: KMBOTAOutlineItem, event: NSEvent) {
  695. let row = outlineView.outlineView.row(forItem: rightDidMoseDown)
  696. if outlineView.outlineView.rowView(atRow: row, makeIfNecessary: false) != nil {
  697. let rowView = outlineView.outlineView.rowView(atRow: row, makeIfNecessary: false)
  698. self.addRightMenu(view: rowView!, event: event)
  699. }
  700. }
  701. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didReloadData: KMBOTAOutlineItem) {
  702. self.updateExtempViewState()
  703. }
  704. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, didSelectItem: [KMBOTAOutlineItem]) {
  705. if self.BOTAOutlineView.outlineView.selectedRowIndexes.count == 1 {
  706. self.isLocalEvent = true
  707. let outlineItem: KMBOTAOutlineItem = self.BOTAOutlineView.outlineView.item(atRow:self.BOTAOutlineView.outlineView.selectedRow) as! KMBOTAOutlineItem
  708. if outlineItem.outline.destination != nil {
  709. if outlineItem.outline.destination.page() != nil {
  710. // self.listView.go(toTargetPoint: outlineItem.outline.destination.point, on: outlineItem.outline.destination.page() , at: .top)
  711. } else {
  712. let alter = NSAlert()
  713. alter.alertStyle = NSAlert.Style.informational
  714. alter.messageText = NSLocalizedString("The target page is invalid, please relocate it.", comment: "")
  715. alter.addButton(withTitle: NSLocalizedString("OK", comment:""))
  716. alter.beginSheetModal(for: self.view.window ?? NSWindow())
  717. }
  718. } else if outlineItem.outline.action != nil {
  719. let alter = NSAlert()
  720. alter.alertStyle = NSAlert.Style.informational
  721. alter.messageText = NSLocalizedString("The target page is invalid, please relocate it.", comment: "")
  722. alter.addButton(withTitle: NSLocalizedString("OK", comment:""))
  723. alter.beginSheetModal(for: self.view.window ?? NSWindow())
  724. // self.listView.perform(outlineItem.outline.action)
  725. }
  726. }
  727. }
  728. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, writeItems items: [Any], to pasteboard: NSPasteboard) -> Bool {
  729. if outlineView.outlineView.selectedRowIndexes.count > 1 || outlineView.outlineView.selectedRow == -1 {
  730. return false
  731. }
  732. self.dragPDFOutline = items.first as? KMBOTAOutlineItem
  733. let indexSet = [outlineView.outlineView.clickedRow]
  734. let indexSetData: Data = try!NSKeyedArchiver.archivedData(withRootObject: indexSet, requiringSecureCoding: true)
  735. pasteboard.declareTypes([NSPasteboard.PasteboardType(rawValue: "kKMPDFViewOutlineDragDataType")], owner: self)
  736. pasteboard.setData(indexSetData, forType: NSPasteboard.PasteboardType(rawValue: NSPasteboard.PasteboardType.RawValue("kKMPDFViewOutlineDragDataType")))
  737. return true
  738. }
  739. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, validateDrop info: NSDraggingInfo, proposedItem item: Any?, proposedChildIndex index: Int) -> NSDragOperation {
  740. var dragOperation = NSDragOperation.init(rawValue: 0)
  741. if index >= 0 {
  742. dragOperation = NSDragOperation.move
  743. }
  744. return dragOperation
  745. }
  746. func BOTAOutlineView(_ outlineView: KMBOTAOutlineView, acceptDrop info: NSDraggingInfo, item: Any?, childIndex index: Int) -> Bool {
  747. guard let dragOutlineItem = self.dragPDFOutline else { return false }
  748. let outlineItem: KMBOTAOutlineItem = (item ?? KMBOTAOutlineItem()) as! KMBOTAOutlineItem
  749. if index < 0 {
  750. return false
  751. }
  752. if outlineItem.parent == nil {
  753. var root = dragOutlineItem.parent
  754. while root?.parent?.children != nil {
  755. root = root?.parent!
  756. }
  757. if dragOutlineItem.parent!.isEqual(root) {
  758. if dragOutlineItem.outline.index > index {
  759. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: root)
  760. } else {
  761. self.moveOutline(outlineItem: dragOutlineItem, index: index - 1, parent: root)
  762. }
  763. } else {
  764. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: root)
  765. }
  766. } else {
  767. if dragOutlineItem.parent!.isEqual(item) {
  768. // if dragOutlineItem.outline.index != 0 {
  769. if dragOutlineItem.outline.index > index {
  770. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: item as? KMBOTAOutlineItem)
  771. } else {
  772. self.moveOutline(outlineItem: dragOutlineItem, index: index - 1, parent: item as? KMBOTAOutlineItem)
  773. }
  774. // } else {
  775. // return false
  776. // }
  777. } else {
  778. var tOutline = outlineItem
  779. var isContains = false
  780. while (tOutline.parent != nil) {
  781. if tOutline.outline.isEqual(dragOutlineItem.outline) {
  782. isContains = true
  783. break
  784. }
  785. tOutline = tOutline.parent!
  786. }
  787. if isContains == false {
  788. self.moveOutline(outlineItem: dragOutlineItem, index: index, parent: item as? KMBOTAOutlineItem)
  789. }
  790. }
  791. }
  792. self.BOTAOutlineView.selectItem(outlineItem: dragOutlineItem)
  793. return true
  794. }
  795. }
  796. //MARK: - NSTextFieldDelegate
  797. extension KMOutlineViewController: NSTextFieldDelegate {
  798. func controlTextDidEndEditing(_ obj: Notification) {
  799. if (self.renameTextField.isEqual(obj.object)) {
  800. let textField : NSTextField = obj.object as! NSTextField
  801. self.renamePDFOutline(outlineItem: self.renamePDFOutline, label: textField.stringValue)
  802. }
  803. }
  804. }
  805. //MARK: - NSPopoverDelegate
  806. extension KMOutlineViewController: NSPopoverDelegate {
  807. func popoverWillClose(_ notification: Notification) {
  808. let popover : NSPopover = notification.object as! NSPopover
  809. if popover.contentViewController!.isKind(of: KMOutlineEditViewController.self) {
  810. }
  811. }
  812. func popoverDidClose(_ notification: Notification) {
  813. if popover_ == (notification.object as? NSPopover) {
  814. popover_ = nil
  815. }
  816. }
  817. }
  818. //MARK: - NSMenuItemValidation
  819. extension KMOutlineViewController: NSMenuDelegate, NSMenuItemValidation {
  820. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  821. let action = menuItem.action
  822. if (action == #selector(undo)) {
  823. return handdler.canUndo()
  824. }
  825. if (action == #selector(redo)) {
  826. return handdler.canRedo()
  827. }
  828. return true
  829. }
  830. }
  831. // MARK: - KMNOutlineHanddlerDelegate
  832. extension KMOutlineViewController: KMNOutlineHanddlerDelegate {
  833. func handdler(_ handdler: KMNOutlineHanddler, didAdd info: [String : Any]?) {
  834. let tempOutlineItems = info?["data"] as? [KMBOTAOutlineItem] ?? []
  835. let tempOutlineView = self.BOTAOutlineView
  836. if tempOutlineView?.data?.children.count == 0 || tempOutlineView?.data == nil {
  837. tempOutlineView?.inputData = self.handdler.outlineRoot()
  838. } else {
  839. tempOutlineView?.outlineView.reloadData()
  840. }
  841. //展开
  842. // DispatchQueue.main.async {
  843. for outlineItem in tempOutlineItems {
  844. var tempParent = outlineItem
  845. while tempParent.parent != nil {
  846. tempParent.isItemExpanded = true
  847. tempParent = tempParent.parent!
  848. tempOutlineView?.outlineView.expandItem(tempParent)
  849. }
  850. tempOutlineView?.outlineView.expandItem(tempParent.parent)
  851. }
  852. // }
  853. self.updateExtempViewState()
  854. }
  855. func handdler(_ handdler: KMNOutlineHanddler, didRemove info: [String : Any]?) {
  856. let tempOutlineItems = info?["data"] as? [KMBOTAOutlineItem] ?? []
  857. let tempOutlineView = self.BOTAOutlineView
  858. //展开
  859. for outlineItem in tempOutlineItems {
  860. outlineItem.parent?.isItemExpanded = true
  861. tempOutlineView?.outlineView.expandItem(outlineItem.parent)
  862. }
  863. tempOutlineView?.outlineView.reloadData()
  864. //删除需要取消选中
  865. tempOutlineView?.cancelSelect()
  866. //刷新nil数据
  867. self.updateExtempViewState()
  868. }
  869. func handdler(_ handdler: KMNOutlineHanddler, didRename outline: CPDFOutline?, info: [String : Any]?) {
  870. let outlineItem = info?["data"] as? KMBOTAOutlineItem
  871. let tempOutlineView = self.BOTAOutlineView
  872. tempOutlineView?.outlineView.reloadItem(outlineItem)
  873. }
  874. func handdler(_ handdler: KMNOutlineHanddler, didChangeLocation outline: CPDFOutline?, info: [String : Any]?) {
  875. let outlineItem = info?["data"] as? KMBOTAOutlineItem
  876. let tempOutlineView = self.BOTAOutlineView
  877. tempOutlineView?.outlineView.reloadItem(outlineItem)
  878. }
  879. func handdler(_ handdler: KMNOutlineHanddler, didMove outline: CPDFOutline?, info: [String : Any]?) {
  880. guard let outlineItem = info?["data"] as? KMBOTAOutlineItem else {
  881. return
  882. }
  883. let parent = info?["parent"] as? KMBOTAOutlineItem
  884. let tempOutlineView = self.BOTAOutlineView
  885. let index = info?["index"] as? Int ?? 0
  886. //显示数据刷新
  887. outlineItem.parent?.children.removeObject(outlineItem)
  888. parent?.children.insert(outlineItem, at: index)
  889. outlineItem.parent = parent
  890. tempOutlineView?.outlineView.reloadData()
  891. tempOutlineView?.cancelSelect()
  892. //展开
  893. outlineItem.isItemExpanded = true
  894. outlineItem.parent?.isItemExpanded = true
  895. tempOutlineView?.outlineView.expandItem(outlineItem)
  896. tempOutlineView?.outlineView.expandItem(outlineItem.parent)
  897. }
  898. }
  899. //MARK: - 快捷键
  900. extension KMOutlineViewController {
  901. @IBAction func delete(_ sender: Any) {
  902. self.deleteItemAction()
  903. }
  904. }
  905. //MARK: - undoRedo
  906. extension KMOutlineViewController {
  907. func moveOutline(outlineItem: KMBOTAOutlineItem, index: NSInteger, parent: KMBOTAOutlineItem!) {
  908. handdler.moveOutline(outlineItem: outlineItem, index: index, parent: parent)
  909. }
  910. func changeLocation(outlineItem: KMBOTAOutlineItem, destination: CPDFDestination) {
  911. handdler.changeLocation(outlineItem: outlineItem, destination: destination)
  912. }
  913. func renamePDFOutline(outlineItem: KMBOTAOutlineItem!, label: String) {
  914. let tempOutlineView = self.BOTAOutlineView!
  915. self.view.window?.makeFirstResponder(tempOutlineView.outlineView)
  916. self.renameTextField.isEditable = false
  917. if outlineItem.outline.label == label {
  918. return
  919. }
  920. handdler.renamePDFOutline(outlineItem: outlineItem, label: label)
  921. }
  922. func deleteOutline(outlineItems: [KMBOTAOutlineItem]) {
  923. NSApp.mainWindow?.makeFirstResponder(self.BOTAOutlineView)
  924. let tempOutlineView = self.BOTAOutlineView!
  925. handdler.deleteOutline(outlineItems: outlineItems)
  926. }
  927. func addOutline(outlineItems: [KMBOTAOutlineItem]) {
  928. NSApp.mainWindow?.makeFirstResponder(self.BOTAOutlineView)
  929. let tempOutlineView = self.BOTAOutlineView!
  930. //先取消选中
  931. tempOutlineView.cancelSelect()
  932. var tempOutlineItems: [KMBOTAOutlineItem] = outlineItems
  933. tempOutlineItems.sort(){$0.toIndex < $1.toIndex}
  934. handdler.addOutline(outlineItems: tempOutlineItems)
  935. }
  936. @IBAction func undo(_ sender: Any) {
  937. handdler.undo()
  938. }
  939. @IBAction func redo(_ sender: Any) {
  940. handdler.redo()
  941. }
  942. }
  943. //MARK: - ComponentDropdownDelegate
  944. extension KMOutlineViewController: ComponentDropdownDelegate {
  945. func componentDropdownDidShowMenuItem(dropdown: ComponentDropdown) {
  946. showGroupView()
  947. }
  948. }
  949. //MARK: - ComponentGroupDelegate
  950. extension KMOutlineViewController: ComponentGroupDelegate {
  951. func componentGroupDidDismiss(group: ComponentGroup?) {
  952. if group == groupView_ {
  953. removeGroupView()
  954. } else if group == menuGroupView_ {
  955. group?.removeFromSuperview()
  956. menuGroupView_ = nil
  957. }
  958. }
  959. func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  960. if group == groupView_ {
  961. if let selItem = menuItemProperty {
  962. let index = group?.menuItemArr.firstIndex(of: selItem)
  963. if index == 0 {
  964. expandAllComments(item: NSMenuItem())
  965. } else if index == 1 {
  966. collapseAllComments(item: NSMenuItem())
  967. } else if index == 2 {
  968. removeAllOutlineItem(item: NSMenuItem())
  969. }
  970. }
  971. } else if group == menuGroupView_ {
  972. if let selItem = menuItemProperty {
  973. let index = group?.menuItemArr.firstIndex(of: selItem)
  974. if index == 0 {
  975. addItemAction()
  976. } else if index == 1 {
  977. addChildItemAction()
  978. } else if index == 2 {
  979. addHigherItemAction()
  980. } else if index == 4 {
  981. deleteItemAction()
  982. } else if index == 6 {
  983. group?.removeFromSuperview()
  984. editItemAction()
  985. } else if index == 7 {
  986. renameItemAction()
  987. } else if index == 8 {
  988. changeItemAction()
  989. } else if index == 10 {
  990. promoteItemAction()
  991. } else if index == 11 {
  992. demoteItemAction()
  993. }
  994. group?.removeFromSuperview()
  995. }
  996. }
  997. }
  998. }