Unit test for getting page region. Fix bug in br.getPageRegionURI
[bookreader.git] / BookReaderIA / datanode / BookReaderJSIA.php
index 5082569..8b7a4f9 100644 (file)
@@ -209,6 +209,26 @@ br.getPageURI = function(index, reduce, rotate) {
     return 'http://'+this.server+'/BookReader/BookReaderImages.php?zip='+this.zip+'&file='+file+'&scale='+_reduce+'&rotate='+_rotate;
 }
 
+// Get a rectangular region out of a page
+br.getRegionURI = function(index, reduce, rotate, sourceX, sourceY, sourceWidth, sourceHeight) {
+
+    // Map function arguments to the url keys
+    var urlKeys = ['n', 'r', 'rot', 'x', 'y', 'w', 'h'];
+    var page = '';
+    for (var i = 0; i < arguments.length; i++) {
+        if ('undefined' != typeof(arguments[i])) {
+            if (i > 0 ) {
+                page += '_';
+            }
+            page += urlKeys[i] + arguments[i];
+        }
+    }
+    
+    var itemPath = this.bookPath.replace(new RegExp('/'+this.subPrefix+'$'), ''); // remove trailing subPrefix
+    
+    return 'http://'+this.server+'/BookReader/BookReaderImages.php?id=' + this.bookId + '&itemPath=' + itemPath + '&server=' + this.server + '&subPrefix=' + this.subPrefix + '&page=' +page + '.jpg';
+}
+
 br._getPageFile = function(index) {
     var leafStr = '0000';
     var imgStr = this.leafMap[index].toString();
@@ -375,20 +395,22 @@ br.getEmbedCode = function(frameWidth, frameHeight, viewParams) {
 br.getOpenLibraryRecord = function(callback) {
     // Try looking up by ocaid first, then by source_record
     
-    var jsonURL = this.olHost + '/query.json?type=/type/edition&*=&ocaid=' + br.bookId;
+    var self = this; // closure
+    
+    var jsonURL = self.olHost + '/query.json?type=/type/edition&*=&ocaid=' + self.bookId;
     $.ajax({
         url: jsonURL,
         success: function(data) {
             if (data && data.length > 0) {
-                callback(br, data[0]);
+                callback(self, data[0]);
             } else {
                 // try sourceid
-                jsonURL = this.olHost + '/query.json?type=/type/edition&*=&source_records=ia:' + br.bookId;
+                jsonURL = self.olHost + '/query.json?type=/type/edition&*=&source_records=ia:' + self.bookId;
                 $.ajax({
                     url: jsonURL,
                     success: function(data) {
                         if (data && data.length > 0) {
-                            callback(br, data[0]);
+                            callback(self, data[0]);
                         }
                     },
                     dataType: 'jsonp'
@@ -529,14 +551,16 @@ if ('' != $metaData->{'page-progression'}) {
 }
 
 $useOLAuth = false;
+$protected = false;
 foreach ($metaData->xpath('//collection') as $collection) {
     if('browserlending' == $collection) {
         $useOLAuth = true;
+        $protected = true;
     }
 }
 
-echo "br.olHost = 'http://openlibrary.org'\n";
-#echo "br.olHost = 'http://ol-mang:8080'\n";
+echo "br.olHost = 'http://openlibrary.org';\n";
+#echo "br.olHost = 'http://mang-dev.us.archive.org:8080';\n";
 
 if ($useOLAuth) {
     echo "br.olAuth = true;\n";
@@ -544,10 +568,13 @@ if ($useOLAuth) {
     echo "br.olAuth = false;\n";
 }
 
-# Special cases
-if ('bandersnatchhsye00scarrich' == $id) {
-    echo "br.mode     = 2;\n";
-    echo "br.auto     = true;\n";
+if ($protected) {
+    echo "br.protected = true;\n";
+}
+
+# Default options for BookReader
+if ('' != $metaData->{'bookreader-defaults'}) {
+    echo "br.defaults = '" . $metaData->{'bookreader-defaults'} . "';\n";
 }
 
 ?>
@@ -567,6 +594,12 @@ if (typeof(brConfig) != 'undefined') {
     } else if (brConfig['mode'] == 2) {
         br.mode = 2;      
     }
+    
+    if (typeof(brConfig["isAdmin"]) != 'undefined') {
+        br.isAdmin = brConfig["isAdmin"];
+    } else {
+        br.isAdmin = false;
+    }
 } // brConfig
 
 
@@ -574,7 +607,7 @@ function OLAuth() {
     this.authUrl = br.olHost + '/ia_auth/' + br.bookId;
     this.olConnect = false;
     this.loanUUID = false;
-    this.loanToken = false;
+    this.permsToken = false;
     
     var cookieRe = /;\s*/;
     var cookies = document.cookie.split(cookieRe);
@@ -585,7 +618,7 @@ function OLAuth() {
             this.loanUUID = cookies[i].split('=')[1];
         }
         if (0 == cookies[i].indexOf('loan-' + br.bookId)) {
-            this.loanToken = cookies[i].split('=')[1];
+            this.permsToken = cookies[i].split('=')[1];
         }
     }
 
@@ -595,13 +628,13 @@ function OLAuth() {
 OLAuth.prototype.init = function() {
     var htmlStr =  'Checking loan status with Open Library';
 
-    this.showPopup("#ddd", "#000", htmlStr, 'Please wait as we check the status of this book...');
+    this.showPopup("#F0EEE2", "#000", htmlStr, 'Please wait as we check the status of this book...');
     var authUrl = this.authUrl+'?rand='+Math.random();
     if (false !== this.loanUUID) {
         authUrl += '&loan='+this.loanUUID
     }
-    if (false !== this.loanToken) {
-        authUrl += '&token='+this.loanToken
+    if (false !== this.permsToken) {
+        authUrl += '&token='+this.permsToken
     }
     $.ajax({url:authUrl, dataType:'jsonp', jsonpCallback:'olAuth.initCallback'});
 }
@@ -610,10 +643,10 @@ OLAuth.prototype.showPopup = function(bgColor, textColor, msg, resolution) {
     this.popup = document.createElement("div");
     $(this.popup).css({
         position: 'absolute',
-        top:      '20px',
+        top:      '50px',
         left:     ($('#BookReader').attr('clientWidth')-400)/2 + 'px',
         width:    '400px',
-        padding:  "20px",
+        padding:  "15px",
         border:   "3px double #999999",
         zIndex:   3,
         textAlign: 'center',
@@ -629,27 +662,39 @@ OLAuth.prototype.setPopupMsg = function(msg, resolution) {
     this.popup.innerHTML = ['<p><strong>', msg, '</strong></p><p>', resolution, '</p>'].join('\n');
 }
 
+OLAuth.prototype.showError = function(msg, resolution) {
+   $(this.popup).css({
+        backgroundColor: "#fff",
+        color: "#000"
+    });
+
+    this.setPopupMsg(msg, resolution);
+}
+
 OLAuth.prototype.initCallback = function(obj) {
     if (false == obj.success) {
-        $(this.popup).css({
-            backgroundColor: "#fff",
-            color: "#000"
-        });
-
-        this.setPopupMsg(obj.msg, obj.resolution);
-        return;
+        if (br.isAdmin) {
+            ret = confirm("We couldn't authenticate your loan with Open Library, but since you are an administrator or uploader of this book, you can access this book for QA purposes. Would you like to QA this book?");
+            if (!ret) {
+                this.showError(obj.msg, obj.resolution)
+            } else {
+                br.init();
+            }
+        } else {
+            this.showError(obj.msg, obj.resolution)
+        }       
+    } else {    
+        //user is authenticated
+        this.setCookie(obj.token);
+        this.olConnect = true;
+        this.startPolling();    
+        br.init();
     }
-    
-    //user is authenticated
-    this.setCookie(obj.token);
-    this.olConnect = true;
-    this.startPolling();    
-    br.init();
 }
 
 OLAuth.prototype.callback = function(obj) {
     if (false == obj.success) {
-        this.showPopup("#fff", "#000", obj.msg, obj.resolution);
+        this.showPopup("#F0EEE2", "#000", obj.msg, obj.resolution);
         clearInterval(this.poller);
         this.ttsPoller = null;
     } else {
@@ -666,6 +711,7 @@ OLAuth.prototype.setCookie = function(value) {
     cookie    += '; expires='+expiry;
     cookie    += '; path=/; domain=.archive.org;';
     document.cookie = cookie;
+    this.permsToken = value;
     
     //refresh the br-loan uuid cookie with current expiry, if needed
     if (false !== this.loanUUID) {
@@ -695,7 +741,7 @@ OLAuth.prototype.startPolling = function () {
     var self = this;
     this.poller=setInterval(function(){
         if (!self.olConnect) {
-          self.showPopup("#f00", "#fff", 'Connection error', 'The BookReader cannot reach Open Library. This might mean that you are offline or that Open Library is down. Please check your Internet connection or try again later.');
+          self.showPopup("#F0EEE2", "#000", 'Connection error', 'The BookReader cannot reach Open Library. This might mean that you are offline or that Open Library is down. Please check your Internet connection and refresh this page or try again later.');
           clearInterval(self.poller);
           self.ttsPoller = null;        
         } else {
@@ -705,8 +751,8 @@ OLAuth.prototype.startPolling = function () {
           if (false !== self.loanUUID) {
               authUrl += '&loan='+self.loanUUID
           }
-          if (false !== self.loanToken) {
-              authUrl += '&token='+self.loanToken
+          if (false !== self.permsToken) {
+              authUrl += '&token='+self.permsToken
           }
 
           $.ajax({url:authUrl, dataType:'jsonp', jsonpCallback:'olAuth.callback'});