|
| 1 | +// install : cordova plugin add https://github.com/EddyVerbruggen/cordova-plugin-3dtouch.git |
| 2 | +// link : https://github.com/EddyVerbruggen/cordova-plugin-3dtouch |
| 3 | + |
| 4 | +angular.module('ngCordova.plugins.3dtouch', []) |
| 5 | + |
| 6 | + .factory('$cordova3DTouch', ['$q', function($q) { |
| 7 | + var quickActions = []; |
| 8 | + var quickActionHandler = {}; |
| 9 | + |
| 10 | + var createQuickActionHandler = function(quickActionHandler) { |
| 11 | + return function (payload) { |
| 12 | + for (var key in quickActionHandler) { |
| 13 | + if (payload.type === key) { |
| 14 | + quickActionHandler[key](); |
| 15 | + } |
| 16 | + } |
| 17 | + }; |
| 18 | + }; |
| 19 | + |
| 20 | + return { |
| 21 | + /* |
| 22 | + * Checks if Cordova 3D touch is present and loaded |
| 23 | + * |
| 24 | + * @return promise |
| 25 | + */ |
| 26 | + isAvailable: function () { |
| 27 | + var deferred = $q.defer(); |
| 28 | + if (!window.cordova) { |
| 29 | + deferred.reject('Not supported in browser'); |
| 30 | + } else { |
| 31 | + if (!window.ThreeDeeTouch) { |
| 32 | + deferred.reject('Could not find 3D touch plugin'); |
| 33 | + } else { |
| 34 | + window.ThreeDeeTouch.isAvailable(function (value) { |
| 35 | + deferred.resolve(value); |
| 36 | + }, function (err) { |
| 37 | + deferred.reject(err); |
| 38 | + }); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return deferred.promise; |
| 43 | + }, |
| 44 | + |
| 45 | + /* |
| 46 | + * Add a quick action to menu |
| 47 | + * |
| 48 | + * @param string type |
| 49 | + * @param string title |
| 50 | + * @param string iconType (optional) |
| 51 | + * @param string subtitle (optional) |
| 52 | + * @param function callback (optional) |
| 53 | + * @return promise |
| 54 | + */ |
| 55 | + addQuickAction: function(type, title, iconType, iconTemplate, subtitle, callback) { |
| 56 | + var deferred = $q.defer(); |
| 57 | + |
| 58 | + var quickAction = { |
| 59 | + type: type, |
| 60 | + title: title, |
| 61 | + subtitle: subtitle |
| 62 | + }; |
| 63 | + |
| 64 | + if (iconType) { |
| 65 | + quickAction.iconType = iconType; |
| 66 | + } |
| 67 | + |
| 68 | + if (iconTemplate) { |
| 69 | + quickAction.iconTemplate = iconTemplate; |
| 70 | + } |
| 71 | + |
| 72 | + this.isAvailable().then(function() { |
| 73 | + quickActions.push(quickAction); |
| 74 | + quickActionHandler[type] = callback; |
| 75 | + window.ThreeDeeTouch.configureQuickActions(quickActions); |
| 76 | + window.ThreeDeeTouch.onHomeIconPressed = createQuickActionHandler(quickActionHandler); |
| 77 | + deferred.resolve(quickActions); |
| 78 | + }, |
| 79 | + function(err) { |
| 80 | + deferred.reject(err); |
| 81 | + }); |
| 82 | + |
| 83 | + return deferred.promise; |
| 84 | + }, |
| 85 | + |
| 86 | + /* |
| 87 | + * Add a quick action handler. Used for static quick actions |
| 88 | + * |
| 89 | + * @param string type |
| 90 | + * @param function callback |
| 91 | + * @return promise |
| 92 | + */ |
| 93 | + addQuickActionHandler: function(type, callback) { |
| 94 | + var deferred = $q.defer(); |
| 95 | + |
| 96 | + this.isAvailable().then(function() { |
| 97 | + quickActionHandler[type] = callback; |
| 98 | + window.ThreeDeeTouch.onHomeIconPressed = createQuickActionHandler(quickActionHandler); |
| 99 | + deferred.resolve(true); |
| 100 | + }, |
| 101 | + function(err) { |
| 102 | + deferred.reject(err); |
| 103 | + }); |
| 104 | + |
| 105 | + return deferred.promise; |
| 106 | + }, |
| 107 | + |
| 108 | + /* |
| 109 | + * Enable link preview popup when force touch is appled to link elements |
| 110 | + * |
| 111 | + * @return bool |
| 112 | + */ |
| 113 | + enableLinkPreview: function() { |
| 114 | + var deferred = $q.defer(); |
| 115 | + |
| 116 | + this.isAvailable().then(function() { |
| 117 | + window.ThreeDeeTouch.enableLinkPreview(); |
| 118 | + deferred.resolve(true); |
| 119 | + }, |
| 120 | + function(err) { |
| 121 | + deferred.reject(err); |
| 122 | + }); |
| 123 | + |
| 124 | + return deferred.promise; |
| 125 | + }, |
| 126 | + |
| 127 | + /* |
| 128 | + * Add a hanlder function for force touch events, |
| 129 | + * |
| 130 | + * @param function callback |
| 131 | + * @return promise |
| 132 | + */ |
| 133 | + addForceTouchHandler: function(callback) { |
| 134 | + var deferred = $q.defer(); |
| 135 | + |
| 136 | + this.isAvailable().then(function() { |
| 137 | + window.ThreeDeeTouch.watchForceTouches(callback); |
| 138 | + deferred.resolve(true); |
| 139 | + }, |
| 140 | + function(err) { |
| 141 | + deferred.reject(err); |
| 142 | + }); |
| 143 | + |
| 144 | + return deferred.promise; |
| 145 | + } |
| 146 | + }; |
| 147 | + }]); |
0 commit comments