UNPKG

vectra-scanner-plugin

Version:

Allows interfacing with pebble device

157 lines (138 loc) 3.89 kB
let exec = require('cordova/exec') let TAG = 'ScannerPlugin > ' function VectraScannerPlugin () { console.log(TAG + 'is created') } VectraScannerPlugin.prototype.exec = function (params, success, fail) { console.log(TAG + 'exec') return exec( function (result) { success(result) }, function (result) { fail(result) }, 'VectraScannerPlugin', '', [params] ) } VectraScannerPlugin.prototype.handlePossibleScanEvent = function (keyUp, keyCode, mustPlayBeep, appUrlScheme, successCallback, errorCallback) { console.log(TAG + 'handlePossibleScanEvent') //Start an Android activity for result. if (keyCode == 18) { this.exec( { keyUp: keyUp, keyCode: 57, mustPlayBeep: mustPlayBeep }, function (result) { successCallback({ event_type: result.event_type, code_len: result.code_len, code: result.code }) }, function (result) { errorCallback(result) } ) } } let VectraScannerPlugin = new VectraScannerPlugin() module.exports = VectraScannerPlugin function $Querystring (qs) { return new Querystring(qs) } function Querystring (qs) { // optionally pass a querystring to parse this.params = new Object() this.keys = new Array() if (qs == null) { qs = location.search.substring(1, location.search.length) this.path = location.protocol + '//' + location.host + location.pathname } else { this.path = (qs.indexOf('?') > 0) ? qs.split('?')[0] : qs } if (qs.length == 0 || qs.indexOf('?') < 0) return if (qs.indexOf('?') > 0) qs = qs.split('?')[1] // Turn <plus> back to <space> // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1 qs = qs.replace(/\+/g, ' ') let args = qs.split('&') // parse out name/value pairs separated via & // split out each name=value pair for (let i = 0; i < args.length; i++) { let pair = args[i].split('=') let name = decodeURIComponent(pair[0]) let value = (pair.length == 2) ? decodeURIComponent(pair[1]) : name this.keys[i] = name this.params[name] = value } } Querystring.prototype.get = function (key, default_) { let value = this.params[key] return (value != null) ? value : default_ } Querystring.prototype.set = function (key, value) { value = encodeURI(value) this.params[key] = value let index = this.indexOfKey(key) if (index < 0) this.keys[this.keys.length] = key return this } Querystring.prototype.indexOfKey = function (key) { for (let i = 0; i < this.keys.length; i++) { if (this.keys[i] == key) return i } return -1 } Querystring.prototype.remove = function (key) { let arr = key.split(',') for (let i = 0; i < arr.length; i++) { this._remove(arr[i]) } return this } Querystring.prototype._remove = function (key) { let index = this.indexOfKey(key) if (index >= 0) { this.params[key] = null this.keys.splice(index, 1) } return this } Querystring.prototype.toString = function () { let ret = '' for (let i = 0; i < this.keys.length; i++) { if (this.keys[i]) ret += ((i == 0) ? '?' : '&') + this.keys[i] + '=' + this.params[this.keys[i]] } return ret } Querystring.prototype.getAbsolutePath = function () { return this.path } Querystring.prototype.toAbsoluteUrl = function () { return this.path + this.toString() } Querystring.prototype.contains = function (key) { let arr = key.split(',') for (let i = 0; i < arr.length; i++) { if (this._contains(arr[i])) return true } return false } Querystring.prototype._contains = function (key) { let value = this.params[key] return (value != null) } function getParameterByName (name, url) { let match = RegExp('[?&]' + name + '=([^&]*)').exec(url) return match && decodeURIComponent(match[1].replace(/\+/g, ' ')) }