browser.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var mappings = require("./mappings"),
  2. platformName = "browser";
  3. module.exports = function (context) {
  4. var
  5. req = context ? context.requireCordovaModule : require,
  6. Q = req('q'),
  7. path = req('path'),
  8. ET = req('elementtree'),
  9. cordova = req('cordova'),
  10. cordova_lib = cordova.cordova_lib,
  11. ConfigParser = cordova_lib.configparser,
  12. cordova_util = req('cordova-lib/src/cordova/util'),
  13. fs = require("./filesystem")(Q, req('fs'), path),
  14. platforms = {};
  15. // fs, path, ET, cordova_util, ConfigParser
  16. function mapConfig(config) {
  17. var element = {
  18. attrs: {},
  19. children: []
  20. };
  21. if (!config.type) {
  22. throw "no type defined for "+JSON.stringify (config, null, "\t");
  23. }
  24. var mapping = mappings[config.type];
  25. if (!mapping)
  26. throw "no mapping for "+ config.type;
  27. element.tagname = mapping[platformName];
  28. if (mapping.required) {
  29. mapping.required.forEach (function (k) {
  30. if (!(k in config)) {
  31. throw ['attribute', k, 'not found for', config.title, '(' + config.type + ')'].join (" ");
  32. }
  33. });
  34. }
  35. if (mapping.attrs) {
  36. for (var attrName in mapping.attrs) {
  37. if (!config.hasOwnProperty(attrName))
  38. continue;
  39. var attrConfig = mapping.attrs[attrName];
  40. var elementKey = attrConfig[platformName];
  41. var targetCheck = elementKey.split ('@');
  42. var targetAttr;
  43. if (targetCheck.length === 2 && targetCheck[0] === '') {
  44. targetAttr = targetCheck[1];
  45. if (!element.attrs)
  46. element.attrs = {};
  47. element.attrs[targetAttr] = [];
  48. }
  49. if (attrConfig.value) {
  50. if (!attrConfig.value[config[attrName]] || !attrConfig.value[config[attrName]][platformName])
  51. throw "no mapping for type: "+ config.type + ", attr: " + attrName + ", value: " + config[attrName];
  52. if (targetAttr)
  53. element.attrs[targetAttr].push (attrConfig.value[config[attrName]][platformName]);
  54. else
  55. element[elementKey] = attrConfig.value[config[attrName]][platformName]
  56. } else {
  57. if (targetAttr)
  58. element.attrs[targetAttr].push (config[attrName]);
  59. else
  60. element[elementKey] = config[attrName];
  61. }
  62. }
  63. }
  64. if (mapping.fixup && mapping.fixup[platformName]) {
  65. mapping.fixup[platformName] (element, config, mapping);
  66. }
  67. return element;
  68. }
  69. function buildNode(parent, config, stringsArrays) {
  70. for (var attr in config.attrs) {
  71. if (config.attrs[attr] && config.attrs[attr].constructor === Array)
  72. config.attrs[attr] = config.attrs[attr].join ('|');
  73. }
  74. var newNode = new ET.SubElement(parent, config.tagname);
  75. newNode.attrib = config.attrs;
  76. if (config.strings) {
  77. console.log("will push strings array "+JSON.stringify(config.strings));
  78. stringsArrays.push(config.strings);
  79. }
  80. if (config.children) {
  81. config.children.forEach(function(child){
  82. buildNode(newNode, child, stringsArrays);
  83. });
  84. }
  85. }
  86. // build Android settings XML
  87. function buildSettings(configJson) {
  88. var screenNode = new ET.Element('PreferenceScreen'),
  89. resourcesNode = new ET.Element('resources'),
  90. stringsArrays = [];
  91. screenNode.set('xmlns:android', 'http://schemas.android.com/apk/res/android');
  92. // Generate base settings file
  93. configJson.forEach(function (preference) {
  94. var node = mapConfig(preference);
  95. if (preference.type === 'group' && preference.items && preference.items.length) {
  96. preference.items.forEach(function(childNode) {
  97. node.children.push(mapConfig(childNode));
  98. });
  99. }
  100. buildNode(screenNode, node, stringsArrays);
  101. });
  102. // Generate resource file
  103. stringsArrays.forEach(function (stringsArray) {
  104. var titlesXml = new ET.SubElement(resourcesNode, 'string-array'),
  105. valuesXml = new ET.SubElement(resourcesNode, 'string-array');
  106. titlesXml.set("name", "apppreferences_" + stringsArray.name);
  107. valuesXml.set("name", "apppreferences_" + stringsArray.name + 'Values');
  108. for (var i=0, l=stringsArray.titles.length; i<l; i++) {
  109. var titleItemXml = new ET.SubElement(titlesXml, "item"),
  110. valueItemXml = new ET.SubElement(valuesXml, "item");
  111. titleItemXml.text = stringsArray.titles[i];
  112. valueItemXml.text = stringsArray.values[i];
  113. }
  114. });
  115. return {
  116. preferencesDocument: new ET.ElementTree(screenNode),
  117. preferencesStringDocument: new ET.ElementTree(resourcesNode)
  118. };
  119. }
  120. function build(config) {
  121. // cordova contains no packages to build dom,
  122. //
  123. var settingsDocuments = buildSettings(config),
  124. preferencesDocument = settingsDocuments.preferencesDocument,
  125. preferencesStringDocument = settingsDocuments.preferencesStringDocument;
  126. return fs.exists('platforms/android')
  127. // Write preferences xml file
  128. .then(function () { return fs.mkdir('platforms/android/res/xml'); })
  129. .then(function () { return fs.writeFile('platforms/android/res/xml/apppreferences.xml', preferencesDocument.write()); })
  130. // Write localization resource file
  131. .then(function () { return fs.mkdir('platforms/android/res/values'); })
  132. .then(function (prefs) { return fs.writeFile('platforms/android/res/values/apppreferences.xml', preferencesStringDocument.write()); })
  133. .then(function () { console.log('android preferences file was successfully generated'); })
  134. .catch(function (err) {
  135. if (err.code === 'NEXIST') {
  136. console.log("Platform android not found: skipping");
  137. return;
  138. }
  139. throw err;
  140. });
  141. }
  142. function afterPluginInstall () {
  143. return fs.exists('platforms/android')
  144. // Import preferences into native android project
  145. .then(function () { return fs.readFile(path.resolve(__dirname, '../../src/android/AppPreferencesActivity.template')); })
  146. .then(function (tmpl) {
  147. var projectRoot = cordova_lib.cordova.findProjectRoot(process.cwd()),
  148. projectXml = cordova_util.projectConfig(projectRoot),
  149. projectConfig = new ConfigParser(projectXml);
  150. return ('package me.apla.cordova;\n\n' +
  151. 'import ' + (projectConfig.android_packageName() || projectConfig.packageName()) + '.R;\n\n' +
  152. tmpl);
  153. })
  154. .then(function (data) {
  155. var androidPackagePath = "me.apla.cordova".replace (/\./g, '/');
  156. var activityFileName= path.join ('platforms/android/src', androidPackagePath, 'AppPreferencesActivity.java');
  157. return fs.writeFile(activityFileName, data);
  158. })
  159. .catch(function (err) {
  160. if (err.code === 'NEXIST') {
  161. console.log("Platform android not found: skipping");
  162. return;
  163. }
  164. throw err;
  165. });
  166. }
  167. function clean(config) {
  168. var androidPackagePath = "me.apla.cordova".replace (/\./g, '/');
  169. var activityFileName = path.join ('platforms/android/src', androidPackagePath, 'AppPreferencesActivity.java');
  170. return fs.exists('platforms/android')
  171. // Remove preferences xml file
  172. .then(function () { return fs.unlink('platforms/android/res/xml/apppreferences.xml'); })
  173. // Remove localization resource file
  174. .then(function (prefs) { return fs.unlink('platforms/android/res/values/apppreferences.xml'); })
  175. // Remove preferences from native android project
  176. .then(function (data) {
  177. return fs.unlink(activityFileName);
  178. })
  179. .then(function () { console.log('android preferences file was successfully cleaned'); })
  180. .catch(function (err) {
  181. if (err.code === 'NEXIST') {
  182. console.log("Platform android not found: skipping");
  183. return;
  184. } else if (err.code === 'ENOENT' && err.path === activityFileName) {
  185. // Activity not generated, that's fine
  186. return;
  187. }
  188. throw err;
  189. });
  190. }
  191. return {
  192. mapConfig: mapConfig,
  193. buildSettings: buildSettings,
  194. build: build,
  195. afterPluginInstall: afterPluginInstall,
  196. clean: clean
  197. };
  198. };