sidebar.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * @description This module takes care of the sidebar.
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. sidebar = {
  6. _dom: $('#sidebar'),
  7. types: {
  8. DEFAULT : 0,
  9. TAGS : 1
  10. },
  11. createStructure: {}
  12. }
  13. sidebar.dom = function(selector) {
  14. if (selector==null || selector==='') return sidebar._dom
  15. return sidebar._dom.find(selector)
  16. }
  17. sidebar.bind = function() {
  18. // This function should be called after building and appending
  19. // the sidebars content to the DOM.
  20. // This function can be called multiple times, therefore
  21. // event handlers should be removed before binding a new one.
  22. // Event Name
  23. let eventName = lychee.getEventName();
  24. sidebar
  25. .dom('#edit_title')
  26. .off(eventName)
  27. .on(eventName, function() {
  28. if (visible.photo()) photo.setTitle([photo.getID()])
  29. else if (visible.album()) album.setTitle([album.getID()])
  30. })
  31. sidebar
  32. .dom('#edit_description')
  33. .off(eventName)
  34. .on(eventName, function() {
  35. if (visible.photo()) photo.setDescription(photo.getID())
  36. else if (visible.album()) album.setDescription(album.getID())
  37. })
  38. sidebar
  39. .dom('#edit_tags')
  40. .off(eventName)
  41. .on(eventName, function() {
  42. photo.editTags([photo.getID()])
  43. })
  44. sidebar
  45. .dom('#tags .tag span')
  46. .off(eventName)
  47. .on(eventName, function() {
  48. photo.deleteTag(photo.getID(), $(this).data('index'))
  49. })
  50. return true
  51. }
  52. sidebar.toggle = function() {
  53. if (visible.sidebar() || visible.sidebarbutton()) {
  54. header.dom('.button--info').toggleClass('active')
  55. lychee.content.toggleClass('sidebar')
  56. sidebar.dom().toggleClass('active')
  57. return true
  58. }
  59. return false
  60. }
  61. sidebar.setSelectable = function(selectable = true) {
  62. // Attributes/Values inside the sidebar are selectable by default.
  63. // Selection needs to be deactivated to prevent an unwanted selection
  64. // while using multiselect.
  65. if (selectable===true) sidebar.dom().removeClass('notSelectable')
  66. else sidebar.dom().addClass('notSelectable')
  67. }
  68. sidebar.changeAttr = function(attr, value = '-') {
  69. if (attr==null || attr==='') return false
  70. // Set a default for the value
  71. if (value==null || value==='') value = '-'
  72. sidebar.dom('.attr_' + attr).html(value)
  73. return true
  74. }
  75. sidebar.createStructure.photo = function(data) {
  76. if (data==null || data==='') return false
  77. let editable = false,
  78. exifHash = data.takestamp + data.make + data.model + data.shutter + data.aperture + data.focal + data.iso,
  79. structure = {},
  80. _public = ''
  81. // Enable editable when user logged in
  82. if (lychee.publicMode===false) editable = true
  83. // Set value for public
  84. switch (data.public) {
  85. case '0' : _public = 'No'
  86. break
  87. case '1' : _public = 'Yes'
  88. break
  89. case '2' : _public = 'Yes (Album)'
  90. break
  91. default : _public = '-'
  92. break
  93. }
  94. structure.basics = {
  95. title : 'Basics',
  96. type : sidebar.types.DEFAULT,
  97. rows : [
  98. { title: 'Title', value: data.title, editable },
  99. { title: 'Uploaded', value: data.sysdate },
  100. { title: 'Description', value: data.description, editable }
  101. ]
  102. }
  103. structure.image = {
  104. title : 'Image',
  105. type : sidebar.types.DEFAULT,
  106. rows : [
  107. { title: 'Size', value: data.size },
  108. { title: 'Format', value: data.type },
  109. { title: 'Resolution', value: data.width + ' x ' + data.height }
  110. ]
  111. }
  112. // Only create tags section when user logged in
  113. if (lychee.publicMode===false) {
  114. structure.tags = {
  115. title : 'Tags',
  116. type : sidebar.types.TAGS,
  117. value : build.tags(data.tags),
  118. editable
  119. }
  120. } else {
  121. structure.tags = {}
  122. }
  123. // Only create EXIF section when EXIF data available
  124. if (exifHash!=='0') {
  125. structure.exif = {
  126. title : 'Camera',
  127. type : sidebar.types.DEFAULT,
  128. rows : [
  129. { title: 'Captured', value: data.takedate },
  130. { title: 'Make', value: data.make },
  131. { title: 'Type/Model', value: data.model },
  132. { title: 'Shutter Speed', value: data.shutter },
  133. { title: 'Aperture', value: data.aperture },
  134. { title: 'Focal Length', value: data.focal },
  135. { title: 'ISO', value: data.iso }
  136. ]
  137. }
  138. } else {
  139. structure.exif = {}
  140. }
  141. structure.sharing = {
  142. title : 'Sharing',
  143. type : sidebar.types.DEFAULT,
  144. rows : [
  145. { title: 'Public', value: _public }
  146. ]
  147. }
  148. // Construct all parts of the structure
  149. structure = [
  150. structure.basics,
  151. structure.image,
  152. structure.tags,
  153. structure.exif,
  154. structure.sharing
  155. ]
  156. return structure
  157. }
  158. sidebar.createStructure.album = function(data) {
  159. if (data==null || data==='') return false
  160. let editable = false,
  161. structure = {},
  162. _public = '',
  163. visible = '',
  164. downloadable = '',
  165. password = ''
  166. // Enable editable when user logged in
  167. if (lychee.publicMode===false) editable = true
  168. // Set value for public
  169. switch (data.public) {
  170. case '0' : _public = 'No'
  171. break
  172. case '1' : _public = 'Yes'
  173. break
  174. default : _public = '-'
  175. break
  176. }
  177. // Set value for visible
  178. switch (data.visible) {
  179. case '0' : visible = 'No'
  180. break
  181. case '1' : visible = 'Yes'
  182. break
  183. default : visible = '-'
  184. break
  185. }
  186. // Set value for downloadable
  187. switch (data.downloadable) {
  188. case '0' : downloadable = 'No'
  189. break
  190. case '1' : downloadable = 'Yes'
  191. break
  192. default : downloadable = '-'
  193. break
  194. }
  195. // Set value for password
  196. switch (data.password) {
  197. case '0' : password = 'No'
  198. break
  199. case '1' : password = 'Yes'
  200. break
  201. default : password = '-'
  202. break
  203. }
  204. structure.basics = {
  205. title : 'Basics',
  206. type : sidebar.types.DEFAULT,
  207. rows : [
  208. { title: 'Title', value: data.title, editable },
  209. { title: 'Description', value: data.description, editable }
  210. ]
  211. }
  212. structure.album = {
  213. title : 'Album',
  214. type : sidebar.types.DEFAULT,
  215. rows : [
  216. { title: 'Created', value: data.sysdate },
  217. { title: 'Images', value: data.num }
  218. ]
  219. }
  220. structure.share = {
  221. title : 'Share',
  222. type : sidebar.types.DEFAULT,
  223. rows : [
  224. { title: 'Public', value: _public },
  225. { title: 'Visible', value: visible },
  226. { title: 'Downloadable', value: downloadable },
  227. { title: 'Password', value: password }
  228. ]
  229. }
  230. // Construct all parts of the structure
  231. structure = [
  232. structure.basics,
  233. structure.album,
  234. structure.share
  235. ]
  236. return structure
  237. }
  238. sidebar.render = function(structure) {
  239. if (structure==null || structure==='') return false
  240. let html = ''
  241. let renderDefault = function(section) {
  242. let _html = ''
  243. _html += `
  244. <div class='divider'>
  245. <h1>${ section.title }</h1>
  246. </div>
  247. <table>
  248. `
  249. section.rows.forEach(function(row) {
  250. let value = row.value
  251. // Set a default for the value
  252. if (value==='' || value==null) value = '-'
  253. // Wrap span-element around value for easier selecting on change
  254. value = `<span class='attr_${ row.title.toLowerCase() }'>${ value }</span>`
  255. // Add edit-icon to the value when editable
  256. if (row.editable===true) value += ' ' + build.editIcon('edit_' + row.title.toLowerCase())
  257. _html += `
  258. <tr>
  259. <td>${ row.title }</td>
  260. <td>${ value }</td>
  261. </tr>
  262. `
  263. })
  264. _html += `
  265. </table>
  266. `
  267. return _html
  268. }
  269. let renderTags = function(section) {
  270. let _html = ''
  271. _html += `
  272. <div class='divider'>
  273. <h1>${ section.title }</h1>
  274. </div>
  275. <div id='tags'>
  276. <div class='attr_${ section.title.toLowerCase() }'>${ section.value }</div>
  277. `
  278. // Add edit-icon to the value when editable
  279. if (section.editable===true) _html += build.editIcon('edit_tags')
  280. _html += `
  281. </div>
  282. `
  283. return _html
  284. }
  285. structure.forEach(function(section) {
  286. if (section.type===sidebar.types.DEFAULT) html += renderDefault(section)
  287. else if (section.type===sidebar.types.TAGS) html += renderTags(section)
  288. })
  289. return html
  290. }