Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.

Commit ccf1ffb

Browse files
author
Hans Seiffert
committed
Merge branch 'issue/132' of github.com:smartmobilefactory/FolioReaderKit into issue/133
2 parents 0661603 + 2649840 commit ccf1ffb

6 files changed

Lines changed: 49 additions & 26 deletions

File tree

Example/StoryboardExample/ExampleFolioReaderContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ExampleFolioReaderContainer: FolioReaderContainer {
2121
// Print the chapter ID if one was clicked
2222
// A chapter in "The Silver Chair" looks like this "<section class="chapter" title="Chapter I" epub:type="chapter" id="id70364673704880">"
2323
// To knwo if a user tapped on a chapter we can listen to events on the class "chapter" and receive the id value
24-
let listener = ClassBasedOnClickListener(schemeName: "chaptertapped", className: "chapter", parameterName: "id", onClickAction: { (parameterContent: String?) in
24+
let listener = ClassBasedOnClickListener(schemeName: "chaptertapped", querySelector: ".chapter", attributeName: "id", onClickAction: { (parameterContent: String?) in
2525
print("chapter with id: " + (parameterContent ?? "-") + " clicked")
2626
})
2727
config.classBasedOnClickListeners.append(listener)

Source/FolioReaderCenter.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var isScrolling = false
2121

2222
public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
2323

24+
/// This delegate receives the events from the current `FolioReaderPage`s delegate.
25+
public weak var pageDelegate: FolioReaderPageDelegate?
26+
2427
var collectionView: UICollectionView!
2528
let collectionViewLayout = UICollectionViewFlowLayout()
2629
var loadingView: UIActivityIndicatorView!
@@ -1056,7 +1059,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
10561059

10571060
extension FolioReaderCenter: FolioReaderPageDelegate {
10581061

1059-
func pageDidLoad(page: FolioReaderPage) {
1062+
public func pageDidLoad(page: FolioReaderPage) {
10601063

10611064
if let position = FolioReader.defaults.valueForKey(kBookId) as? NSDictionary {
10621065
let pageNumber = position["pageNumber"]! as! Int
@@ -1088,6 +1091,8 @@ extension FolioReaderCenter: FolioReaderPageDelegate {
10881091
let offsetPoint = self.currentWebViewScrollPositions[page.pageNumber - 1] {
10891092
page.webView.scrollView.setContentOffset(offsetPoint, animated: false)
10901093
}
1094+
1095+
self.pageDelegate?.pageDidLoad(page)
10911096
}
10921097
}
10931098

Source/FolioReaderConfig.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,24 @@ public struct ClassBasedOnClickListener {
5252
/// The name of the URL scheme which should be used. Note: Make sure that the given `String` is a valid as scheme name.
5353
public var schemeName : String
5454

55-
/// The HTML class name to which the listener should be added.
56-
public var className : String
55+
/// 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.
56+
public var querySelector : String
5757

58-
/// The name of the parameter whose content should be passed to the `onClickAction` action
59-
public var parameterName : String
58+
/// The name of the attribute whose content should be passed to the `onClickAction` action.
59+
public var attributeName : String
60+
61+
/// 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`.
62+
public var selectAll : Bool
6063

6164
/// The closure which will be called if the specified class was clicked.
6265
public var onClickAction : ((parameterContent: String?) -> Void)
6366

64-
/// Initializes a `ClassBasedOnClickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events.
65-
public init(schemeName: String, className: String, parameterName: String, onClickAction: ((parameterContent: String?) -> Void)) {
67+
/// Initializes a `ClassBasedOnClickListener` instance. Append it to the `classBasedOnClickListeners` property from the `FolioReaderConfig` to receive on click events. The default `selectAll` value is `true`.
68+
public init(schemeName: String, querySelector: String, attributeName: String, selectAll: Bool = true, onClickAction: ((attributeContent: String?) -> Void)) {
6669
self.schemeName = schemeName.lowercaseString
67-
self.className = className
68-
self.parameterName = parameterName
70+
self.querySelector = querySelector
71+
self.attributeName = attributeName
72+
self.selectAll = selectAll
6973
self.onClickAction = onClickAction
7074
}
7175
}
@@ -174,6 +178,7 @@ public class FolioReaderConfig: NSObject {
174178
public var localizedShareAllExcerptsFrom = NSLocalizedString("All excerpts from", comment: "")
175179
public var localizedShareBy = NSLocalizedString("by", comment: "")
176180
public var localizedCancel = NSLocalizedString("Cancel", comment: "")
181+
public var localizedShare = NSLocalizedString("Share", comment: "")
177182
public var localizedChooseExisting = NSLocalizedString("Choose existing", comment: "")
178183
public var localizedTakePhoto = NSLocalizedString("Take Photo", comment: "")
179184
public var localizedShareImageQuote = NSLocalizedString("Share image quote", comment: "")

Source/FolioReaderPage.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import SafariServices
1111
import UIMenuItem_CXAImageSupport
1212
import JSQWebViewController
1313

14-
protocol FolioReaderPageDelegate: class {
14+
/// Protocol which is used from `FolioReaderPage`s.
15+
public protocol FolioReaderPageDelegate: class {
1516
/**
1617
Notify that page did loaded
1718

@@ -23,8 +24,9 @@ protocol FolioReaderPageDelegate: class {
2324
public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate {
2425

2526
weak var delegate: FolioReaderPageDelegate?
26-
var pageNumber: Int!
27-
var webView: UIWebView!
27+
/// The index of the current page. Note: The index start at 1!
28+
public var pageNumber: Int!
29+
var webView: UIWebView!
2830
private var colorView: UIView!
2931
private var shouldShowBar = true
3032
private var menuIsVisible = false
@@ -418,7 +420,7 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture
418420
private func setupClassBasedOnClickListeners() {
419421

420422
for listener in readerConfig.classBasedOnClickListeners {
421-
self.webView.js("addClassBasedOnClickListener(\"\(listener.schemeName)\", \"\(listener.className)\", \"\(listener.parameterName)\")");
423+
self.webView.js("addClassBasedOnClickListener(\"\(listener.schemeName)\", \"\(listener.querySelector)\", \"\(listener.attributeName)\", \"\(listener.selectAll)\")");
422424
}
423425
}
424426
}

Source/FolioReaderQuoteShare.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class FolioReaderQuoteShare: UIViewController {
4646
configureNavBar()
4747

4848
let titleAttrs = [NSForegroundColorAttributeName: readerConfig.tintColor]
49-
let share = UIBarButtonItem(title: "Share", style: .Plain, target: self, action: #selector(shareQuote(_:)))
49+
let share = UIBarButtonItem(title: readerConfig.localizedShare, style: .Plain, target: self, action: #selector(shareQuote(_:)))
5050
share.setTitleTextAttributes(titleAttrs, forState: .Normal)
5151
navigationItem.rightBarButtonItem = share
5252

Source/Resources/Bridge.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ var getRectForSelectedText = function(elm) {
145145
// Method that call that a hightlight was clicked
146146
// with URL scheme and rect informations
147147
var callHighlightURL = function(elm) {
148-
var URLBase = "highlight://";
148+
event.stopPropagation();
149+
var URLBase = "highlight://";
149150
var currentHighlightRect = getRectForSelectedText(elm);
150151
thisHighlight = elm;
151152

@@ -582,18 +583,28 @@ function wrappingSentencesWithinPTags(){
582583

583584
// Class based onClick listener
584585

585-
function addClassBasedOnClickListener(schemeName, className, parameterName) {
586-
// Get all elements with the given className
587-
var elements = document.getElementsByClassName(className);
588-
for (elementIndex = 0; elementIndex < elements.length; elementIndex++) {
589-
var element = elements[elementIndex];
590-
// Get the content from the given parameterName
591-
var parameterContent = element.getAttribute(parameterName);
592-
// Add the on click logic
593-
element.setAttribute("onclick", "onClassBasedListenerClick(\"" + schemeName + "\", \"" + parameterContent + "\");");
586+
function addClassBasedOnClickListener(schemeName, querySelector, attributeName, selectAll) {
587+
if (selectAll) {
588+
// Get all elements with the given query selector
589+
var elements = document.querySelectorAll(querySelector);
590+
for (elementIndex = 0; elementIndex < elements.length; elementIndex++) {
591+
var element = elements[elementIndex];
592+
addClassBasedOnClickListenerToElement(element, schemeName, attributeName);
593+
}
594+
} else {
595+
// Get the first element with the given query selector
596+
var element = document.querySelector(querySelector);
597+
addClassBasedOnClickListenerToElement(element, schemeName, attributeName);
594598
}
595599
}
596600

601+
function addClassBasedOnClickListenerToElement(element, schemeName, attributeName) {
602+
// Get the content from the given attribute name
603+
var attributeContent = element.getAttribute(attributeName);
604+
// Add the on click logic
605+
element.setAttribute("onclick", "onClassBasedListenerClick(\"" + schemeName + "\", \"" + encodeURIComponent(attributeContent) + "\");");
606+
}
607+
597608
var onClassBasedListenerClick = function(schemeName, parameterContent) {
598-
window.location = schemeName + "://" + encodeURIComponent(parameterContent);
609+
window.location = schemeName + "://" + parameterContent;
599610
}

0 commit comments

Comments
 (0)