Commit 0840fb0e by Sebastián Katzer

New exec method

parent bf337136
...@@ -122,67 +122,15 @@ exports.add = function (props, callback, scope) { ...@@ -122,67 +122,15 @@ exports.add = function (props, callback, scope) {
this.registerPermission(function(granted) { this.registerPermission(function(granted) {
if (granted) { if (granted) {
exec(fn, null, 'LocalNotification', 'add', [options]); this.exec('add', options, callback, scope);
} }
}); }, this);
return options.id; return options.id;
}; };
/** /**
* Add new entries to the registry (more than one) * Update existing notification specified by ID in options.
*
* @param {Object} options
* The notification properties
* @param {Function} callback
* A function to be called after the notification has been added
* @param {Object} scope
* The scope for the callback function
*
* @return {Number}
* The notification's ID
*/
exports.addMultiple = function (notifications, callback, scope) {
var length = notifications.length;
var notificationsMerged = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var options = this.mergeWithDefaults(notifications[i]);
if (options.id) {
options.id = options.id.toString();
}
if (options.date === undefined) {
options.date = new Date();
}
if (options.title) {
options.title = options.title.toString();
}
if (options.message) {
options.message = options.message.toString();
}
if (typeof options.date == 'object') {
options.date = Math.round(options.date.getTime()/1000);
}
if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
callbackFn = function (cmd) {
eval(cmd);
};
}
notificationsMerged.push(options);
}
cordova.exec(callbackFn, null, 'LocalNotification', 'addMultiple', notificationsMerged);
return options.id;
};
/**
* Update existing notification (currently android only)
* *
* @param {Object} options * @param {Object} options
* The notification properties to update * The notification properties to update
...@@ -190,13 +138,9 @@ exports.addMultiple = function (notifications, callback, scope) { ...@@ -190,13 +138,9 @@ exports.addMultiple = function (notifications, callback, scope) {
* A function to be called after the notification has been updated * A function to be called after the notification has been updated
* @param {Object} scope * @param {Object} scope
* The scope for the callback function * The scope for the callback function
*
* @return {Number}
* The notification's ID
*/ */
exports.update = function (updates, callback, scope) { exports.update = function (options, callback, scope) {
callbackFn = this.createCallbackFn(callback, scope); this.exec('update', options, callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'update', [updates]);
}; };
/** /**
...@@ -210,31 +154,9 @@ exports.update = function (updates, callback, scope) { ...@@ -210,31 +154,9 @@ exports.update = function (updates, callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.clear = function (id, callback, scope) { exports.clear = function (id, callback, scope) {
var id = id.toString(), var notId = (id || '0').toString();
callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'clear', [id]);
};
/** this.exec('clear', notId, callback, scope);
* Clear the specified notifications (more than one).
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notifications has been cleared.
* @param {Object} scope
* The scope for the callback function
*/
exports.clearMultiple = function (ids, callback, scope) {
var length = ids.length;
var idArray = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var id = ids[i].toString();
idArray.push(id);
}
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'clearMultiple', [ids]);
}; };
/** /**
...@@ -246,9 +168,7 @@ exports.clearMultiple = function (ids, callback, scope) { ...@@ -246,9 +168,7 @@ exports.clearMultiple = function (ids, callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.clearAll = function (callback, scope) { exports.clearAll = function (callback, scope) {
var callbackFn = this.createCallbackFn(callback, scope); this.exec('clearAll', null, callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'clearAll', []);
}; };
/** /**
...@@ -262,31 +182,9 @@ exports.clearAll = function (callback, scope) { ...@@ -262,31 +182,9 @@ exports.clearAll = function (callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.cancel = function (id, callback, scope) { exports.cancel = function (id, callback, scope) {
var fn = this.createCallbackFn(callback, scope); var notId = (id || '0').toString();
exec(fn, null, 'LocalNotification', 'cancel', [(id || '0').toString()]); this.exec('cancel', notId, callback, scope);
};
/**
* Cancel the specified notifications (more than one).
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notifications has been canceled
* @param {Object} scope
* The scope for the callback function
*/
exports.cancelMultiple = function (ids, callback, scope) {
var length = ids.length;
var idArray = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var id = ids[i].toString();
idArray.push(id);
}
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'cancelMultiple', [ids]);
}; };
/** /**
...@@ -298,9 +196,7 @@ exports.cancelMultiple = function (ids, callback, scope) { ...@@ -298,9 +196,7 @@ exports.cancelMultiple = function (ids, callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.cancelAll = function (callback, scope) { exports.cancelAll = function (callback, scope) {
var fn = this.createCallbackFn(callback, scope); this.exec('cancelAll', null, callback, scope);
exec(fn, null, 'LocalNotification', 'cancelAll', []);
}; };
/** /**
...@@ -312,9 +208,7 @@ exports.cancelAll = function (callback, scope) { ...@@ -312,9 +208,7 @@ exports.cancelAll = function (callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.getScheduledIds = function (callback, scope) { exports.getScheduledIds = function (callback, scope) {
var fn = this.createCallbackFn(callback, scope); this.exec('getScheduledIds', null, callback, scope);
exec(fn, null, 'LocalNotification', 'getScheduledIds', []);
}; };
/** /**
...@@ -328,9 +222,9 @@ exports.getScheduledIds = function (callback, scope) { ...@@ -328,9 +222,9 @@ exports.getScheduledIds = function (callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.isScheduled = function (id, callback, scope) { exports.isScheduled = function (id, callback, scope) {
var fn = this.createCallbackFn(callback, scope); var notId = (id || '0').toString();
exec(fn, null, 'LocalNotification', 'isScheduled', [id.toString()]); this.exec('isScheduled', notId, callback, scope);
}; };
/** /**
...@@ -342,9 +236,7 @@ exports.isScheduled = function (id, callback, scope) { ...@@ -342,9 +236,7 @@ exports.isScheduled = function (id, callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.getTriggeredIds = function (callback, scope) { exports.getTriggeredIds = function (callback, scope) {
var fn = this.createCallbackFn(callback, scope); this.exec('getTriggeredIds', null, callback, scope);
exec(fn, null, 'LocalNotification', 'getTriggeredIds', []);
}; };
/** /**
...@@ -358,9 +250,9 @@ exports.getTriggeredIds = function (callback, scope) { ...@@ -358,9 +250,9 @@ exports.getTriggeredIds = function (callback, scope) {
* The scope for the callback function * The scope for the callback function
*/ */
exports.isTriggered = function (id, callback, scope) { exports.isTriggered = function (id, callback, scope) {
var fn = this.createCallbackFn(callback, scope); var notId = (id || '0').toString();
exec(fn, null, 'LocalNotification', 'isTriggered', [id.toString()]); this.exec('isTriggered', notId, callback, scope);
}; };
/** /**
...@@ -401,12 +293,123 @@ exports.registerPermission = function (callback, scope) { ...@@ -401,12 +293,123 @@ exports.registerPermission = function (callback, scope) {
exec(fn, null, 'LocalNotification', 'registerPermission', []); exec(fn, null, 'LocalNotification', 'registerPermission', []);
}; };
/**
* @deprecated
*
* Register permission to show notifications if not already granted.
*
* @param {Function} callback
* The function to be exec as the callback
* @param {Object?} scope
* The callback function's scope
*/
exports.promptForPermission = function (callback, scope) { exports.promptForPermission = function (callback, scope) {
console.warn('Depreated: Please use `notification.local.registerPermission` instead.');
console.warn(
'`notification.local.promptForPermission` is deprecated,',
'please use `notification.local.registerPermission`.'
);
exports.registerPermission.apply(this, arguments); exports.registerPermission.apply(this, arguments);
}; };
/** /**
* Add new entries to the registry (more than one)
*
* @param {Object} options
* The notification properties
* @param {Function} callback
* A function to be called after the notification has been added
* @param {Object} scope
* The scope for the callback function
*
* @return {Number}
* The notification's ID
*/
exports.addMultiple = function (notifications, callback, scope) {
var length = notifications.length;
var notificationsMerged = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var options = this.mergeWithDefaults(notifications[i]);
if (options.id) {
options.id = options.id.toString();
}
if (options.date === undefined) {
options.date = new Date();
}
if (options.title) {
options.title = options.title.toString();
}
if (options.message) {
options.message = options.message.toString();
}
if (typeof options.date == 'object') {
options.date = Math.round(options.date.getTime()/1000);
}
if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
callbackFn = function (cmd) {
eval(cmd);
};
}
notificationsMerged.push(options);
}
cordova.exec(callbackFn, null, 'LocalNotification', 'addMultiple', notificationsMerged);
return options.id;
};
/**
* Clear the specified notifications (more than one).
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notifications has been cleared.
* @param {Object} scope
* The scope for the callback function
*/
exports.clearMultiple = function (ids, callback, scope) {
var length = ids.length;
var idArray = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var id = ids[i].toString();
idArray.push(id);
}
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'clearMultiple', [ids]);
};
/**
* Cancel the specified notifications (more than one).
*
* @param {String} id
* The ID of the notification
* @param {Function} callback
* A function to be called after the notifications has been canceled
* @param {Object} scope
* The scope for the callback function
*/
exports.cancelMultiple = function (ids, callback, scope) {
var length = ids.length;
var idArray = new Array(),
callbackFn = this.createCallbackFn(callback, scope);
for (var i=0;i<length;i++){
var id = ids[i].toString();
idArray.push(id);
}
var callbackFn = this.createCallbackFn(callback, scope);
cordova.exec(callbackFn, null, 'LocalNotification', 'cancelMultiple', [ids]);
};
/**
* Occurs when a notification was added. * Occurs when a notification was added.
* *
* @param {String} id * @param {String} id
...@@ -415,8 +418,10 @@ exports.promptForPermission = function (callback, scope) { ...@@ -415,8 +418,10 @@ exports.promptForPermission = function (callback, scope) {
* Either "foreground" or "background" * Either "foreground" or "background"
* @param {String} json * @param {String} json
* A custom (JSON) string * A custom (JSON) string
* @param {Object} data
* The notification properties
*/ */
exports.onadd = function (id, state, json) {}; exports.onadd = function (id, state, json, data) {};
/** /**
* Occurs when the notification is triggered. * Occurs when the notification is triggered.
...@@ -427,8 +432,10 @@ exports.onadd = function (id, state, json) {}; ...@@ -427,8 +432,10 @@ exports.onadd = function (id, state, json) {};
* Either "foreground" or "background" * Either "foreground" or "background"
* @param {String} json * @param {String} json
* A custom (JSON) string * A custom (JSON) string
* @param {Object} data
* The notification properties
*/ */
exports.ontrigger = function (id, state, json) {}; exports.ontrigger = function (id, state, json, data) {};
/** /**
* Fires after the notification was clicked. * Fires after the notification was clicked.
...@@ -439,8 +446,10 @@ exports.ontrigger = function (id, state, json) {}; ...@@ -439,8 +446,10 @@ exports.ontrigger = function (id, state, json) {};
* Either "foreground" or "background" * Either "foreground" or "background"
* @param {String} json * @param {String} json
* A custom (JSON) string * A custom (JSON) string
* @param {Object} data
* The notification properties
*/ */
exports.onclick = function (id, state, json) {}; exports.onclick = function (id, state, json, data) {};
/** /**
* Fires if the notification was canceled. * Fires if the notification was canceled.
...@@ -451,8 +460,10 @@ exports.onclick = function (id, state, json) {}; ...@@ -451,8 +460,10 @@ exports.onclick = function (id, state, json) {};
* Either "foreground" or "background" * Either "foreground" or "background"
* @param {String} json * @param {String} json
* A custom (JSON) string * A custom (JSON) string
* @param {Object} data
* The notification properties
*/ */
exports.oncancel = function (id, state, json) {}; exports.oncancel = function (id, state, json, data) {};
/** /**
* Get fired when the notification was cleared. * Get fired when the notification was cleared.
...@@ -463,8 +474,10 @@ exports.oncancel = function (id, state, json) {}; ...@@ -463,8 +474,10 @@ exports.oncancel = function (id, state, json) {};
* Either "foreground" or "background" * Either "foreground" or "background"
* @param {String} json * @param {String} json
* A custom (JSON) string * A custom (JSON) string
* @param {Object} data
* The notification properties
*/ */
exports.onclear = function (id, state, json) {}; exports.onclear = function (id, state, json, data) {};
/** /**
...@@ -583,3 +596,30 @@ exports.createCallbackFn = function (callbackFn, scope) { ...@@ -583,3 +596,30 @@ exports.createCallbackFn = function (callbackFn, scope) {
callbackFn.apply(scope || this, arguments); callbackFn.apply(scope || this, arguments);
}; };
}; };
/**
* @private
*
* Executes the native counterpart.
*
* @param {String} action
* The name of the action
* @param args[]
* Array of arguments
* @param {Function} callback
* The callback function
* @param {Object} scope
* The scope for the function
*/
exports.exec = function (action, args, callback, scope) {
var fn = this.createCallbackFn(callback, scope),
params = [];
if (Array.isArray(args)) {
params = args;
} else if (args) {
params.push(args);
}
exec(fn, null, 'LocalNotification', action, params);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment