You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2017/02/22 03:26:47 UTC

[4/9] cordova-plugin-screen-orientation git commit: working version of screen orientation plugin adhering to w3c specs

 working version of screen orientation plugin adhering to w3c specs


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/commit/1f536ec8
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/tree/1f536ec8
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/diff/1f536ec8

Branch: refs/heads/master
Commit: 1f536ec80e6f54edb618dabc2a0dd7c9b3e62a0e
Parents: 912b6b1
Author: maverickmishra <vi...@gmail.com>
Authored: Mon Jan 9 16:42:53 2017 -0800
Committer: maverickmishra <vi...@gmail.com>
Committed: Wed Jan 25 10:47:32 2017 -0800

----------------------------------------------------------------------
 demo/www/index.html      |   2 +-
 demo/www/js/index.js     |  27 +++----
 tests/tests.js           |  34 +++++++++
 www/screenorientation.js | 171 ++++++++++++++++++++----------------------
 4 files changed, 127 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/blob/1f536ec8/demo/www/index.html
----------------------------------------------------------------------
diff --git a/demo/www/index.html b/demo/www/index.html
index 36f0596..cd1e003 100644
--- a/demo/www/index.html
+++ b/demo/www/index.html
@@ -50,7 +50,7 @@
                 <button id="btnPortPrimary">Portrait - Primary</button>
                 <button id="btnLandPrimary">Landscape - Primary</button>
             </div>
-            <button id="btnAny">Any</button>
+            <button id="btnAny">Unlock</button>
         </div>
         <script type="text/javascript" src="cordova.js"></script>
         <script type="text/javascript" src="js/index.js"></script>

