/lib/gallery/gallery.js

https://github.com/zeropaper/kern · JavaScript · 170 lines · 125 code · 38 blank · 7 comment · 11 complexity · 7efc53813d000d44365b803cfc05e4b2 MD5 · raw file

  1. (function(gallery){
  2. var onServer = typeof exports != 'undefined',
  3. settings = {},
  4. defaults = {},
  5. _ = onServer ? require('underscore')._ : window._,
  6. Backbone = onServer ? require('backbone') : window.Backbone,
  7. $,
  8. GalleryCollection,
  9. GalleryView
  10. ;
  11. gallery.extensionName = 'KernGallery';
  12. /**
  13. * Implements expender hook
  14. */
  15. gallery.extender = function() {
  16. // this function is overriden on server side, its safe to use the window
  17. var K = this;
  18. $ = K.$;
  19. K.bind('gallery:initialized', function(){
  20. });
  21. K.bind('initialized', function() {
  22. gallery.initialize.call(K);
  23. });
  24. GalleryView = Backbone.View.extend({
  25. tagName: 'ol',
  26. initialize: function() {
  27. _.bindAll(this, 'render');
  28. this.template = _.template($('#kern-gallery-images-template').html());
  29. this.collection.bind('reset', function() {
  30. });
  31. },
  32. events: {
  33. 'click .prev': 'prev',
  34. 'click .next': 'next'
  35. },
  36. render: function() {
  37. if (this.collection.length) $(this.el).html(this.template({
  38. collection: this.collection.toJSON(),
  39. preset: '/i/micro',
  40. current: this.collection.image().toJSON()
  41. }));
  42. return this;
  43. },
  44. prev: function(){
  45. this.collection.prev();
  46. this.render();
  47. },
  48. next: function(){
  49. this.collection.next();
  50. this.render();
  51. }
  52. });
  53. GalleryCollection = Backbone.Collection.extend({
  54. model: K.models.ContentEntry,
  55. current: 0,
  56. view: null,
  57. image: function(n) {
  58. if (_.isUndefined(n)) n = this.current;
  59. return this.at(n);
  60. },
  61. next: function() {
  62. this.current++;
  63. if (this.current >= this.length) this.current = 0;
  64. return this.image();
  65. },
  66. prev: function() {
  67. this.current--;
  68. if (this.current <= 0) this.current = this.length - 1;
  69. return this.image();
  70. },
  71. toJSON: function() {
  72. var coll = this;
  73. return this.map(function(model, i){ return _.extend(model.toJSON(), {cid: model.cid, current: coll.current == i ? 'current' : ''}); });
  74. },
  75. initialize: function(models, options) {
  76. this.view = new GalleryView({
  77. collection: this,
  78. el: options.el
  79. });
  80. this.bind('reset', function() {
  81. this.view.render();
  82. });
  83. }
  84. });
  85. _.extend(settings, K.settings.gallery || (K.settings.gallery = {}));
  86. };
  87. /**
  88. * Implements initialize hook
  89. */
  90. gallery.initialize = function() {
  91. var K = this;
  92. $ = K.$;
  93. K.settings.image.presets.micro = [
  94. '-thumbnail'
  95. , '45x45^'
  96. , '-gravity'
  97. , 'center'
  98. , '-extent'
  99. , '45x45'
  100. ];
  101. if (onServer) return;
  102. var K = this;
  103. $ = K.$;
  104. if (!$('#kern-gallery"').length) {
  105. $('#content').append('<div id="kern-gallery">');
  106. }
  107. else {
  108. $('#kern-gallery"').empty();
  109. }
  110. var kernGallery = new GalleryCollection([], {
  111. el: $('#kern-gallery')[0]
  112. });
  113. window.kernGallery = kernGallery;
  114. function applyGallery() {
  115. var entries = [];
  116. $('.image-jpeg, .image-png, .image-gif').each(function(){
  117. var entry = K.paths[$('a', this).attr('href')];
  118. if (!entry) return;
  119. $(this).remove();
  120. entries.push(entry);
  121. });
  122. kernGallery.reset(entries);
  123. }
  124. K.bind('fileapplied', applyGallery);
  125. applyGallery();
  126. K.trigger('gallery:initialized', kernGallery);
  127. };
  128. _.extend(
  129. gallery,
  130. onServer ? _.extend(gallery, require('./gallery.server.js')) : {}
  131. );
  132. })(typeof exports == 'undefined' ? this.KernGallery = {} : exports);