/jaam/static_media/third_party/ckeditor-3.2/ckeditor/plugins/inlinesinsert/popup-photo.js

https://github.com/CoJMC/jaam · JavaScript · 174 lines · 148 code · 14 blank · 12 comment · 10 complexity · 1406677ca16c6791fc7547544c694df8 MD5 · raw file

  1. $(document).ready(function() {
  2. app = {
  3. models: {},
  4. views: {},
  5. collections: {},
  6. instances: {
  7. collections: {},
  8. models: {},
  9. views: {}
  10. },
  11. utils: {}
  12. };
  13. app.utils.selectTemplate = _.template($("#select-template").html());
  14. app.models.Project = Backbone.Model.extend({});
  15. app.models.PhotoGallery = Backbone.Model.extend({});
  16. app.collections.Projects = Backbone.Collection.extend({
  17. model: app.models.Project,
  18. url: '/api/v1/project',
  19. parse: function(response) {
  20. return response.objects;
  21. }
  22. });
  23. app.collections.PhotoGallerys = Backbone.Collection.extend({
  24. model: app.models.PhotoGallery,
  25. url: '/api/v1/photogallery',
  26. parse: function(response) {
  27. return response.objects;
  28. }
  29. });
  30. app.collections.ProjectList = Backbone.Collection.extend({ model: app.models.Project })
  31. app.models.Photo = Backbone.Model.extend({
  32. defaults: function () {
  33. return {
  34. image: 'https://www.google.com/logos/classicplus.png',
  35. id: "-1"
  36. };
  37. }
  38. });
  39. app.collections.Photos = Backbone.Collection.extend({
  40. model: app.models.Photo,
  41. url: '/api/v1/photo',
  42. parse: function(response) {
  43. return response.objects;
  44. }
  45. });
  46. app.views.PhotoView = Backbone.View.extend({
  47. tagName: "li",
  48. template: _.template($("#photo-template").html()),
  49. events: {
  50. "click a": "insertImage",
  51. },
  52. insertImage: function(e) {
  53. var htmlToInsert = "{{ photo {id} }}";
  54. htmlToInsert = htmlToInsert.replace("{src}", this.model.attributes.image);
  55. htmlToInsert = htmlToInsert.replace("{id}", this.model.attributes.id);
  56. //htmlToInsert = htmlToInsert.replace("{args}", this.model.attributes.i);
  57. if(window.opener == null) {
  58. alert("You MUST use this from the popup window");
  59. return;
  60. }
  61. window.opener.ckeditor_inlinesinsert.insertHtml(htmlToInsert);
  62. },
  63. render: function() {
  64. $(this.el).html(this.template({
  65. url: this.model.attributes.image,
  66. id: this.model.attributes.id
  67. })).addClass("span2");
  68. return this;
  69. },
  70. });
  71. app.views.AppView = Backbone.View.extend({
  72. el: "#photoSelector",
  73. events: {
  74. "change #projectSelector": "changeProject",
  75. "change #gallerySelector": "changeGallery",
  76. },
  77. initialize: function() {
  78. app.instances.collections.projects = new app.collections.Projects;
  79. app.instances.collections.photogallerys = new app.collections.PhotoGallerys;
  80. app.instances.collections.photos = new app.collections.Photos;
  81. app.instances.collections.photos.bind('add', this.addOne, this);
  82. app.instances.collections.photos.bind('reset', this.addAll, this);
  83. app.instances.collections.projects.bind('add', this.renderP, this);
  84. app.instances.collections.projects.bind('reset', this.renderP, this);
  85. app.instances.collections.photogallerys.bind('add', this.renderPG, this);
  86. app.instances.collections.photogallerys.bind('reset', this.renderPG, this);
  87. app.instances.collections.photos.url = "/api/v1/photo/?size=200,200";
  88. app.instances.collections.projects.fetch();
  89. app.instances.collections.photogallerys.fetch();
  90. app.instances.collections.photos.fetch();
  91. },
  92. addOne: function(photo) {
  93. var view = new app.views.PhotoView({model: photo});
  94. $("#photoList").append(view.render().el);
  95. },
  96. addAll: function(photos) {
  97. //console.log("reset");
  98. //console.log(app.instances.collections.photos);
  99. //console.log("/reset");
  100. $("#photoList").html("");
  101. photos.each(this.addOne);
  102. },
  103. renderPG: function(e) {
  104. // modify gallery select
  105. var galleryOptions = {};
  106. _.each(app.instances.collections.photogallerys.models, function(gallery, i) {
  107. galleryOptions[i] = {
  108. id: gallery.attributes.id,
  109. value: gallery.attributes.title
  110. }
  111. });
  112. $("#gallerySelectorContainer").html(app.utils.selectTemplate({ id: 'gallerySelector', options: galleryOptions, label: 'Gallery:' }));
  113. // end gallery select
  114. },
  115. renderP: function() {
  116. // initial load for the projects
  117. var projectOptions = {};
  118. _.each(app.instances.collections.projects.models, function(project, i) {
  119. projectOptions[i] = {
  120. id: project.attributes.id,
  121. value: project.attributes.title
  122. }
  123. });
  124. $("#projectSelectorContainer").html(app.utils.selectTemplate({ id: 'projectSelector', options: projectOptions, label: 'Project:' }));
  125. // end initial project load
  126. return this.el;
  127. },
  128. changeProject: function(e) {
  129. // update url for galleries
  130. // update select box
  131. $("#gallerySelector").val("null");
  132. app.instances.collections.photogallerys.url = "/api/v1/photogallery/";
  133. var project_id = $("#projectSelector").val();
  134. if(project_id != "null") {
  135. app.instances.collections.photogallerys.url += "?project__id=" + project_id;
  136. }
  137. app.instances.collections.photogallerys.reset();
  138. app.instances.collections.photogallerys.fetch();
  139. this.changeGallery();
  140. },
  141. changeGallery: function(e) {
  142. app.instances.collections.photos.url = "/api/v1/photo/";
  143. var gallery_id = $("#gallerySelector").val();
  144. var project_id = $("#projectSelector").val();
  145. //console.log(project_id);
  146. //console.log(gallery_id);
  147. if(gallery_id != "null") {
  148. app.instances.collections.photos.url += "?gallery__id=" + gallery_id + "&size=200,200"
  149. } else if(project_id != "null") {
  150. app.instances.collections.photos.url += "?project__id=" + project_id + "&size=200,200";
  151. } else {
  152. app.instances.collections.photos.url += "?size=200,200";
  153. }
  154. console.log(app.instances.collections.photos.url)
  155. app.instances.collections.photos.reset();
  156. app.instances.collections.photos.fetch();
  157. },
  158. });
  159. app.instances.views.appview = new app.views.AppView();
  160. });