From 4c1c3f031eca96b26f2bbd2ea102821975ee7677 Mon Sep 17 00:00:00 2001 From: PanajotisMaroungas Date: Mon, 22 Aug 2016 19:26:47 +0200 Subject: [PATCH 01/12] add listenerBlock and listernerDirectory in ReaderConfig. --- Source/FolioReaderConfig.swift | 14 +++++++++++- Source/FolioReaderPage.swift | 39 +++++++++++++++++++++++++++++++++- Source/Resources/Bridge.js | 22 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index 53df64905..dee61ea10 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -42,7 +42,12 @@ public enum FolioReaderScrollDirection: Int { Defines the Reader custom configuration */ public class FolioReaderConfig: NSObject { - + + /// array of disctionary with keys className and parameterName + public var listernetDictionary = [[:]] + + public var listenerBlock: ((json: String?) -> Void)? + // MARK: Colors /// Base header custom TintColor @@ -119,3 +124,10 @@ public class FolioReaderConfig: NSObject { public var localizedShareAllExcerptsFrom = NSLocalizedString("All excerpts from", comment: "") public var localizedShareBy = NSLocalizedString("by", comment: "") } + +// MARK: - Constants + +public struct Listener { + static let className = "className" + static let parameterName = "parameterName" +} diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index f9a093beb..ed2fe8962 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -136,6 +136,8 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni // MARK: - UIWebView Delegate func webViewDidFinishLoad(webView: UIWebView) { + + self.setupListeners() refreshPageMode() if readerConfig.enableTTS && !book.hasAudio() { @@ -223,7 +225,12 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni } return true - } else if url.scheme == "mailto" { + } else if url.scheme == "listener" { + + let parameter = (request.URL?.absoluteString.stringByReplacingOccurrencesOfString("listener://", withString: "").stringByRemovingPercentEncoding) + readerConfig.listenerBlock?(json: parameter) + return false + }else if url.scheme == "mailto" { print("Email") return true } else if url.absoluteString != "about:blank" && url.scheme.containsString("http") && navigationType == .LinkClicked { @@ -656,3 +663,33 @@ extension UIMenuItem { #endif } } + +// MARK: - Private helpers +extension FolioReaderPage { + + private func setupListeners() { + + let javascriptArray = self.extractListenerDictionary() + self.webView.js("setListenerDictionary(\(javascriptArray))"); + } + + private func extractListenerDictionary() -> String { + var javascriptDictionary = "[" + + for index in 0.. Date: Tue, 23 Aug 2016 09:45:31 +0200 Subject: [PATCH 02/12] Minor refactoring --- Source/FolioReaderConfig.swift | 11 ++++++++--- Source/FolioReaderPage.swift | 20 ++++++++------------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index dee61ea10..13c9f5662 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -44,9 +44,9 @@ public enum FolioReaderScrollDirection: Int { public class FolioReaderConfig: NSObject { /// array of disctionary with keys className and parameterName - public var listernetDictionary = [[:]] + public var userTapListenerConfiguration = [ListenerCofiguration]() - public var listenerBlock: ((json: String?) -> Void)? + public var userTapListenerBlock: ((json: String?) -> Void)? // MARK: Colors @@ -127,7 +127,12 @@ public class FolioReaderConfig: NSObject { // MARK: - Constants -public struct Listener { + +public struct ListenerCofiguration { + static let className = "className" static let parameterName = "parameterName" + + var classNameValue : String + var parameterNameValue : String } diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index ed2fe8962..382aed245 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -228,7 +228,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni } else if url.scheme == "listener" { let parameter = (request.URL?.absoluteString.stringByReplacingOccurrencesOfString("listener://", withString: "").stringByRemovingPercentEncoding) - readerConfig.listenerBlock?(json: parameter) + readerConfig.userTapListenerBlock?(json: parameter) return false }else if url.scheme == "mailto" { print("Email") @@ -676,17 +676,13 @@ extension FolioReaderPage { private func extractListenerDictionary() -> String { var javascriptDictionary = "[" - for index in 0.. Date: Tue, 30 Aug 2016 19:01:56 +0200 Subject: [PATCH 03/12] Merge with solved conflicts --- Example/StoryboardExample/Base.lproj/Main.storyboard | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Example/StoryboardExample/Base.lproj/Main.storyboard b/Example/StoryboardExample/Base.lproj/Main.storyboard index 15ceab661..ed6e03531 100644 --- a/Example/StoryboardExample/Base.lproj/Main.storyboard +++ b/Example/StoryboardExample/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -42,6 +42,10 @@ + + + + From 12bda49ad092baf8c0a4bc0f012729c21b362fab Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Wed, 7 Sep 2016 17:18:49 +0200 Subject: [PATCH 04/12] Fix typo --- Source/FolioReaderConfig.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index d1970dc24..1f0c0485a 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -49,7 +49,7 @@ Eg. A ClassBasedOnCLickListener with the className "quote" and parameterName "id */ public struct ClassBasedOnCLickListener { - /// The name of the URL scheme which should be used. Note: Make sure that the given `String` is valid as scheme. + /// The name of the URL scheme which should be used. Note: Make sure that the given `String` is a valid as scheme name. public var schemeName : String /// The HTML class name to which the listener should be added. From 4d888f1e305b148f218d7bbe83162da0f3f268a8 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Thu, 8 Sep 2016 12:39:43 +0200 Subject: [PATCH 05/12] Fix typo in ClassBasedOnCLickListener --- Source/FolioReaderConfig.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index 1f0c0485a..c969e7507 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -39,15 +39,15 @@ public enum FolioReaderScrollDirection: Int { } } -// MARK: - ClassBasedOnCLickListener +// MARK: - ClassBasedOnClickListener /** -A `ClassBasedOnCLickListener` takes a closure which is performed if a given html `class` is clicked. The closure will reveice the content of the specified parameter. +A `ClassBasedOnClickListener` takes a closure which is performed if a given html `class` is clicked. The closure will reveice the content of the specified parameter. -Eg. A ClassBasedOnCLickListener with the className "quote" and parameterName "id" with the given epub html content "
" would call the given closure on a click on this section with the String "12345" as parameter. +Eg. A ClassBasedOnClickListener with the className "quote" and parameterName "id" with the given epub html content "
" would call the given closure on a click on this section with the String "12345" as parameter. */ -public struct ClassBasedOnCLickListener { +public struct ClassBasedOnClickListener { /// The name of the URL scheme which should be used. Note: Make sure that the given `String` is a valid as scheme name. public var schemeName : String @@ -61,7 +61,7 @@ public struct ClassBasedOnCLickListener { /// The closure which will be called if the specified class was clicked. public var onClickAction : ((parameterContent: String?) -> Void) - /// Initializes a `ClassBasedOnCLickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events. + /// Initializes a `ClassBasedOnClickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events. public init(schemeName: String, className: String, parameterName: String, onClickAction: ((parameterContent: String?) -> Void)) { self.schemeName = schemeName.lowercaseString self.className = className @@ -77,15 +77,15 @@ public struct ClassBasedOnCLickListener { */ public class FolioReaderConfig: NSObject { - // MARK: ClassBasedOnCLickListener + // MARK: ClassBasedOnClickListener /** - Array of `ClassBasedOnCLickListener` objects. A `ClassBasedOnCLickListener` takes a closure which is performed if a given html `class` is clicked. The closure will reveice the content of the specified parameter. + Array of `ClassBasedOnClickListener` objects. A `ClassBasedOnClickListener` takes a closure which is performed if a given html `class` is clicked. The closure will reveice the content of the specified parameter. - Eg. A ClassBasedOnCLickListener with the className "quote" and parameterName "id" with the given epub html content "
" would call the given closure on a click on this section with the String "12345" as parameter. + Eg. A ClassBasedOnClickListener with the className "quote" and parameterName "id" with the given epub html content "
" would call the given closure on a click on this section with the String "12345" as parameter. */ - public var classBasedOnClickListeners = [ClassBasedOnCLickListener]() + public var classBasedOnClickListeners = [ClassBasedOnClickListener]() // MARK: Colors From bff575e013d9f7d6beb905203f16d81a32b64af0 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Thu, 8 Sep 2016 12:40:29 +0200 Subject: [PATCH 06/12] Add a simple ClassBasedOnClickListener example to the StoryboardExample project --- .../ExampleFolioReaderContainer.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Example/StoryboardExample/ExampleFolioReaderContainer.swift b/Example/StoryboardExample/ExampleFolioReaderContainer.swift index bb6729a51..63824e51d 100644 --- a/Example/StoryboardExample/ExampleFolioReaderContainer.swift +++ b/Example/StoryboardExample/ExampleFolioReaderContainer.swift @@ -16,7 +16,16 @@ class ExampleFolioReaderContainer: FolioReaderContainer { let config = FolioReaderConfig() config.scrollDirection = .horizontalWithVerticalContent - + config.shouldHideNavigationOnTap = false + + // Print the chapter ID if one was clicked + // A chapter in "The Silver Chair" looks like this "
" + // To knwo if a user tapped on a chapter we can listen to events on the class "chapter" and receive the id value + let listener = ClassBasedOnClickListener(schemeName: "chapterTapped", className: "chapter", parameterName: "id", onClickAction: { (parameterContent: String?) in + print("chapter with id: " + (parameterContent ?? "-") + " clicked") + }) + config.classBasedOnClickListeners.append(listener) + guard let bookPath = NSBundle.mainBundle().pathForResource("The Silver Chair", ofType: "epub") else { return } setupConfig(config, epubPath: bookPath) } From 41b52cc2a92f78693489f66f71ae583c51b7bf12 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Thu, 8 Sep 2016 12:42:56 +0200 Subject: [PATCH 07/12] Use valid URL scheme name --- Example/StoryboardExample/ExampleFolioReaderContainer.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Example/StoryboardExample/ExampleFolioReaderContainer.swift b/Example/StoryboardExample/ExampleFolioReaderContainer.swift index 63824e51d..52afbe2b8 100644 --- a/Example/StoryboardExample/ExampleFolioReaderContainer.swift +++ b/Example/StoryboardExample/ExampleFolioReaderContainer.swift @@ -21,7 +21,7 @@ class ExampleFolioReaderContainer: FolioReaderContainer { // Print the chapter ID if one was clicked // A chapter in "The Silver Chair" looks like this "
" // To knwo if a user tapped on a chapter we can listen to events on the class "chapter" and receive the id value - let listener = ClassBasedOnClickListener(schemeName: "chapterTapped", className: "chapter", parameterName: "id", onClickAction: { (parameterContent: String?) in + let listener = ClassBasedOnClickListener(schemeName: "chaptertapped", className: "chapter", parameterName: "id", onClickAction: { (parameterContent: String?) in print("chapter with id: " + (parameterContent ?? "-") + " clicked") }) config.classBasedOnClickListeners.append(listener) From c490695b193e10349540e09ca01aa64491eb6f69 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Fri, 9 Sep 2016 12:05:46 +0200 Subject: [PATCH 08/12] Expose font settings and update the readers layout if font values changed --- .../Base.lproj/Main.storyboard | 18 +- Source/FolioReaderAudioPlayer.swift | 25 +-- Source/FolioReaderCenter.swift | 41 +---- Source/FolioReaderContainer.swift | 4 +- Source/FolioReaderFontsMenu.swift | 164 +++++++++--------- Source/FolioReaderHighlightList.swift | 2 +- Source/FolioReaderKit.swift | 52 ++++-- Source/FolioReaderPage.swift | 32 ++-- Source/FolioReaderPageIndicator.swift | 40 ++++- Source/FolioReaderPlayerMenu.swift | 4 +- Source/Models/Highlight+Helper.swift | 2 +- 11 files changed, 203 insertions(+), 181 deletions(-) diff --git a/Example/StoryboardExample/Base.lproj/Main.storyboard b/Example/StoryboardExample/Base.lproj/Main.storyboard index 5ac738da4..9d5f6ab52 100644 --- a/Example/StoryboardExample/Base.lproj/Main.storyboard +++ b/Example/StoryboardExample/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -19,7 +19,7 @@ - + @@ -27,19 +27,27 @@ - - + + + + + - + + + + + + diff --git a/Source/FolioReaderAudioPlayer.swift b/Source/FolioReaderAudioPlayer.swift index 708cf729d..a42ace299 100644 --- a/Source/FolioReaderAudioPlayer.swift +++ b/Source/FolioReaderAudioPlayer.swift @@ -152,7 +152,7 @@ public class FolioReaderAudioPlayer: NSObject { func play() { if book.hasAudio() { - guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return } + guard let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage else { return } currentPage.webView.js("playAudio()") } else { readCurrentSentence() @@ -212,7 +212,7 @@ public class FolioReaderAudioPlayer: NSObject { func playPrevChapter() { stopPlayerTimer() // Wait for "currentPage" to update, then request to play audio - FolioReader.sharedInstance.readerCenter.changePageToPrevious { + FolioReader.sharedInstance.readerCenter?.changePageToPrevious { if self.isPlaying() { self.play() } else { @@ -224,7 +224,7 @@ public class FolioReaderAudioPlayer: NSObject { func playNextChapter() { stopPlayerTimer() // Wait for "currentPage" to update, then request to play audio - FolioReader.sharedInstance.readerCenter.changePageToNext { + FolioReader.sharedInstance.readerCenter?.changePageToNext { if self.isPlaying() { self.play() } @@ -293,7 +293,7 @@ public class FolioReaderAudioPlayer: NSObject { // get the fragment ID so we can "mark" it in the webview let textParts = textFragment!.componentsSeparatedByString("#") let fragmentID = textParts[1]; - FolioReader.sharedInstance.readerCenter.audioMark(href: currentHref, fragmentID: fragmentID) + FolioReader.sharedInstance.readerCenter?.audioMark(href: currentHref, fragmentID: fragmentID) return true } @@ -353,18 +353,23 @@ public class FolioReaderAudioPlayer: NSObject { // MARK: TTS Sentence func speakSentence() { - guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return } + guard let + _readerCenter = FolioReader.sharedInstance.readerCenter, + currentPage = _readerCenter.currentPage else { + return + } + let sentence = currentPage.webView.js("getSentenceWithIndex('\(book.playbackActiveClass())')") if sentence != nil { - let chapter = FolioReader.sharedInstance.readerCenter.getCurrentChapter() + let chapter = _readerCenter.getCurrentChapter() let href = chapter != nil ? chapter!.href : ""; playText(href, text: sentence!) } else { - if FolioReader.sharedInstance.readerCenter.isLastPage() { + if _readerCenter.isLastPage() { stop() } else { - FolioReader.sharedInstance.readerCenter.changePageToNext() + _readerCenter.changePageToNext() } } } @@ -378,7 +383,7 @@ public class FolioReaderAudioPlayer: NSObject { } else { if synthesizer.speaking { stopSynthesizer(immediate: false, completion: { - if let currentPage = FolioReader.sharedInstance.readerCenter.currentPage { + if let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage { currentPage.webView.js("resetCurrentSentenceIndex()") } self.speakSentence() @@ -463,7 +468,7 @@ public class FolioReaderAudioPlayer: NSObject { the `currentPage` in ReaderCenter may not have updated just yet */ func getCurrentChapterName() -> String? { - guard let chapter = FolioReader.sharedInstance.readerCenter.getCurrentChapter() else { + guard let chapter = FolioReader.sharedInstance.readerCenter?.getCurrentChapter() else { return nil } diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift index 51a45fdf5..43a1bebfd 100755 --- a/Source/FolioReaderCenter.swift +++ b/Source/FolioReaderCenter.swift @@ -355,25 +355,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo html = html?.stringByReplacingOccurrencesOfString("", withString: toInject) // Font class name - var classes = "" - let currentFontName = FolioReader.currentFontName - switch currentFontName { - case 0: - classes = "andada" - break - case 1: - classes = "lato" - break - case 2: - classes = "lora" - break - case 3: - classes = "raleway" - break - default: - break - } - + var classes = FolioReader.currentFont.cssIdentifier classes += " "+FolioReader.currentMediaOverlayStyle.className() // Night mode @@ -382,26 +364,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo } // Font Size - let currentFontSize = FolioReader.currentFontSize - switch currentFontSize { - case 0: - classes += " textSizeOne" - break - case 1: - classes += " textSizeTwo" - break - case 2: - classes += " textSizeThree" - break - case 3: - classes += " textSizeFour" - break - case 4: - classes += " textSizeFive" - break - default: - break - } + classes += " \(FolioReader.currentFontSize.cssIdentifier)" html = html?.stringByReplacingOccurrencesOfString(" FolioReaderFont? { + var font: FolioReaderFont? + switch fontName { + case "andada" : font = .Andada + case "lato" : font = .Lato + case "lora" : font = .Lora + case "raleway" : font = .Raleway + default : break + } + return font + } + + public var cssIdentifier: String { + switch self { + case .Andada : return "andada" + case .Lato : return "lato" + case .Lora : return "lora" + case .Raleway : return "raleway" + } + } +} + +public enum FolioReaderFontSize: Int { + case XS = 0 + case S + case M + case L + case XL + + public static func folioReaderFontSize(fontSizeStringRepresentation fontSizeStringRepresentation: String) -> FolioReaderFontSize? { + var fontSize: FolioReaderFontSize? + switch fontSizeStringRepresentation { + case "textSizeOne" : fontSize = .XS + case "textSizeTwo" : fontSize = .S + case "textSizeThree" : fontSize = .M + case "textSizeFour" : fontSize = .L + case "textSizeFive" : fontSize = .XL + default : break + } + return fontSize + } + + public var cssIdentifier: String { + switch self { + case .XS : return "textSizeOne" + case .S : return "textSizeTwo" + case .M : return "textSizeThree" + case .L : return "textSizeFour" + case .XL : return "textSizeFive" + } + } +} + class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRecognizerDelegate { var menuView: UIView! @@ -99,7 +158,8 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe fontName.segments[1].titleFont = UIFont(name: "Lato-Regular", size: 18)! fontName.segments[2].titleFont = UIFont(name: "Lora-Regular", size: 18)! fontName.segments[3].titleFont = UIFont(name: "Raleway-Regular", size: 18)! - fontName.selectSegmentAtIndex(FolioReader.currentFontName) + + fontName.selectSegmentAtIndex(FolioReader.currentFont.rawValue) menuView.addSubview(fontName) // Separator 2 @@ -122,7 +182,7 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe slider.backgroundColor = UIColor.clearColor() slider.tintColor = readerConfig.nightModeSeparatorColor slider.minimumValue = 0 - slider.value = CGFloat(FolioReader.currentFontSize) + slider.value = CGFloat(FolioReader.currentFontSize.rawValue) slider.addTarget(self, action: #selector(FolioReaderFontsMenu.sliderValueChanged(_:)), forControlEvents: UIControlEvents.ValueChanged) // Force remove fill color @@ -181,66 +241,24 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe // MARK: - SMSegmentView delegate func segmentView(segmentView: SMSegmentView, didSelectSegmentAtIndex index: Int) { - guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return } + guard let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage else { return } if segmentView.tag == 1 { - FolioReader.nightMode = Bool(index) - - let readerCenter = FolioReader.sharedInstance.readerCenter - - switch index { - case 0: - currentPage.webView.js("nightMode(false)") - UIView.animateWithDuration(0.6, animations: { - self.menuView.backgroundColor = UIColor.whiteColor() - readerCenter.collectionView.backgroundColor = UIColor.whiteColor() - readerCenter.configureNavBar() - readerCenter.scrollScrubber?.updateColors() - }) - break - case 1: - currentPage.webView.js("nightMode(true)") - UIView.animateWithDuration(0.6, animations: { - self.menuView.backgroundColor = readerConfig.nightModeMenuBackground - readerCenter.collectionView.backgroundColor = readerConfig.nightModeBackground - readerCenter.configureNavBar() - readerCenter.scrollScrubber?.updateColors() - }) - break - default: - break - } - - NSNotificationCenter.defaultCenter().postNotificationName("needRefreshPageMode", object: nil) - } - - if segmentView.tag == 2 { - switch index { - case 0: - currentPage.webView.js("setFontName('andada')") - break - case 1: - currentPage.webView.js("setFontName('lato')") - break - case 2: - currentPage.webView.js("setFontName('lora')") - break - case 3: - currentPage.webView.js("setFontName('raleway')") - break - default: - break - } - - FolioReader.currentFontName = index - } - - if segmentView.tag == 3 { - guard FolioReader.currentScrollDirection != index else { return } + FolioReader.nightMode = Bool(index == 1) + + UIView.animateWithDuration(0.6, animations: { + self.menuView.backgroundColor = (FolioReader.nightMode ?readerConfig.nightModeBackground : UIColor.whiteColor()) + }) + + } else if segmentView.tag == 2 { + + FolioReader.currentFont = FolioReaderFont(rawValue: index)! + + } else if segmentView.tag == 3 { + + guard FolioReader.currentScrollDirection != index else { return } - let direction = FolioReaderScrollDirection(rawValue: index) ?? .vertical - FolioReader.sharedInstance.readerCenter.setScrollDirection(direction) FolioReader.currentScrollDirection = index } } @@ -248,30 +266,12 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe // MARK: - Font slider changed func sliderValueChanged(sender: HADiscreteSlider) { - guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return } + guard let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage else { return } let index = Int(sender.value) - - switch index { - case 0: - currentPage.webView.js("setFontSize('textSizeOne')") - break - case 1: - currentPage.webView.js("setFontSize('textSizeTwo')") - break - case 2: - currentPage.webView.js("setFontSize('textSizeThree')") - break - case 3: - currentPage.webView.js("setFontSize('textSizeFour')") - break - case 4: - currentPage.webView.js("setFontSize('textSizeFive')") - break - default: - break - } - - FolioReader.currentFontSize = index + + if let _fontSize = FolioReaderFontSize(rawValue: index) { + FolioReader.currentFontSize = _fontSize + } } // MARK: - Gestures @@ -280,7 +280,7 @@ class FolioReaderFontsMenu: UIViewController, SMSegmentViewDelegate, UIGestureRe dismiss() if readerConfig.shouldHideNavigationOnTap == false { - FolioReader.sharedInstance.readerCenter.showBars() + FolioReader.sharedInstance.readerCenter?.showBars() } } diff --git a/Source/FolioReaderHighlightList.swift b/Source/FolioReaderHighlightList.swift index fba9967d7..11f63c1cd 100644 --- a/Source/FolioReaderHighlightList.swift +++ b/Source/FolioReaderHighlightList.swift @@ -125,7 +125,7 @@ class FolioReaderHighlightList: UITableViewController { override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let highlight = highlights[indexPath.row] - FolioReader.sharedInstance.readerCenter.changePageWith(page: highlight.page, andFragment: highlight.highlightId) + FolioReader.sharedInstance.readerCenter?.changePageWith(page: highlight.page, andFragment: highlight.highlightId) dismiss() } diff --git a/Source/FolioReaderKit.swift b/Source/FolioReaderKit.swift index 529dc178e..405b22e24 100755 --- a/Source/FolioReaderKit.swift +++ b/Source/FolioReaderKit.swift @@ -74,7 +74,7 @@ public class FolioReader: NSObject { public static let sharedInstance = FolioReader() static let defaults = NSUserDefaults.standardUserDefaults() public weak var delegate: FolioReaderDelegate? - public weak var readerCenter: FolioReaderCenter! + public weak var readerCenter: FolioReaderCenter? public weak var readerContainer: FolioReaderContainer! public weak var readerAudioPlayer: FolioReaderAudioPlayer? @@ -97,29 +97,48 @@ public class FolioReader: NSObject { } /// Check if current theme is Night mode - static var nightMode: Bool { + public static var nightMode: Bool { get { return FolioReader.defaults.boolForKey(kNightMode) } set (value) { FolioReader.defaults.setBool(value, forKey: kNightMode) + FolioReader.defaults.synchronize() + + if let _readerCenter = FolioReader.sharedInstance.readerCenter { + UIView.animateWithDuration(0.6, animations: { + _readerCenter.currentPage?.webView.js("nightMode(\(nightMode))") + _readerCenter.pageIndicatorView?.reloadColors() + _readerCenter.configureNavBar() + _readerCenter.scrollScrubber?.updateColors() + _readerCenter.collectionView.backgroundColor = (nightMode ? readerConfig.nightModeBackground : UIColor.whiteColor()) + }, completion: { (finished: Bool) in + NSNotificationCenter.defaultCenter().postNotificationName("needRefreshPageMode", object: nil) + }) + } } } - + /// Check current font name - static var currentFontName: Int { - get { return FolioReader.defaults.valueForKey(kCurrentFontFamily) as! Int } - set (value) { - FolioReader.defaults.setValue(value, forKey: kCurrentFontFamily) + public static var currentFont: FolioReaderFont { + get { return FolioReaderFont(rawValue: FolioReader.defaults.valueForKey(kCurrentFontFamily) as! Int)! } + set (font) { + FolioReader.defaults.setValue(font.rawValue, forKey: kCurrentFontFamily) + + FolioReader.sharedInstance.readerCenter?.currentPage?.webView.js("setFontName('\(font.cssIdentifier)')") } } /// Check current font size - static var currentFontSize: Int { - get { return FolioReader.defaults.valueForKey(kCurrentFontSize) as! Int } + public static var currentFontSize: FolioReaderFontSize { + get { return FolioReaderFontSize(rawValue: FolioReader.defaults.valueForKey(kCurrentFontSize) as! Int)! } set (value) { - FolioReader.defaults.setValue(value, forKey: kCurrentFontSize) + FolioReader.defaults.setValue(value.rawValue, forKey: kCurrentFontSize) + + if let _currentPage = FolioReader.sharedInstance.readerCenter?.currentPage { + _currentPage.webView.js("setFontSize('\(currentFontSize.cssIdentifier)')") + } } } - + /// Check current audio rate, the speed of speech voice static var currentAudioRate: Int { get { return FolioReader.defaults.valueForKey(kCurrentAudioRate) as! Int } @@ -145,13 +164,18 @@ public class FolioReader: NSObject { } /// Check the current scroll direction - static var currentScrollDirection: Int { + public static var currentScrollDirection: Int { get { return FolioReader.defaults.valueForKey(kCurrentScrollDirection) as! Int } set (value) { FolioReader.defaults.setValue(value, forKey: kCurrentScrollDirection) + + if let _readerCenter = FolioReader.sharedInstance.readerCenter { + let direction = FolioReaderScrollDirection(rawValue: currentScrollDirection) ?? .vertical + _readerCenter.setScrollDirection(direction) + } } } - + // MARK: - Get Cover Image /** @@ -194,7 +218,7 @@ public class FolioReader: NSObject { public class func saveReaderState() { guard FolioReader.isReaderOpen else { return } - if let currentPage = FolioReader.sharedInstance.readerCenter.currentPage { + if let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage { let position = [ "pageNumber": currentPageNumber, "pageOffsetX": currentPage.webView.scrollView.contentOffset.x, diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index efb087ed5..2749bb4e3 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -83,8 +83,8 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture } let statusbarHeight = UIApplication.sharedApplication().statusBarFrame.size.height - let navBarHeight = FolioReader.sharedInstance.readerCenter.navigationController?.navigationBar.frame.size.height - let navTotal = readerConfig.shouldHideNavigationOnTap ? 0 : statusbarHeight + navBarHeight! + let navBarHeight = FolioReader.sharedInstance.readerCenter?.navigationController?.navigationBar.frame.size.height ?? CGFloat(0) + let navTotal = readerConfig.shouldHideNavigationOnTap ? 0 : statusbarHeight + navBarHeight let paddingTop: CGFloat = 20 let paddingBottom: CGFloat = 30 @@ -176,7 +176,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture let decoded = url.absoluteString.stringByRemovingPercentEncoding as String! let playID = decoded.substringFromIndex(decoded.startIndex.advancedBy(13)) - let chapter = FolioReader.sharedInstance.readerCenter.getCurrentChapter() + let chapter = FolioReader.sharedInstance.readerCenter?.getCurrentChapter() let href = chapter != nil ? chapter!.href : ""; FolioReader.sharedInstance.readerAudioPlayer?.playAudio(href, fragmentID: playID) @@ -197,7 +197,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture } let href = splitedPath[1].stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "/")) - let hrefPage = FolioReader.sharedInstance.readerCenter.findPageByHref(href)+1 + let hrefPage = (FolioReader.sharedInstance.readerCenter?.findPageByHref(href) ?? 0) + 1 if hrefPage == pageNumber { // Handle internal #anchor @@ -206,7 +206,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture return false } } else { - FolioReader.sharedInstance.readerCenter.changePageWith(href: href, animated: true) + FolioReader.sharedInstance.readerCenter?.changePageWith(href: href, animated: true) } return false @@ -227,12 +227,12 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture if #available(iOS 9.0, *) { let safariVC = SFSafariViewController(URL: request.URL!) safariVC.view.tintColor = readerConfig.tintColor - FolioReader.sharedInstance.readerCenter.presentViewController(safariVC, animated: true, completion: nil) + FolioReader.sharedInstance.readerCenter?.presentViewController(safariVC, animated: true, completion: nil) } else { let webViewController = WebViewController(url: request.URL!) let nav = UINavigationController(rootViewController: webViewController) nav.view.tintColor = readerConfig.tintColor - FolioReader.sharedInstance.readerCenter.presentViewController(nav, animated: true, completion: nil) + FolioReader.sharedInstance.readerCenter?.presentViewController(nav, animated: true, completion: nil) } return false } else { @@ -279,7 +279,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture public func handleTapGesture(recognizer: UITapGestureRecognizer) { // webView.setMenuVisible(false) - if FolioReader.sharedInstance.readerCenter.navigationController!.navigationBarHidden { + if let _navigationController = FolioReader.sharedInstance.readerCenter?.navigationController where _navigationController.navigationBarHidden { let menuIsVisibleRef = menuIsVisible let selected = webView.js("getSelectedText()") @@ -292,13 +292,13 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture dispatch_after(dispatchTime, dispatch_get_main_queue(), { if self.shouldShowBar && !menuIsVisibleRef { - FolioReader.sharedInstance.readerCenter.toggleBars() + FolioReader.sharedInstance.readerCenter?.toggleBars() } self.shouldShowBar = true }) } } else if readerConfig.shouldHideNavigationOnTap == true { - FolioReader.sharedInstance.readerCenter.hideBars() + FolioReader.sharedInstance.readerCenter?.hideBars() } // Reset menu @@ -385,7 +385,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture - parameter ID: The ID */ func audioMarkID(ID: String) { - guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return } + guard let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage else { return } currentPage.webView.js("audioMarkID('\(book.playbackActiveClass())','\(ID)')") } @@ -492,11 +492,11 @@ extension UIWebView { let shareImage = UIAlertAction(title: readerConfig.localizedShareImageQuote, style: .Default, handler: { (action) -> Void in if self.isShare { if let textToShare = self.js("getHighlightContent()") { - FolioReader.sharedInstance.readerCenter.presentQuoteShare(textToShare) + FolioReader.sharedInstance.readerCenter?.presentQuoteShare(textToShare) } } else { if let textToShare = self.js("getSelectedText()") { - FolioReader.sharedInstance.readerCenter.presentQuoteShare(textToShare) + FolioReader.sharedInstance.readerCenter?.presentQuoteShare(textToShare) self.userInteractionEnabled = false self.userInteractionEnabled = true } @@ -507,11 +507,11 @@ extension UIWebView { let shareText = UIAlertAction(title: readerConfig.localizedShareTextQuote, style: .Default) { (action) -> Void in if self.isShare { if let textToShare = self.js("getHighlightContent()") { - FolioReader.sharedInstance.readerCenter.shareHighlight(textToShare, rect: sender.menuFrame) + FolioReader.sharedInstance.readerCenter?.shareHighlight(textToShare, rect: sender.menuFrame) } } else { if let textToShare = self.js("getSelectedText()") { - FolioReader.sharedInstance.readerCenter.shareHighlight(textToShare, rect: sender.menuFrame) + FolioReader.sharedInstance.readerCenter?.shareHighlight(textToShare, rect: sender.menuFrame) } } self.setMenuVisible(false) @@ -523,7 +523,7 @@ extension UIWebView { alertController.addAction(shareText) alertController.addAction(cancel) - FolioReader.sharedInstance.readerCenter.presentViewController(alertController, animated: true, completion: nil) + FolioReader.sharedInstance.readerCenter?.presentViewController(alertController, animated: true, completion: nil) } func colors(sender: UIMenuController?) { diff --git a/Source/FolioReaderPageIndicator.swift b/Source/FolioReaderPageIndicator.swift index aa867b105..d3876087c 100644 --- a/Source/FolioReaderPageIndicator.swift +++ b/Source/FolioReaderPageIndicator.swift @@ -56,17 +56,39 @@ class FolioReaderPageIndicator: UIView { if updateShadow { layer.shadowPath = UIBezierPath(rect: bounds).CGPath - - // Update colors - let color = isNight(readerConfig.nightModeBackground, UIColor.whiteColor()) - backgroundColor = color - layer.shadowColor = color.CGColor - - minutesLabel.textColor = isNight(UIColor(white: 5, alpha: 0.3), UIColor(white: 0, alpha: 0.6)) - pagesLabel.textColor = isNight(UIColor(white: 5, alpha: 0.6), UIColor(white: 0, alpha: 0.9)) + reloadColors() } } - + + func reloadColors() { + let color = isNight(readerConfig.nightModeBackground, UIColor.whiteColor()) + backgroundColor = color + + // Animate the shadow color change + let animation = CABasicAnimation(keyPath: "shadowColor") + let currentColor = UIColor(CGColor: layer.shadowColor!) + animation.fromValue = currentColor.CGColor + animation.toValue = color.CGColor + animation.fillMode = kCAFillModeForwards + animation.removedOnCompletion = false + animation.duration = 0.6 + animation.delegate = self + animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) + layer.addAnimation(animation, forKey: "shadowColor") + + minutesLabel.textColor = isNight(UIColor(white: 5, alpha: 0.3), UIColor(white: 0, alpha: 0.6)) + pagesLabel.textColor = isNight(UIColor(white: 5, alpha: 0.6), UIColor(white: 0, alpha: 0.9)) + } + + override func animationDidStop(anim: CAAnimation, finished flag: Bool) { + // Set the shadow color to the final value of the animation is done + print("animationDidStop animation: \(anim)") + if let _keyPath = anim.valueForKeyPath("keyPath") as? String where _keyPath == "shadowColor" { + let color = isNight(readerConfig.nightModeBackground, UIColor.whiteColor()) + layer.shadowColor = color.CGColor + } + } + private func reloadViewWithPage(page: Int) { let pagesRemaining = FolioReader.needsRTLChange ? totalPages-(totalPages-page+1) : totalPages-page diff --git a/Source/FolioReaderPlayerMenu.swift b/Source/FolioReaderPlayerMenu.swift index 333a79cbc..423c4dad6 100644 --- a/Source/FolioReaderPlayerMenu.swift +++ b/Source/FolioReaderPlayerMenu.swift @@ -255,7 +255,7 @@ class FolioReaderPlayerMenu: UIViewController, SMSegmentViewDelegate, UIGestureR } // update the current page style - if let currentPage = FolioReader.sharedInstance.readerCenter.currentPage { + if let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage { currentPage.webView.js("setMediaOverlayStyle(\"\(FolioReader.currentMediaOverlayStyle.className())\")") } } @@ -264,7 +264,7 @@ class FolioReaderPlayerMenu: UIViewController, SMSegmentViewDelegate, UIGestureR dismiss() if readerConfig.shouldHideNavigationOnTap == false { - FolioReader.sharedInstance.readerCenter.showBars() + FolioReader.sharedInstance.readerCenter?.showBars() } } diff --git a/Source/Models/Highlight+Helper.swift b/Source/Models/Highlight+Helper.swift index b14284607..46c9cb82b 100644 --- a/Source/Models/Highlight+Helper.swift +++ b/Source/Models/Highlight+Helper.swift @@ -244,7 +244,7 @@ extension Highlight { - returns: The removed id */ public static func removeFromHTMLById(highlightId: String) -> String? { - guard let currentPage = FolioReader.sharedInstance.readerCenter.currentPage else { return nil } + guard let currentPage = FolioReader.sharedInstance.readerCenter?.currentPage else { return nil } if let removedId = currentPage.webView.js("removeHighlightById('\(highlightId)')") { return removedId From ac27c5d1cf7333c5d880aadf1c18ded66fd14522 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Wed, 14 Sep 2016 10:52:59 +0200 Subject: [PATCH 09/12] Fix not working highlight click if using a class based click listener --- Source/Resources/Bridge.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Resources/Bridge.js b/Source/Resources/Bridge.js index cf44f57ce..88df53cba 100755 --- a/Source/Resources/Bridge.js +++ b/Source/Resources/Bridge.js @@ -145,7 +145,8 @@ var getRectForSelectedText = function(elm) { // Method that call that a hightlight was clicked // with URL scheme and rect informations var callHighlightURL = function(elm) { - var URLBase = "highlight://"; + event.stopPropagation(); + var URLBase = "highlight://"; var currentHighlightRect = getRectForSelectedText(elm); thisHighlight = elm; @@ -595,5 +596,6 @@ function addClassBasedOnClickListener(schemeName, className, parameterName) { } var onClassBasedListenerClick = function(schemeName, parameterContent) { + event.stopPropagation(); window.location = schemeName + "://" + encodeURIComponent(parameterContent); } \ No newline at end of file From 5467a1a7ebfd157c2166917ed4058c505f1971ba Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Wed, 14 Sep 2016 13:14:48 +0200 Subject: [PATCH 10/12] Escape parameter content of class based onclick listeners --- Source/Resources/Bridge.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Source/Resources/Bridge.js b/Source/Resources/Bridge.js index 88df53cba..1696f767f 100755 --- a/Source/Resources/Bridge.js +++ b/Source/Resources/Bridge.js @@ -591,11 +591,10 @@ function addClassBasedOnClickListener(schemeName, className, parameterName) { // Get the content from the given parameterName var parameterContent = element.getAttribute(parameterName); // Add the on click logic - element.setAttribute("onclick", "onClassBasedListenerClick(\"" + schemeName + "\", \"" + parameterContent + "\");"); + element.setAttribute("onclick", "onClassBasedListenerClick(\"" + schemeName + "\", \"" + encodeURIComponent(parameterContent) + "\");"); } } var onClassBasedListenerClick = function(schemeName, parameterContent) { - event.stopPropagation(); - window.location = schemeName + "://" + encodeURIComponent(parameterContent); + window.location = schemeName + "://" + parameterContent; } \ No newline at end of file From 2649840712cffb193f8ff5fd734a8d7ab1d172cb Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Thu, 15 Sep 2016 20:13:51 +0200 Subject: [PATCH 11/12] Refactor class based on click listener to use `querySelector(All)` --- .../ExampleFolioReaderContainer.swift | 2 +- Source/FolioReaderConfig.swift | 20 +++++++------ Source/FolioReaderPage.swift | 2 +- Source/Resources/Bridge.js | 28 +++++++++++++------ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Example/StoryboardExample/ExampleFolioReaderContainer.swift b/Example/StoryboardExample/ExampleFolioReaderContainer.swift index 52afbe2b8..493e4ed2d 100644 --- a/Example/StoryboardExample/ExampleFolioReaderContainer.swift +++ b/Example/StoryboardExample/ExampleFolioReaderContainer.swift @@ -21,7 +21,7 @@ class ExampleFolioReaderContainer: FolioReaderContainer { // Print the chapter ID if one was clicked // A chapter in "The Silver Chair" looks like this "
" // To knwo if a user tapped on a chapter we can listen to events on the class "chapter" and receive the id value - let listener = ClassBasedOnClickListener(schemeName: "chaptertapped", className: "chapter", parameterName: "id", onClickAction: { (parameterContent: String?) in + let listener = ClassBasedOnClickListener(schemeName: "chaptertapped", querySelector: ".chapter", attributeName: "id", onClickAction: { (parameterContent: String?) in print("chapter with id: " + (parameterContent ?? "-") + " clicked") }) config.classBasedOnClickListeners.append(listener) diff --git a/Source/FolioReaderConfig.swift b/Source/FolioReaderConfig.swift index 463ca883f..ba0b74d5b 100755 --- a/Source/FolioReaderConfig.swift +++ b/Source/FolioReaderConfig.swift @@ -52,20 +52,24 @@ public struct ClassBasedOnClickListener { /// The name of the URL scheme which should be used. Note: Make sure that the given `String` is a valid as scheme name. public var schemeName : String - /// The HTML class name to which the listener should be added. - public var className : String + /// The query selector for the elements which the listener should be added to. See https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector for further information about query selectors. + public var querySelector : String - /// The name of the parameter whose content should be passed to the `onClickAction` action - public var parameterName : String + /// The name of the attribute whose content should be passed to the `onClickAction` action. + public var attributeName : String + + /// Whether the listener should be added to all found elements or only to the first one. See https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll for further information. The default value is `true`. + public var selectAll : Bool /// The closure which will be called if the specified class was clicked. public var onClickAction : ((parameterContent: String?) -> Void) - /// Initializes a `ClassBasedOnClickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events. - public init(schemeName: String, className: String, parameterName: String, onClickAction: ((parameterContent: String?) -> Void)) { + /// Initializes a `ClassBasedOnClickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events. The default `selectAll` value is `true`. + public init(schemeName: String, querySelector: String, attributeName: String, selectAll: Bool = true, onClickAction: ((attributeContent: String?) -> Void)) { self.schemeName = schemeName.lowercaseString - self.className = className - self.parameterName = parameterName + self.querySelector = querySelector + self.attributeName = attributeName + self.selectAll = selectAll self.onClickAction = onClickAction } } diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index ea88c47fa..19c14e7af 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -420,7 +420,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture private func setupClassBasedOnClickListeners() { for listener in readerConfig.classBasedOnClickListeners { - self.webView.js("addClassBasedOnClickListener(\"\(listener.schemeName)\", \"\(listener.className)\", \"\(listener.parameterName)\")"); + self.webView.js("addClassBasedOnClickListener(\"\(listener.schemeName)\", \"\(listener.querySelector)\", \"\(listener.attributeName)\", \"\(listener.selectAll)\")"); } } } diff --git a/Source/Resources/Bridge.js b/Source/Resources/Bridge.js index 1696f767f..927e66524 100755 --- a/Source/Resources/Bridge.js +++ b/Source/Resources/Bridge.js @@ -583,18 +583,28 @@ function wrappingSentencesWithinPTags(){ // Class based onClick listener -function addClassBasedOnClickListener(schemeName, className, parameterName) { - // Get all elements with the given className - var elements = document.getElementsByClassName(className); - for (elementIndex = 0; elementIndex < elements.length; elementIndex++) { - var element = elements[elementIndex]; - // Get the content from the given parameterName - var parameterContent = element.getAttribute(parameterName); - // Add the on click logic - element.setAttribute("onclick", "onClassBasedListenerClick(\"" + schemeName + "\", \"" + encodeURIComponent(parameterContent) + "\");"); +function addClassBasedOnClickListener(schemeName, querySelector, attributeName, selectAll) { + if (selectAll) { + // Get all elements with the given query selector + var elements = document.querySelectorAll(querySelector); + for (elementIndex = 0; elementIndex < elements.length; elementIndex++) { + var element = elements[elementIndex]; + addClassBasedOnClickListenerToElement(element, schemeName, attributeName); + } + } else { + // Get the first element with the given query selector + var element = document.querySelector(querySelector); + addClassBasedOnClickListenerToElement(element, schemeName, attributeName); } } +function addClassBasedOnClickListenerToElement(element, schemeName, attributeName) { + // Get the content from the given attribute name + var attributeContent = element.getAttribute(attributeName); + // Add the on click logic + element.setAttribute("onclick", "onClassBasedListenerClick(\"" + schemeName + "\", \"" + encodeURIComponent(attributeContent) + "\");"); +} + var onClassBasedListenerClick = function(schemeName, parameterContent) { window.location = schemeName + "://" + parameterContent; } \ No newline at end of file From 06616035d1c1c2a1679c5f7f732b11911601e6f5 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Thu, 15 Sep 2016 20:17:26 +0200 Subject: [PATCH 12/12] Adjust code style of new code to match the style from the project --- Source/FolioReaderAudioPlayer.swift | 10 +++++----- Source/FolioReaderKit.swift | 12 ++++++------ Source/ScrollScrubber.swift | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Source/FolioReaderAudioPlayer.swift b/Source/FolioReaderAudioPlayer.swift index a42ace299..69dc68e7d 100644 --- a/Source/FolioReaderAudioPlayer.swift +++ b/Source/FolioReaderAudioPlayer.swift @@ -354,22 +354,22 @@ public class FolioReaderAudioPlayer: NSObject { func speakSentence() { guard let - _readerCenter = FolioReader.sharedInstance.readerCenter, - currentPage = _readerCenter.currentPage else { + readerCenter = FolioReader.sharedInstance.readerCenter, + currentPage = readerCenter.currentPage else { return } let sentence = currentPage.webView.js("getSentenceWithIndex('\(book.playbackActiveClass())')") if sentence != nil { - let chapter = _readerCenter.getCurrentChapter() + let chapter = readerCenter.getCurrentChapter() let href = chapter != nil ? chapter!.href : ""; playText(href, text: sentence!) } else { - if _readerCenter.isLastPage() { + if readerCenter.isLastPage() { stop() } else { - _readerCenter.changePageToNext() + readerCenter.changePageToNext() } } } diff --git a/Source/FolioReaderKit.swift b/Source/FolioReaderKit.swift index 405b22e24..87b3f0cd2 100755 --- a/Source/FolioReaderKit.swift +++ b/Source/FolioReaderKit.swift @@ -103,13 +103,13 @@ public class FolioReader: NSObject { FolioReader.defaults.setBool(value, forKey: kNightMode) FolioReader.defaults.synchronize() - if let _readerCenter = FolioReader.sharedInstance.readerCenter { + if let readerCenter = FolioReader.sharedInstance.readerCenter { UIView.animateWithDuration(0.6, animations: { - _readerCenter.currentPage?.webView.js("nightMode(\(nightMode))") - _readerCenter.pageIndicatorView?.reloadColors() - _readerCenter.configureNavBar() - _readerCenter.scrollScrubber?.updateColors() - _readerCenter.collectionView.backgroundColor = (nightMode ? readerConfig.nightModeBackground : UIColor.whiteColor()) + readerCenter.currentPage?.webView.js("nightMode(\(nightMode))") + readerCenter.pageIndicatorView?.reloadColors() + readerCenter.configureNavBar() + readerCenter.scrollScrubber?.reloadColors() + readerCenter.collectionView.backgroundColor = (nightMode ? readerConfig.nightModeBackground : UIColor.whiteColor()) }, completion: { (finished: Bool) in NSNotificationCenter.defaultCenter().postNotificationName("needRefreshPageMode", object: nil) }) diff --git a/Source/ScrollScrubber.swift b/Source/ScrollScrubber.swift index 502a179b5..a9f4add29 100644 --- a/Source/ScrollScrubber.swift +++ b/Source/ScrollScrubber.swift @@ -51,7 +51,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate { self.frame = frame - updateColors() + reloadColors() // less obtrusive knob and fixes jump: http://stackoverflow.com/a/22301039/484780 let thumbImg = UIImage(readerImageNamed: "knob") @@ -66,7 +66,7 @@ class ScrollScrubber: NSObject, UIScrollViewDelegate { slider.addTarget(self, action: #selector(ScrollScrubber.sliderTouchUp(_:)), forControlEvents: .TouchUpOutside) } - func updateColors() { + func reloadColors() { slider.minimumTrackTintColor = readerConfig.tintColor slider.maximumTrackTintColor = isNight(readerConfig.nightModeSeparatorColor, readerConfig.menuSeparatorColor) }