sidebar.js 7.7 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===undefined||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. var eventName = ('ontouchend' in document.documentElement) ? 'touchend' : 'click';
  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()||
  54. visible.sidebarbutton()) {
  55. header.dom('.button--info').toggleClass('active')
  56. lychee.content.toggleClass('sidebar');
  57. sidebar.dom().toggleClass('active');
  58. return true;
  59. }
  60. return false;
  61. }
  62. sidebar.setSelectable = function(selectable = true) {
  63. // Attributes/Values inside the sidebar are selectable by default.
  64. // Selection needs to be deactivated to prevent an unwanted selection
  65. // while using multiselect.
  66. if (selectable===true) sidebar.dom().removeClass('notSelectable');
  67. else sidebar.dom().addClass('notSelectable');
  68. }
  69. sidebar.changeAttr = function(attr, value = '-') {
  70. if (attr===undefined||attr===null||attr==='') return false;
  71. // Set a default for the value
  72. if (value===''||value===null) value = '-';
  73. sidebar.dom('.attr_' + attr).html(value);
  74. return true;
  75. }
  76. sidebar.createStructure.photo = function(data) {
  77. if (data===undefined||data===null||data==='') return false;
  78. var editable = false,
  79. exifHash = data.takestamp + data.make + data.model + data.shutter + data.aperture + data.focal + data.iso,
  80. structure = {},
  81. public = '';
  82. // Enable editable when user logged in
  83. if (lychee.publicMode===false) editable = true;
  84. // Set value for public
  85. switch (data.public) {
  86. case '0': public = 'No';
  87. break;
  88. case '1': public = 'Yes';
  89. break;
  90. case '2': public = 'Yes (Album)';
  91. break;
  92. default: public = '-';
  93. break;
  94. }
  95. structure.basics = {
  96. title: 'Basics',
  97. type: sidebar.types.DEFAULT,
  98. rows: [
  99. { title: 'Title', value: data.title, editable },
  100. { title: 'Uploaded', value: data.sysdate },
  101. { title: 'Description', value: data.description, editable },
  102. ]
  103. }
  104. structure.image = {
  105. title: 'Image',
  106. type: sidebar.types.DEFAULT,
  107. rows: [
  108. { title: 'Size', value: data.size },
  109. { title: 'Format', value: data.type },
  110. { title: 'Resolution', value: data.width + ' x ' + data.height }
  111. ]
  112. }
  113. // Only create tags section when user logged in
  114. if (lychee.publicMode===false) {
  115. structure.tags = {
  116. title: 'Tags',
  117. type: sidebar.types.TAGS,
  118. value: build.tags(data.tags),
  119. editable
  120. }
  121. } else {
  122. structure.tags = {}
  123. }
  124. // Only create EXIF section when EXIF data available
  125. if (exifHash!=='0') {
  126. structure.exif = {
  127. title: 'Camera',
  128. type: sidebar.types.DEFAULT,
  129. rows: [
  130. { title: 'Captured', value: data.takedate },
  131. { title: 'Make', value: data.make },
  132. { title: 'Type/Model', value: data.model },
  133. { title: 'Shutter Speed', value: data.shutter },
  134. { title: 'Aperture', value: data.aperture },
  135. { title: 'Focal Length', value: data.focal },
  136. { title: 'ISO', value: data.iso }
  137. ]
  138. }
  139. } else {
  140. structure.exif = {}
  141. }
  142. structure.sharing = {
  143. title: 'Sharing',
  144. type: sidebar.types.DEFAULT,
  145. rows: [
  146. { title: 'Public', value: public },
  147. ]
  148. }
  149. // Construct all parts of the structure
  150. structure = [
  151. structure.basics,
  152. structure.image,
  153. structure.tags,
  154. structure.exif,
  155. structure.sharing
  156. ]
  157. return structure;
  158. }
  159. sidebar.createStructure.album = function(data) {
  160. if (data===undefined||data===null||data==='') return false;
  161. var editable = false,
  162. structure = {},
  163. public = '',
  164. visible = '',
  165. downloadable = '',
  166. password = '';
  167. // Enable editable when user logged in
  168. if (lychee.publicMode===false) editable = true;
  169. // Set value for public
  170. switch (data.public) {
  171. case '0': public = 'No';
  172. break;
  173. case '1': public = 'Yes';
  174. break;
  175. default: public = '-';
  176. break;
  177. }
  178. // Set value for visible
  179. switch (data.visible) {
  180. case '0': visible = 'No';
  181. break;
  182. case '1': visible = 'Yes';
  183. break;
  184. default: visible = '-';
  185. break;
  186. }
  187. // Set value for downloadable
  188. switch (data.downloadable) {
  189. case '0': downloadable = 'No';
  190. break;
  191. case '1': downloadable = 'Yes';
  192. break;
  193. default: downloadable = '-';
  194. break;
  195. }
  196. // Set value for password
  197. switch (data.password) {
  198. case '0': password = 'No';
  199. break;
  200. case '1': password = 'Yes';
  201. break;
  202. default: password = '-';
  203. break;
  204. }
  205. structure.basics = {
  206. title: 'Basics',
  207. type: sidebar.types.DEFAULT,
  208. rows: [
  209. { title: 'Title', value: data.title, editable },
  210. { title: 'Description', value: data.description, editable }
  211. ]
  212. }
  213. structure.album = {
  214. title: 'Album',
  215. type: sidebar.types.DEFAULT,
  216. rows: [
  217. { title: 'Created', value: data.sysdate },
  218. { title: 'Images', value: data.num }
  219. ]
  220. }
  221. structure.share = {
  222. title: 'Share',
  223. type: sidebar.types.DEFAULT,
  224. rows: [
  225. { title: 'Public', value: public },
  226. { title: 'Visible', value: visible },
  227. { title: 'Downloadable', value: downloadable },
  228. { title: 'Password', value: password }
  229. ]
  230. }
  231. // Construct all parts of the structure
  232. structure = [
  233. structure.basics,
  234. structure.album,
  235. structure.share
  236. ]
  237. return structure;
  238. }
  239. sidebar.render = function(structure) {
  240. if (structure===undefined||structure===null||structure==='') return false;
  241. var html = '';
  242. var renderDefault = function(section) {
  243. let _html = '';
  244. _html += `
  245. <div class='divider'>
  246. <h1>${ section.title }</h1>
  247. </div>
  248. <table>
  249. `
  250. section.rows.forEach(function(row) {
  251. let value = row.value;
  252. // Set a default for the value
  253. if (value===''||value===null||value===undefined) value = '-';
  254. // Wrap span-element around value for easier selecting on change
  255. value = `<span class='attr_${ row.title.toLowerCase() }'>${ value }</span>`;
  256. // Add edit-icon to the value when editable
  257. if (row.editable===true) value += ' ' + build.editIcon('edit_' + row.title.toLowerCase());
  258. _html += `
  259. <tr>
  260. <td>${ row.title }</td>
  261. <td>${ value }</td>
  262. </tr>
  263. `
  264. });
  265. _html += `
  266. </table>
  267. `
  268. return _html;
  269. };
  270. var renderTags = function(section) {
  271. let _html = '';
  272. _html += `
  273. <div class='divider'>
  274. <h1>${ section.title }</h1>
  275. </div>
  276. <div id='tags'>
  277. <div class='attr_${ section.title.toLowerCase() }'>${ section.value }</div>
  278. `
  279. // Add edit-icon to the value when editable
  280. if (section.editable===true) _html += build.editIcon('edit_tags');
  281. _html += `
  282. </div>
  283. `
  284. return _html;
  285. }
  286. structure.forEach(function(section) {
  287. if (section.type===sidebar.types.DEFAULT) html += renderDefault(section);
  288. else if (section.type===sidebar.types.TAGS) html += renderTags(section);
  289. });
  290. return html;
  291. }