source : PixLiveEvent.js

  1. /*
  2. * angular-pixlive v1
  3. * (c) 2015-2016 Vidinoti https://vidinoti.com
  4. * License: MIT
  5. *
  6. * Event directives
  7. *
  8. */
  9. 'use strict';
  10. pixliveModule
  11. /**
  12. * @memberof pixlive
  13. * @ngdoc service
  14. * @name PxlEventService
  15. * @description
  16. * Add / Remove event subscribers to PixLive SDK related events.
  17. *
  18. * **Note:** You should use the plugin's directive (like `pxlContextEnter`) instead of using this service directly.
  19. */
  20. .constant('PxlEventService', (function() {
  21. var eventListeners={};
  22. var handler = function(event) {
  23. if(event.type && eventListeners[event.type]) {
  24. for(var i = eventListeners[event.type].length-1; i>=0; i--) {
  25. eventListeners[event.type][i](event);
  26. }
  27. }
  28. };
  29. return {
  30. handler: handler,
  31. /**
  32. * Add a new listener for the provided event type.
  33. *
  34. * @memberof PxlEventService
  35. * @param {string} event The event to register for. See the [cordova-plugin-PixLive](https://github.com/vidinoti/cordova-plugin-PixLive) plugin for more info on the event types.
  36. * @param {function} callback The function to be called when the provided event is generated.
  37. */
  38. addListener: function(event, callback) {
  39. if(!eventListeners[event]) {
  40. eventListeners[event]=[];
  41. }
  42. eventListeners[event].push(callback);
  43. },
  44. /**
  45. * Remove an existing listener for the provided event type.
  46. *
  47. * @memberof PxlEventService
  48. * @param {string} event The event to register for. See the [cordova-plugin-PixLive](https://github.com/vidinoti/cordova-plugin-PixLive) plugin for more info on the event types.
  49. * @param {function} callback The function that has been passed to the `addListener(event, callback)` method.
  50. */
  51. removeListener: function(event, callback) {
  52. if(!eventListeners[event] || eventListeners[event].length == 0) {
  53. return;
  54. }
  55. var index = eventListeners[event].indexOf(callback);
  56. if(index==-1)
  57. return;
  58. eventListeners[event].splice(index,1);
  59. }
  60. };
  61. })())
  62. .run(['PxlEventService', '$ionicPlatform', function(PxlEventService, $ionicPlatform) {
  63. //We make sure the event service is executed and loaded.
  64. $ionicPlatform.ready(function() {
  65. if (window.cordova && window.cordova.plugins && window.cordova.plugins.PixLive && !window.cordova.plugins.PixLive.onEventReceived) {
  66. cordova.plugins.PixLive.onEventReceived = PxlEventService.handler;
  67. }
  68. });
  69. }])
  70. /**
  71. * @ngdoc directive
  72. * @name pxlContextEnter
  73. * @element Attribute
  74. * @memberof pixlive
  75. * @param {service} PxlEventService PixLive SDK Event service
  76. * @restrict A
  77. *
  78. * @description
  79. * Expression that is evaluated when a context is entered. Such an event
  80. * happens when a context is linked with a beacon and you are getting close
  81. * to the beacon, or when an image is linked with such a context and this image has been recognized.
  82. *
  83. * The unique ID of the context is passed as a parameter.
  84. *
  85. * @example
  86. * <div pxl-context-enter="contextEnter(contextId)">
  87. * ...
  88. * </div>
  89. */
  90. .directive('pxlContextEnter', [
  91. 'PxlEventService',
  92. function(PxlEventService) {
  93. return {
  94. restrict: 'A',
  95. link: function(scope, element, attrs) {
  96. var listener = function(event) {
  97. scope.$apply(function(self) {
  98. self[attrs.pxlContextEnter](event.context);
  99. });
  100. };
  101. PxlEventService.addListener('enterContext',listener);
  102. element.bind('$destroy', function() {
  103. PxlEventService.removeListener('enterContext',listener);
  104. });
  105. }
  106. };
  107. }
  108. ])
  109. /**
  110. * @ngdoc directive
  111. * @name pxlContextExit
  112. * @element Attribute
  113. * @memberof pixlive
  114. * @param {service} PxlEventService PixLive SDK Event service
  115. * @restrict A
  116. *
  117. * @description
  118. * Expression that is evaluated when a context is exited. Such an event
  119. * happens when a context is linked with a beacon and you are getting away
  120. * from the beacon, or when an image is linked with such a context and this image is not
  121. * within the camera sight anymore.
  122. *
  123. * The unique ID of the context is passed as a parameter.
  124. *
  125. * @example
  126. * <div pxl-context-enter="contextExit(contextId)">
  127. * ...
  128. * </div>
  129. */
  130. .directive('pxlContextExit', [
  131. 'PxlEventService',
  132. function(PxlEventService) {
  133. return {
  134. restrict: 'A',
  135. link: function(scope, element, attrs) {
  136. var listener = function(event) {
  137. scope.$apply(function(self) {
  138. self[attrs.pxlContextExit](event.context);
  139. });
  140. };
  141. PxlEventService.addListener('exitContext',listener);
  142. element.bind('$destroy', function() {
  143. PxlEventService.removeListener('exitContext',listener);
  144. });
  145. }
  146. };
  147. }
  148. ])
  149. /**
  150. * @ngdoc directive
  151. * @name eventFromContent
  152. * @element Attribute
  153. * @memberof pixlive
  154. * @param {service} PxlEventService PixLive SDK Event service
  155. * @restrict A
  156. *
  157. * @description
  158. * Expression that is evaluated when an event is received from the content (PixliveJS).
  159. * To dispatch an event from PixLiveJS use: device.dispatchEventInApp(eventName, eventParams);
  160. *
  161. * event.eventName The name of the event
  162. * event.eventParams The parameters of the event
  163. * @example
  164. * <div pxl-context-enter="eventFromContent(eventName,eventParams)">
  165. * ...
  166. * </div>
  167. */
  168. .directive('pxlEventFromContent', [
  169. 'PxlEventService',
  170. function(PxlEventService) {
  171. return {
  172. restrict: 'A',
  173. link: function(scope, element, attrs) {
  174. var listener = function(event) {
  175. scope.$apply(function(self) {
  176. self[attrs.pxlEventFromContent](event.eventName,event.eventParams);
  177. });
  178. };
  179. PxlEventService.addListener('eventFromContent',listener);
  180. element.bind('$destroy', function() {
  181. PxlEventService.removeListener('eventFromContent',listener);
  182. });
  183. }
  184. };
  185. }
  186. ])
  187. /**
  188. * @ngdoc directive
  189. * @name pxlSensorTriggered
  190. * @element Attribute
  191. * @memberof pixlive
  192. * @param {service} PxlEventService PixLive SDK Event service
  193. * @restrict A
  194. *
  195. * @description
  196. * Expression that is evaluated when a sensor state become triggered (i.e. active).
  197. *
  198. * The ID of the sensor and the type of sensor are passed as parameters. The types and IDs are defined hereafter.
  199. *
  200. * Three types of sensors are defined:
  201. * 1. `Vision`<br/>
  202. * Corresponds to an image that has been recognized. As of today, the sensor ID corresponds
  203. * to the context ID to which the sensor is linked but this might change in the future as the
  204. * PixLive SDK does support any kind of IDs.
  205. * 2. `iBeacon`<br/>
  206. * Corresponds to an iBeacon that is in the required proximity of the smartphone. The ID is defined to be:
  207. * ```
  208. * BeaconUUID_Major_Minor
  209. * ```
  210. * 3. `VidiBeacon`<br/>
  211. * Corresponds to a VidiBeacon that is in the required proximity of the smartphone.
  212. * The ID is defined to be the VidiBeacon serial.
  213. *
  214. * @example
  215. * <div pxl-sensor-triggered="sensorTriggered(sensorId, sensorType)">
  216. * ...
  217. * </div>
  218. */
  219. .directive('pxlSensorTriggered', [
  220. 'PxlEventService',
  221. function(PxlEventService) {
  222. return {
  223. restrict: 'A',
  224. link: function(scope, element, attrs) {
  225. var listener = function(event) {
  226. scope.$apply(function(self) {
  227. self[attrs.pxlSensorTriggered](event.sensorId,event.sensorType);
  228. });
  229. };
  230. PxlEventService.addListener('sensorTriggered',listener);
  231. element.bind('$destroy', function() {
  232. PxlEventService.removeListener('sensorTriggered',listener);
  233. });
  234. }
  235. };
  236. }
  237. ])
  238. /**
  239. * @ngdoc directive
  240. * @name pxlSensorUpdate
  241. * @element Attribute
  242. * @memberof pixlive
  243. * @param {service} PxlEventService PixLive SDK Event service
  244. * @restrict A
  245. *
  246. * @description
  247. * Expression that is evaluated when a sensor parameter changes.
  248. *
  249. * The ID of the sensor, the type of sensor, and the sensor parameters are passed as parameters. The types and IDs are defined hereafter.
  250. *
  251. * Three types of sensors are defined:
  252. * 1. `Vision`<br/>
  253. * Corresponds to an image that has been recognized. As of today, this sensor never gets updated.
  254. * 2. `iBeacon`<br/>
  255. * Corresponds to an iBeacon that is in the required proximity of the smartphone. The ID is defined to be:
  256. * ```
  257. * BeaconUUID_Major_Minor
  258. * ```
  259. *
  260. * The sensor object contains the following two properties:
  261. * * `rssi`: The RSSI in dbm of the received beacon signal
  262. * * `distance`: The estimated distance in meters between the beacon and the smartphone
  263. *
  264. * 3. `VidiBeacon`<br/>
  265. * Corresponds to a VidiBeacon that is in the required proximity of the smartphone.
  266. * The ID is defined to be the VidiBeacon serial.
  267. *
  268. * The sensor object contains the following property:
  269. * * `rssi`: The RSSI in dbm of the received beacon signal
  270. *
  271. * @example
  272. * <div pxl-sensor-update="sensorUpdate(sensorId, sensorType, sensor)">
  273. * ...
  274. * </div>
  275. */
  276. .directive('pxlSensorUpdate', [
  277. 'PxlEventService',
  278. function(PxlEventService) {
  279. return {
  280. restrict: 'A',
  281. link: function(scope, element, attrs) {
  282. var listener = function(event) {
  283. scope.$apply(function(self) {
  284. self[attrs.pxlSensorUpdate](event.sensorId,event.sensorType, event);
  285. });
  286. };
  287. PxlEventService.addListener('sensorUpdate',listener);
  288. element.bind('$destroy', function() {
  289. PxlEventService.removeListener('sensorUpdate',listener);
  290. });
  291. }
  292. };
  293. }
  294. ])
  295. /**
  296. * @ngdoc directive
  297. * @name pxlSensorUntriggered
  298. * @element Attribute
  299. * @memberof pixlive
  300. * @param {service} PxlEventService PixLive SDK Event service
  301. * @restrict A
  302. *
  303. * @description
  304. * Expression that is evaluated when a sensor state become untriggered (i.e. not anymore active).
  305. *
  306. * The ID of the sensor and the type of sensor are passed as parameters. The types and IDs are defined hereafter.
  307. *
  308. * Three types of sensors are defined:
  309. * 1. `Vision`<br/>
  310. * Corresponds to an image that has been recognized. As of today, the sensor ID corresponds
  311. * to the context ID to which the sensor is linked but this might change in the future as the
  312. * PixLive SDK does support any kind of IDs.
  313. * 2. `iBeacon`<br/>
  314. * Corresponds to an iBeacon that is in the required proximity of the smartphone. The ID is defined to be:
  315. * ```
  316. * BeaconUUID_Major_Minor
  317. * ```
  318. * 3. `VidiBeacon`<br/>
  319. * Corresponds to a VidiBeacon that is in the required proximity of the smartphone.
  320. * The ID is defined to be the VidiBeacon serial.
  321. *
  322. * @example
  323. * <div pxl-sensor-triggered="sensorUntriggered(sensorId, sensorType)">
  324. * ...
  325. * </div>
  326. */
  327. .directive('pxlSensorUntriggered', [
  328. 'PxlEventService',
  329. function(PxlEventService) {
  330. return {
  331. restrict: 'A',
  332. link: function(scope, element, attrs) {
  333. var listener = function(event) {
  334. scope.$apply(function(self) {
  335. self[attrs.pxlSensorUntriggered](event.sensorId,event.sensorType);
  336. });
  337. };
  338. PxlEventService.addListener('sensorUntriggered',listener);
  339. element.bind('$destroy', function() {
  340. PxlEventService.removeListener('sensorUntriggered',listener);
  341. });
  342. }
  343. };
  344. }
  345. ])
  346. /**
  347. * @ngdoc directive
  348. * @name pxlCodeRecognize
  349. * @element Attribute
  350. * @memberof pixlive
  351. * @param {service} PxlEventService PixLive SDK Event service
  352. * @restrict A
  353. *
  354. * @description
  355. * Expression that is evaluated when a code (QR Code, Barcode etc..) is recognized by the PixLive SDK
  356. *
  357. * The code value (e.g. the URL in case of a QR Code with URL) is passed as parameter.
  358. *
  359. * *Note*: You have to enable Code recognition on the SDK for this method to be called.
  360. *
  361. *
  362. * @example
  363. * <div pxl-code-recognize="codeRec(codeValue)">
  364. * ...
  365. * </div>
  366. */
  367. .directive('pxlCodeRecognize', [
  368. 'PxlEventService',
  369. function(PxlEventService) {
  370. return {
  371. restrict: 'A',
  372. link: function(scope, element, attrs) {
  373. var listener = function(event) {
  374. scope.$apply(function(self) {
  375. self[attrs.pxlCodeRecognize](event.code);
  376. });
  377. };
  378. PxlEventService.addListener('codeRecognize',listener);
  379. element.bind('$destroy', function() {
  380. PxlEventService.removeListener('codeRecognize',listener);
  381. });
  382. }
  383. };
  384. }
  385. ])
  386. /**
  387. * @ngdoc directive
  388. * @name pxlAnnotationsPresent
  389. * @element Attribute
  390. * @memberof pixlive
  391. * @param {service} PxlEventService PixLive SDK Event service
  392. * @restrict A
  393. *
  394. * @description
  395. * Expression that is evaluated when some augmented reality content is presented on screen.
  396. *
  397. * This gives you the opportunity to hide any overlay you may have added over the Augmented Reality (AR) view.
  398. *
  399. * *Note*: This method is only called when the AR view is displayed.
  400. *
  401. * @example
  402. * <div pxl-annotations-present="hideOverlay()">
  403. * ...
  404. * </div>
  405. */
  406. .directive('pxlAnnotationsPresent', [
  407. 'PxlEventService',
  408. function(PxlEventService) {
  409. return {
  410. restrict: 'A',
  411. link: function(scope, element, attrs) {
  412. var listener = function(event) {
  413. scope.$apply(function(self) {
  414. self[attrs.pxlAnnotationsPresent]();
  415. });
  416. };
  417. PxlEventService.addListener('presentAnnotations',listener);
  418. element.bind('$destroy', function() {
  419. PxlEventService.removeListener('presentAnnotations',listener);
  420. });
  421. }
  422. };
  423. }
  424. ])
  425. /**
  426. * @ngdoc directive
  427. * @name pxlAnnotationsHide
  428. * @element Attribute
  429. * @memberof pixlive
  430. * @param {service} PxlEventService PixLive SDK Event service
  431. * @restrict A
  432. *
  433. * @description
  434. * Expression that is evaluated when no more augmented reality content is present on screen.
  435. *
  436. * This gives you the opportunity to put back any overlay you may have added over the Augmented Reality (AR) view.
  437. *
  438. * *Note*: This method is only called when the AR view is displayed.
  439. *
  440. * @example
  441. * <div pxl-annotations-hide="showOverlay()">
  442. * ...
  443. * </div>
  444. */
  445. .directive('pxlAnnotationsHide', [
  446. 'PxlEventService',
  447. function(PxlEventService) {
  448. return {
  449. restrict: 'A',
  450. link: function(scope, element, attrs) {
  451. var listener = function(event) {
  452. scope.$apply(function(self) {
  453. self[attrs.pxlAnnotationsHide]();
  454. });
  455. };
  456. PxlEventService.addListener('hideAnnotations',listener);
  457. element.bind('$destroy', function() {
  458. PxlEventService.removeListener('hideAnnotations',listener);
  459. });
  460. }
  461. };
  462. }
  463. ])
  464. /**
  465. * @ngdoc directive
  466. * @name pxlSynchronizationRequired
  467. * @element Attribute
  468. * @memberof pixlive
  469. * @param {service} PxlEventService PixLive SDK Event service
  470. * @restrict A
  471. *
  472. * @description
  473. * Expression that is evaluated when a context synchronization is required.
  474. *
  475. * You should then call the RemoteController to trigger the synchronization with the passed tags (and any others you might want to add).
  476. *
  477. * The tags array to synchronize the app with, is passed as parameter (`tags`in the example below)
  478. *
  479. * @example
  480. * <div pxl-synchronization-required="doSync(tags)">
  481. * ...
  482. * </div>
  483. */
  484. .directive('pxlSynchronizationRequired', [
  485. 'PxlEventService',
  486. function(PxlEventService) {
  487. return {
  488. restrict: 'A',
  489. link: function(scope, element, attrs) {
  490. var listener = function(event) {
  491. scope.$apply(function(self) {
  492. self[attrs.pxlSynchronizationRequired](event.tags);
  493. });
  494. };
  495. PxlEventService.addListener('requireSync',listener);
  496. element.bind('$destroy', function() {
  497. PxlEventService.removeListener('requireSync',listener);
  498. });
  499. }
  500. };
  501. }
  502. ])
  503. ;