http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/blob/1f536ec8/demo/www/js/index.js
----------------------------------------------------------------------
diff --git a/demo/www/js/index.js b/demo/www/js/index.js
index 3df971c..aed764a 100644
--- a/demo/www/js/index.js
+++ b/demo/www/js/index.js
@@ -36,44 +36,35 @@ var app = {
     onDeviceReady: function() {
         app.receivedEvent('deviceready');
         btnPortrait.addEventListener("click", function() {
-            //    alert('Orientation is ' + screen.orientation);
-            screen.lockOrientation('portrait').then(function(obj) {
+            screen.orientation.lock('portrait').then(function(obj) {
                 console.log(obj);
-            }).catch(function(obj) {
+            }, function(obj) {
                 console.log(obj);
             });
         });
         btnLandscape.addEventListener("click", function() {
-            //    alert('Orientation is ' + screen.orientation);
-            screen.lockOrientation('landscape').then(function(obj) {
+            screen.orientation.lock('landscape').then(function(obj) {
                 console.log(obj);
-            }).catch(function(obj) {
+            }, function(obj) {
                 console.log(obj);
             });
         });
         btnPortPrimary.addEventListener("click", function() {
-            //    alert('Orientation is ' + screen.orientation);
-            screen.lockOrientation('portrait-primary').then(function(obj) {
+            screen.orientation.lock('portrait-primary').then(function(obj) {
                 console.log(obj);
-            }).catch(function(obj) {
+            }, function(obj) {
                 console.log(obj);
             });
         });
         btnLandPrimary.addEventListener("click", function() {
-            //    alert('Orientation is ' + screen.orientation);
-            screen.lockOrientation('landscape-primary').then(function(obj) {
+            screen.orientation.lock('landscape-primary').then(function(obj) {
                 console.log(obj);
-            }).catch(function(obj) {
+            }, function(obj) {
                 console.log(obj);
             });
         });
         btnAny.addEventListener("click", function() {
-            //    alert('Orientation is ' + screen.orientation);
-            screen.lockOrientation('any').then(function(obj) {
-                console.log(obj);
-            }).catch(function(obj) {
-                console.log(obj);
-            });
+            screen.orientation.unlock();
         });
 
 

http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/blob/1f536ec8/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index 167e1a1..08acc69 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -56,4 +56,38 @@ exports.defineAutoTests = function() {
             }
         });
     });
+  
+  describe('OrientationLockType should have one of seven values', function () {
+
+	it("should have one of seven orientation lock types", function(){
+		expect(window.OrientationLockType['portrait-primary']).toBe(1);
+		expect(window.OrientationLockType['portrait-secondary']).toBe(2);
+		expect(window.OrientationLockType['landscape-primary']).toBe(4);
+		expect(window.OrientationLockType['landscape-secondary']).toBe(8);
+		expect(window.OrientationLockType['portrait']).toBe(3);
+		expect(window.OrientationLockType['landscape']).toBe(12);
+		expect(window.OrientationLockType['any']).toBe(15);
+		 alert(window.screen.orientation.angle);
+		});
+   });
+  describe('Screen object should exist', function () {
+
+    it("should exist", function() {
+      expect(window.screen).toBeDefined();
+    });
+
+    it(" screen should contain an attribute called ScreenOrientation", function() {
+      expect(window.screen.orientation).toBeDefined();
+      });
+    it(" Screenorientation object should contain methods called lock and unlock", function() {
+      expect(window.screen.orientation.lock).toEqual(jasmine.any(Function));
+      expect(window.screen.orientation.unlock).toEqual(jasmine.any(Function));
+      });
+	it(" Screenorientation object should contain properties called type and angle", function() {
+      expect(window.screen.orientation.type).toBeDefined();
+      expect(window.screen.orientation.angle).toBeDefined();
+
+      });
+
+  });
 };

http://git-wip-us.apache.org/repos/asf/cordova-plugin-screen-orientation/blob/1f536ec8/www/screenorientation.js
----------------------------------------------------------------------
diff --git a/www/screenorientation.js b/www/screenorientation.js
index d24a48c..bf0f1e8 100644
--- a/www/screenorientation.js
+++ b/www/screenorientation.js
@@ -19,102 +19,97 @@
  *
  */
 
-var screenOrientation = {},
-    Orientations = [
-        'portrait-primary',
-        'portrait-secondary',
-        'landscape-primary',
-        'landscape-secondary',
-        'portrait',  // either portrait-primary or portrait-secondary.
-        'landscape', // either landscape-primary or landscape-secondary.
-        'any'        // All orientations are supported (unlocked orientation)
-    ];
 
-screenOrientation.Orientations = Orientations;
-screenOrientation.currOrientation = 'any';
-var orientationMask = 0;
-screenOrientation.setOrientation = function(orientation) {
-    if(orientation == Orientations[0]){
-        orientationMask = 1;
+    var screenOrientation = {};
+    if (!window.OrientationType) {
+        window.OrientationType = {
+            '0': 'portrait-primary',
+            '180': 'portrait-secondary',
+            '90': 'landscape-primary',
+            '-90': 'landscape-secondary'
+        };
     }
-    else if(orientation == Orientations[1]){
-        orientationMask = 2;
+    if (!window.OrientationLockType) {
+        window.OrientationLockType = {
+            'portrait-primary': 1,
+            'portrait-secondary': 2,
+            'landscape-primary': 4,
+            'landscape-secondary': 8,
+            'portrait': 3, // either portrait-primary or portrait-secondary.
+            'landscape': 12, // either landscape-primary or landscape-secondary.
+            'any': 15 // All orientations are supported (unlocked orientation)
+        };
     }
-    else if(orientation == Orientations[2]){
-        orientationMask = 4;
-    }
-    else if(orientation == Orientations[3]){
-        orientationMask = 8;
-    }
-    else if(orientation == Orientations[4]){
-        orientationMask = 3;
-    }
-    else if(orientation == Orientations[5]){
-        orientationMask = 12;
-    }
-    else if(orientation == Orientations[6]){
-        orientationMask = 15;
-    }
-
-
-    cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
-    //console.log('setOrientation not supported on device');
-};
-
-function addScreenOrientationApi(screenObject) {
-    if (screenObject.unlockOrientation || screenObject.lockOrientation) {
-        return;
-    }
-
-    screenObject.lockOrientation = function(orientation) {
-
-        var p = new Promise(function(resolve,reject){
-            if (Orientations.indexOf(orientation) == -1) {
-                var err = new Error();
-                err.name = "NotSupportedError";
-                reject(err);//"cannot change orientation");
-            }
-            else {
-                screenOrientation.currOrientation = screenObject.orientation = orientation;
-                screenOrientation.setOrientation(orientation);
-                resolve("Orientation set"); // orientation change successful
-            }
-        });
-        return p;
+    var orientationMask = 1;
+    screenOrientation.setOrientation = function(orientation) {
+        orientationMask = window.OrientationLockType[orientation];
+        cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
     };
 
-    screenObject.unlockOrientation = function() {
-        screenOrientation.currOrientation = screenObject.orientation = 'any';
-        screenOrientation.setOrientation('any');
-    };
-}
+    function addScreenOrientationApi(screenObject) {
+        if (screenObject.unlock || screenObject.lock) {
+            screenObject.nativeLock = screenObject.lock;
+        }
 
-addScreenOrientationApi(screen);
-orientationChange();
+        screenObject.lock = function(orientation) {
+            var promiseLock;
+            var p = new Promise(function(resolve, reject) {
+                if (screenObject.nativeLock != null) {
+                    promiseLock = screenObject.nativeLock(orientation);
+                    flag = true;
+                } else {
+                    resolveOrientation(orientation, resolve, reject);
+                }
+                promiseLock.then(function success(res) {
+                    resolve();
+                }, function error(err) {
+                    screenObject.nativeLock = null;
+                    resolveOrientation(orientation, resolve, reject);
+                });
+            })
+            return p;
+        }
 
-function orientationChange() {
-    var orientation;
+        screenObject.unlock = function() {
+            screenOrientation.setOrientation('any');
+        };
 
-    switch (window.orientation) {
-        case 0:
-            orientation = 'portrait-primary';
-            break;
-        case 90:
-            orientation = 'landscape-primary';
-            break;
-        case 180:
-            orientation = 'portrait-secondary';
-            break;
-        case -90:
-            orientation = 'landscape-secondary';
-            break;
-        default:
-        orientation = 'any';
     }
+    function resolveOrientation(orientation, resolve, reject) {
+        if (!OrientationLockType.hasOwnProperty(orientation)) {
+            var err = new Error();
+            err.name = "NotSupportedError";
+            reject(err); //"cannot change orientation");
+        } else {
+            //screenOrientation.currOrientation = screenObject.orientation = orientation;
+            screenOrientation.setOrientation(orientation);
+            resolve("Orientation set"); // orientation change successful
+        }
 
-    screen.orientation = orientation;
-}
-
-window.addEventListener("orientationchange", orientationChange, true);
+    }
+    if (!screen.orientation) {
+        screen.orientation = {};
+    }
+    addScreenOrientationApi(screen.orientation);
+    orientationChange();
 
-module.exports = screenOrientation;
+    function orientationChange() {
+        switch (window.orientation) {
+            case 0:
+                screen.orientation.type = window.OrientationType['0']; //append angle here ?
+                break;
+            case 90:
+                screen.orientation.type = window.OrientationType['90'];
+                break;
+            case 180:
+                screen.orientation.type = window.OrientationType['180'];
+                break;
+            case -90:
+                screen.orientation.type = window.OrientationType['-90'];
+                break;
+            default:
+                screen.orientation.type = window.OrientationType['0'];
+        }
+    }
+    window.addEventListener("orientationchange", orientationChange, true);
+    module.exports = screenOrientation;
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org