settings.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /**
  2. * @description Lets you change settings.
  3. */
  4. settings = {}
  5. settings.createConfig = function() {
  6. const action = function(data) {
  7. let dbName = data.dbName || ''
  8. let dbUser = data.dbUser || ''
  9. let dbPassword = data.dbPassword || ''
  10. let dbHost = data.dbHost || ''
  11. let dbTablePrefix = data.dbTablePrefix || ''
  12. if (dbUser.length<1) {
  13. basicModal.error('dbUser')
  14. return false
  15. }
  16. if (dbHost.length<1) dbHost = 'localhost'
  17. if (dbName.length<1) dbName = 'lychee'
  18. let params = {
  19. dbName,
  20. dbUser,
  21. dbPassword,
  22. dbHost,
  23. dbTablePrefix
  24. }
  25. api.post('Config::create', params, function(data) {
  26. if (data!==true) {
  27. // Connection failed
  28. if (data==='Warning: Connection failed!') {
  29. basicModal.show({
  30. body: '<p>Unable to connect to host database because access was denied. Double-check your host, username and password and ensure that access from your current location is permitted.</p>',
  31. buttons: {
  32. action: {
  33. title: 'Retry',
  34. fn: settings.createConfig
  35. }
  36. }
  37. })
  38. return false
  39. }
  40. // Creation failed
  41. if (data==='Warning: Creation failed!') {
  42. basicModal.show({
  43. body: '<p>Unable to create the database. Double-check your host, username and password and ensure that the specified user has the rights to modify and add content to the database.</p>',
  44. buttons: {
  45. action: {
  46. title: 'Retry',
  47. fn: settings.createConfig
  48. }
  49. }
  50. })
  51. return false
  52. }
  53. // Could not create file
  54. if (data==='Warning: Could not create file!') {
  55. basicModal.show({
  56. body: "<p>Unable to save this configuration. Permission denied in <b>'data/'</b>. Please set the read, write and execute rights for others in <b>'data/'</b> and <b>'uploads/'</b>. Take a look at the readme for more information.</p>",
  57. buttons: {
  58. action: {
  59. title: 'Retry',
  60. fn: settings.createConfig
  61. }
  62. }
  63. })
  64. return false
  65. }
  66. // Something went wrong
  67. basicModal.show({
  68. body: '<p>Something unexpected happened. Please try again and check your installation and server. Take a look at the readme for more information.</p>',
  69. buttons: {
  70. action: {
  71. title: 'Retry',
  72. fn: settings.createConfig
  73. }
  74. }
  75. })
  76. return false
  77. } else {
  78. // Configuration successful
  79. window.location.reload()
  80. }
  81. })
  82. }
  83. let msg = `
  84. <p>
  85. Enter your database connection details below:
  86. <input name='dbHost' class='text' type='text' placeholder='Database Host (optional)' value=''>
  87. <input name='dbUser' class='text' type='text' placeholder='Database Username' value=''>
  88. <input name='dbPassword' class='text' type='password' placeholder='Database Password' value=''>
  89. </p>
  90. <p>
  91. Lychee will create its own database. If required, you can enter the name of an existing database instead:
  92. <input name='dbName' class='text' type='text' placeholder='Database Name (optional)' value=''>
  93. <input name='dbTablePrefix' class='text' type='text' placeholder='Table prefix (optional)' value=''>
  94. </p>
  95. `
  96. basicModal.show({
  97. body: msg,
  98. buttons: {
  99. action: {
  100. title: 'Connect',
  101. fn: action
  102. }
  103. }
  104. })
  105. }
  106. settings.createLogin = function() {
  107. const action = function(data) {
  108. let username = data.username
  109. let password = data.password
  110. if (username.length<1) {
  111. basicModal.error('username')
  112. return false
  113. }
  114. if (password.length<1) {
  115. basicModal.error('password')
  116. return false
  117. }
  118. basicModal.close()
  119. let params = {
  120. username,
  121. password
  122. }
  123. api.post('Settings::setLogin', params, function(data) {
  124. if (data!==true) {
  125. basicModal.show({
  126. body: '<p>Unable to save login. Please try again with another username and password!</p>',
  127. buttons: {
  128. action: {
  129. title: 'Retry',
  130. fn: settings.createLogin
  131. }
  132. }
  133. })
  134. }
  135. })
  136. }
  137. let msg = `
  138. <p>
  139. Enter a username and password for your installation:
  140. <input name='username' class='text' type='text' placeholder='New Username' value=''>
  141. <input name='password' class='text' type='password' placeholder='New Password' value=''>
  142. </p>
  143. `
  144. basicModal.show({
  145. body: msg,
  146. buttons: {
  147. action: {
  148. title: 'Create Login',
  149. fn: action
  150. }
  151. }
  152. })
  153. }
  154. settings.setLogin = function() {
  155. const action = function(data) {
  156. let oldPassword = data.oldPassword || ''
  157. let username = data.username || ''
  158. let password = data.password || ''
  159. if (oldPassword.length<1) {
  160. basicModal.error('oldPassword')
  161. return false
  162. }
  163. if (username.length<1) {
  164. basicModal.error('username')
  165. return false
  166. }
  167. if (password.length<1) {
  168. basicModal.error('password')
  169. return false
  170. }
  171. basicModal.close()
  172. let params = {
  173. oldPassword,
  174. username,
  175. password
  176. }
  177. api.post('Settings::setLogin', params, function(data) {
  178. if (data!==true) lychee.error(null, params, data)
  179. })
  180. }
  181. let msg = `
  182. <p>
  183. Enter your current password:
  184. <input name='oldPassword' class='text' type='password' placeholder='Current Password' value=''>
  185. </p>
  186. <p>
  187. Your username and password will be changed to the following:
  188. <input name='username' class='text' type='text' placeholder='New Username' value=''>
  189. <input name='password' class='text' type='password' placeholder='New Password' value=''>
  190. </p>
  191. `
  192. basicModal.show({
  193. body: msg,
  194. buttons: {
  195. action: {
  196. title: 'Change Login',
  197. fn: action
  198. },
  199. cancel: {
  200. title: 'Cancel',
  201. fn: basicModal.close
  202. }
  203. }
  204. })
  205. }
  206. settings.setSorting = function() {
  207. let sortingPhotos = []
  208. let sortingAlbums = []
  209. const action = function() {
  210. sortingAlbums[0] = $('.basicModal select#settings_albums_type').val()
  211. sortingAlbums[1] = $('.basicModal select#settings_albums_order').val()
  212. sortingPhotos[0] = $('.basicModal select#settings_photos_type').val()
  213. sortingPhotos[1] = $('.basicModal select#settings_photos_order').val()
  214. basicModal.close()
  215. albums.refresh()
  216. let params = {
  217. typeAlbums : sortingAlbums[0],
  218. orderAlbums : sortingAlbums[1],
  219. typePhotos : sortingPhotos[0],
  220. orderPhotos : sortingPhotos[1]
  221. }
  222. api.post('Settings::setSorting', params, function(data) {
  223. if (data===true) {
  224. lychee.sortingAlbums = 'ORDER BY ' + sortingAlbums[0] + ' ' + sortingAlbums[1]
  225. lychee.sortingPhotos = 'ORDER BY ' + sortingPhotos[0] + ' ' + sortingPhotos[1]
  226. lychee.load()
  227. } else lychee.error(null, params, data)
  228. })
  229. }
  230. let msg = `
  231. <p>
  232. Sort albums by
  233. <span class="select">
  234. <select id='settings_albums_type'>
  235. <option value='id'>Creation Time</option>
  236. <option value='title'>Title</option>
  237. <option value='description'>Description</option>
  238. <option value='public'>Public</option>
  239. </select>
  240. </span>
  241. in an
  242. <span class="select">
  243. <select id='settings_albums_order'>
  244. <option value='ASC'>Ascending</option>
  245. <option value='DESC'>Descending</option>
  246. </select>
  247. </span>
  248. order.
  249. </p>
  250. <p>
  251. Sort photos by
  252. <span class="select">
  253. <select id='settings_photos_type'>
  254. <option value='id'>Upload Time</option>
  255. <option value='takestamp'>Take Date</option>
  256. <option value='title'>Title</option>
  257. <option value='description'>Description</option>
  258. <option value='public'>Public</option>
  259. <option value='star'>Star</option>
  260. <option value='type'>Photo Format</option>
  261. </select>
  262. </span>
  263. in an
  264. <span class="select">
  265. <select id='settings_photos_order'>
  266. <option value='ASC'>Ascending</option>
  267. <option value='DESC'>Descending</option>
  268. </select>
  269. </span>
  270. order.
  271. </p>
  272. `
  273. basicModal.show({
  274. body: msg,
  275. buttons: {
  276. action: {
  277. title: 'Change Sorting',
  278. fn: action
  279. },
  280. cancel: {
  281. title: 'Cancel',
  282. fn: basicModal.close
  283. }
  284. }
  285. })
  286. if (lychee.sortingAlbums!=='') {
  287. sortingAlbums = lychee.sortingAlbums.replace('ORDER BY ', '').split(' ')
  288. $('.basicModal select#settings_albums_type').val(sortingAlbums[0])
  289. $('.basicModal select#settings_albums_order').val(sortingAlbums[1])
  290. }
  291. if (lychee.sortingPhotos!=='') {
  292. sortingPhotos = lychee.sortingPhotos.replace('ORDER BY ', '').split(' ')
  293. $('.basicModal select#settings_photos_type').val(sortingPhotos[0])
  294. $('.basicModal select#settings_photos_order').val(sortingPhotos[1])
  295. }
  296. }
  297. settings.setDropboxKey = function(callback) {
  298. const action = function(data) {
  299. let key = data.key
  300. if (data.key.length<1) {
  301. basicModal.error('key')
  302. return false
  303. }
  304. basicModal.close()
  305. api.post('Settings::setDropboxKey', { key }, function(data) {
  306. if (data===true) {
  307. lychee.dropboxKey = key
  308. if (callback) lychee.loadDropbox(callback)
  309. } else lychee.error(null, params, data)
  310. })
  311. }
  312. let msg = lychee.html`
  313. <p>
  314. In order to import photos from your Dropbox, you need a valid drop-ins app key from <a href='https://www.dropbox.com/developers/apps/create'>their website</a>. Generate yourself a personal key and enter it below:
  315. <input class='text' name='key' type='text' placeholder='Dropbox API Key' value='$${ lychee.dropboxKey }'>
  316. </p>
  317. `
  318. basicModal.show({
  319. body: msg,
  320. buttons: {
  321. action: {
  322. title: 'Set Dropbox Key',
  323. fn: action
  324. },
  325. cancel: {
  326. title: 'Cancel',
  327. fn: basicModal.close
  328. }
  329. }
  330. })
  331. }