main.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. (function(window) {
  2. "use strict";
  3. window.upload = {}
  4. }(window));
  5. (function(upload) {
  6. upload.config = {}
  7. upload.load = {
  8. loaded: 0,
  9. doneloaded: function() {
  10. this.loaded -= 1
  11. if (this.loaded <= 0) {
  12. this.cb()
  13. }
  14. },
  15. load: function(filename, test, onload) {
  16. if (test && test()) {
  17. return false
  18. }
  19. var head = document.getElementsByTagName('head')[0]
  20. var script = document.createElement('script')
  21. script.src = './' + filename
  22. script.async = true
  23. script.onload = onload
  24. head.appendChild(script)
  25. return true
  26. },
  27. needsome: function() {
  28. this.loaded += 1
  29. return this
  30. },
  31. done: function(callback) {
  32. this.loaded -= 1
  33. this.cb = callback
  34. return this
  35. },
  36. then: function(callback) {
  37. this.deferred.then(callback)
  38. return this
  39. },
  40. need: function(filename, test) {
  41. this.loaded += 1
  42. if(!this.load(filename, test, this.doneloaded.bind(this))) {
  43. this.loaded -= 1
  44. }
  45. return this
  46. }
  47. }
  48. upload.modules = {
  49. modules: [],
  50. addmodule: function (module) {
  51. this.modules.unshift(module)
  52. upload[module.name] = module
  53. },
  54. initmodule: function (module) {
  55. module.init()
  56. },
  57. setdefault: function (module) {
  58. this.default = module
  59. },
  60. init: function () {
  61. this.modules.forEach(this.initmodule.bind(this))
  62. }
  63. }
  64. upload.modules.addmodule({
  65. name: 'footer',
  66. init: function() {
  67. $('#footer').html(upload.config.footer)
  68. }
  69. })
  70. upload.modules.addmodule({
  71. name: 'route',
  72. init: function () {
  73. window.addEventListener('hashchange', this.hashchange.bind(this))
  74. this.hashchange()
  75. },
  76. setroute: function (module, routeroot, route) {
  77. view = $('.modulecontent.modulearea')
  78. if (!this.currentmodule || this.currentmodule != module) {
  79. // TODO: better
  80. if (this.currentmodule) {
  81. this.currentmodule.unrender()
  82. }
  83. this.currentmodule = module
  84. view.id = 'module_' + module.name
  85. module.render(view)
  86. }
  87. module.initroute(route, routeroot)
  88. },
  89. tryroute: function (route) {
  90. var isroot = route.startsWith('/')
  91. var normalroute = isroot ? route.substring(1) : route
  92. var route = normalroute.substr(normalroute.indexOf('/') + 1)
  93. var routeroot = normalroute.substr(0, normalroute.indexOf('/'))
  94. var chosenmodule
  95. if (!normalroute) {
  96. chosenmodule = upload.modules.default
  97. } else {
  98. upload.modules.modules.every(function (module) {
  99. if (!module.route) {
  100. return true
  101. }
  102. if (module.route(routeroot, route)) {
  103. chosenmodule = module
  104. return false
  105. }
  106. return true
  107. })
  108. }
  109. if (!chosenmodule) {
  110. chosenmodule = upload.modules.default
  111. }
  112. setTimeout(this.setroute.bind(this, chosenmodule, routeroot, route), 0)
  113. },
  114. hashchange: function () {
  115. this.tryroute(window.location.hash.substring(1))
  116. }
  117. })
  118. }(window.upload));
  119. function addError(element, error) {
  120. var deferred = $.Deferred();
  121. console.log(error.toString());
  122. if ( error.status == -1503 ) upload.download.progress('error')
  123. if ( error.status == 0 ) { document.getElementById('nolauncher').className = 'waiting';
  124. document.getElementById('waitauth').className = 'hidden'; }
  125. return deferred.promise();
  126. }
  127. function addErrorwritefile(element, error) {
  128. var deferred = $.Deferred();
  129. console.log("write error : " ,error.toString());
  130. if ( error.status == -1503 ) upload.download.progress('error')
  131. return deferred.promise();
  132. }
  133. function addErrorcreatefile(element, error) {
  134. var deferred = $.Deferred();
  135. console.log("create file error : " ,error.toString());
  136. if ( error.status == -1503 ) upload.download.progress('error')
  137. return deferred.promise();
  138. }
  139. function addErrorcreateservice(element, error) {
  140. var deferred = $.Deferred();
  141. console.log("create service error : " ,error.toString());
  142. if ( error.status == -1503 ) upload.download.progress('error')
  143. return deferred.promise();
  144. }
  145. function addErrorreadservice(element, error) {
  146. var deferred = $.Deferred();
  147. console.log("read service error : " ,error.toString());
  148. upload.download.progress('error')
  149. return deferred.promise();
  150. }
  151. function addErrorcreatedir(element, error) {
  152. var deferred = $.Deferred();
  153. console.log("create dir error : " ,error.toString());
  154. if ( error.status == -1503 ) upload.download.progress('error')
  155. return deferred.promise();
  156. }
  157. function addErrordeletedir(element, error) {
  158. var deferred = $.Deferred();
  159. console.log("delete dir error : " ,error.toString());
  160. upload.download.progress('error')
  161. return deferred.promise();
  162. }
  163. var Safe = null;
  164. function initialize(node) {
  165. Safe = new SafeApp({
  166. id: 'safeid',
  167. name: 'safeshare',
  168. vendor: 'nice',
  169. version: '0.0.1'
  170. }, []); // include SAFEDrive access
  171. //console.log ( 'Initialized a new Safe instance. You can authenticate now.' );
  172. }
  173. function authorize(node) {
  174. Safe.auth.authenticate().then(function() {
  175. //console.log ('Authorizing.');
  176. isAuthorized ( this );
  177. }, addError.bind(null, node.nextElementSibling));
  178. }
  179. function isAuthorized(node) {
  180. Safe.auth.isAuth().then(function(bool) {
  181. var isauth = bool ? 'Authorized successfully.' : 'Not authorized.';
  182. console.log (isauth);
  183. if ( bool == true ) { upload.modules.init(); } // party starts now
  184. else {
  185. document.getElementById('waitauth').className = 'hidden';
  186. document.getElementById('noauth').className = 'waiting';
  187. //alert("Access denied\nLauncher has denied access.");
  188. }
  189. }, addError.bind(null, node.nextElementSibling));
  190. }
  191. function writetofile ( filename, content, whendone, data ) {
  192. var deferred = $.Deferred();
  193. var path = "/"+filename+"/"+filename;
  194. Safe.nfs.updateFile(path, content , {
  195. isPathShared: false
  196. }).then(function() {
  197. //console.log ( 'File Uploaded.');
  198. whendone ( data );
  199. }, addErrorwritefile.bind(null, filename));
  200. return deferred.promise();
  201. }
  202. function createfile ( filename ) {
  203. var deferred = $.Deferred();
  204. var path = "/"+filename+"/"+filename;
  205. Safe.nfs.createFile(path, {
  206. metadata: 'none',
  207. isVersioned: false,
  208. isPathShared: false
  209. }).then(function() {
  210. //console.log ( 'Created file. Now go add content!');
  211. }, addErrorcreatefile.bind(null, filename));
  212. return deferred.promise();
  213. }
  214. function createdir ( dirname ) {
  215. var deferred = $.Deferred();
  216. Safe.nfs.createDirectory(dirname, {
  217. isPrivate: false,
  218. metadata: 'none',
  219. isVersioned: false,
  220. isPathShared: false
  221. }).then(function() {
  222. // console.log ( 'createdir : Created directory.');
  223. }, addErrorcreatedir.bind(null, dirname));
  224. return deferred.promise();
  225. }
  226. function deletename ( name ) {
  227. var deferred = $.Deferred();
  228. var deleteDirectory = "/"+name;
  229. Safe.nfs.deleteDirectory(deleteDirectory, {isPathShared: window.isPathShared}).then(function() {
  230. console.log ( '...');
  231. }, addErrordeletedir.bind(null, name));
  232. Safe.dns.deleteName(name).then(function() {
  233. console.log ( 'File Deleted.');
  234. }, addErrorcreatedir.bind(null, name));
  235. return deferred.promise();
  236. }
  237. function createservice ( filename ) {
  238. var deferred = $.Deferred();
  239. var payload = {
  240. longName: filename,
  241. serviceName: "safeshare_"+filename,
  242. serviceHomeDirPath: "/"+filename,
  243. isPathShared: false
  244. };
  245. createdir ( "/"+filename );
  246. Safe.dns.createServiceAndName(payload).then(function() {
  247. //console.log ( 'Created new service and name.');
  248. }, addErrorcreateservice.bind(null, filename));
  249. return deferred.promise();
  250. }
  251. function Uint8ToBase64(u8Arr){
  252. var CHUNK_SIZE = 0x8000; //arbitrary number
  253. var index = 0;
  254. var length = u8Arr.length;
  255. var result = '';
  256. var slice;
  257. while (index < length) {
  258. slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length));
  259. result += String.fromCharCode.apply(null, slice);
  260. index += CHUNK_SIZE;
  261. }
  262. return btoa(result);
  263. }
  264. (function () {
  265. upload.load.needsome().need('config.js').need('js/shims.js').need('deps/zepto.min.js').done(function() {
  266. upload.load.needsome().need('js/home.js', function() {return upload.home}).done(function() {
  267. if (typeof upload.config != 'undefined') {
  268. initialize(this);
  269. //console.log ( 'storage : ', Safe.storage.get() );
  270. if ( Safe.storage.get() == null ) { var r = confirm( upload.config.disclaimer );/*disclaimer stuff here! */} else { var r = true; };
  271. //var r = confirm( upload.config.disclaimer );
  272. if (r == true) {
  273. //console.log ( "accepted");
  274. document.getElementById('waitauth').className = 'waiting';
  275. document.getElementById('waitnet').className = 'hidden';
  276. //initialize(this);
  277. authorize(this);
  278. } else {
  279. //console.log ( "canceled");
  280. document.getElementById('waitauth').className = 'hidden';
  281. document.getElementById('waitnet').className = 'hidden';
  282. document.getElementById('canceled').className = 'waiting';
  283. return;
  284. }
  285. } else {
  286. alert("Please configure with config.js (see config.js.example)")
  287. }
  288. })
  289. })
  290. }(upload))