Keine Beschreibung

email_composer.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright (c) 2016 Martin Reinhardt
  3. Copyright 2013-2014 appPlant UG
  4. Licensed to the Apache Software Foundation (ASF) under one
  5. or more contributor license agreements. See the NOTICE file
  6. distributed with this work for additional information
  7. regarding copyright ownership. The ASF licenses this file
  8. to you under the Apache License, Version 2.0 (the
  9. "License"); you may not use this file except in compliance
  10. with the License. You may obtain a copy of the License at
  11. http://www.apache.org/licenses/LICENSE-2.0
  12. Unless required by applicable law or agreed to in writing,
  13. software distributed under the License is distributed on an
  14. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. KIND, either express or implied. See the License for the
  16. specific language governing permissions and limitations
  17. under the License.
  18. */
  19. /**
  20. * @namespace
  21. */
  22. var EmailComposerPlugin = function () {
  23. };
  24. var exec = require('cordova/exec');
  25. EmailComposerPlugin.prototype = {
  26. /**
  27. * List of all registered mail app aliases.
  28. */
  29. aliases: {
  30. gmail: 'com.google.android.gm'
  31. },
  32. /**
  33. * List of all available options with their default value.
  34. *
  35. * @return {Object}
  36. */
  37. getDefaults: function () {
  38. return {
  39. app: undefined,
  40. subject: '',
  41. body: '',
  42. to: [],
  43. cc: [],
  44. bcc: [],
  45. attachments: [],
  46. isHtml: true
  47. };
  48. },
  49. /**
  50. * Verifies if sending emails is supported on the device.
  51. *
  52. * @param {Function} callback
  53. * A callback function to be called with the result
  54. * @param {Object} scope
  55. * The scope of the callback
  56. */
  57. isAvailable: function (callback, scope) {
  58. var fn = this.createCallbackFn(callback, scope);
  59. exec(function () {
  60. fn(true);
  61. }, function () {
  62. fn(false);
  63. }, 'EmailComposer', 'isAvailable', []);
  64. },
  65. /**
  66. * Displays the email composer pre-filled with data.
  67. *
  68. * @param {Object} options
  69. * Different properties of the email like the body, subject
  70. * @param {Function} callback
  71. * A callback function to be called with the result
  72. * @param {Object?} scope
  73. * The scope of the callback
  74. */
  75. open: function (options, callback, scope) {
  76. var fn = this.createCallbackFn(callback, scope);
  77. options = this.mergeWithDefaults(options || {});
  78. exec(fn, null, 'EmailComposer', 'open', [options]);
  79. },
  80. /**
  81. * Adds a new mail app alias.
  82. *
  83. * @param {String} alias
  84. * The alias name
  85. * @param {String} packageName
  86. * The package name
  87. */
  88. addAlias: function (alias, packageName) {
  89. this.aliases[alias] = packageName;
  90. },
  91. /**
  92. * @depreacted
  93. */
  94. isServiceAvailable: function () {
  95. console.log('`email.isServiceAvailable` is deprecated.' +
  96. ' Please use `email.isAvailable` instead.');
  97. this.isAvailable.apply(this, arguments);
  98. },
  99. /**
  100. * Alias für `open()`.
  101. */
  102. openDraft: function () {
  103. this.open.apply(this, arguments);
  104. },
  105. /**
  106. * @private
  107. *
  108. * Merge settings with default values.
  109. *
  110. * @param {Object} options
  111. * The custom options
  112. *
  113. * @retrun {Object}
  114. * Default values merged
  115. * with custom values
  116. */
  117. mergeWithDefaults: function (options) {
  118. var defaults = this.getDefaults();
  119. if (options.hasOwnProperty('isHTML')) {
  120. options.isHtml = options.isHTML;
  121. }
  122. if (options.hasOwnProperty('app')) {
  123. var packageName = this.aliases[options.app];
  124. options.app = packageName || options.app;
  125. }
  126. for (var key in defaults) {
  127. if (!options.hasOwnProperty(key)) {
  128. options[key] = defaults[key];
  129. continue;
  130. }
  131. var custom_ = options[key],
  132. default_ = defaults[key];
  133. if (custom_ === null || custom_ === undefined) {
  134. options[key] = default_;
  135. continue;
  136. }
  137. if (typeof default_ != typeof custom_) {
  138. if (typeof default_ == 'string') {
  139. options[key] = custom_.join('');
  140. }
  141. else if (typeof default_ == 'object') {
  142. options[key] = [custom_.toString()];
  143. }
  144. }
  145. }
  146. return options;
  147. },
  148. /**
  149. * @private
  150. *
  151. * Creates a callback, which will be executed
  152. * within a specific scope.
  153. *
  154. * @param {Function} callbackFn
  155. * The callback function
  156. * @param {Object} scope
  157. * The scope for the function
  158. *
  159. * @return {Function}
  160. * The new callback function
  161. */
  162. createCallbackFn: function (callbackFn, scope) {
  163. if (typeof callbackFn != 'function') {
  164. return function () {
  165. };
  166. } else {
  167. return function () {
  168. callbackFn.apply(scope || this, arguments);
  169. };
  170. }
  171. }
  172. };
  173. module.exports = new EmailComposerPlugin();