PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/crm/symfony/src/MDR/IntranetBundle/Resources/public/js/ArchivosIntranet.js

https://gitlab.com/Alberto.SS/crm_verquet
JavaScript | 321 lines | 294 code | 13 blank | 14 comment | 25 complexity | 1a3b54b2fe8480f41316c52d6906b44e MD5 | raw file
  1. var contArchivosIntranet = 0
  2. //Modelos
  3. var ArchivoIntranet = Backbone.Model.extend({
  4. initialize: function(){
  5. this.set({extension: this.getExtension()});
  6. var tipo = this.getTipo();
  7. if(tipo === undefined)
  8. {
  9. this.destroy();
  10. } else {
  11. this.set({icono: tipo.get('icono')});
  12. }
  13. },
  14. defaults: {
  15. idArchivo: contArchivosIntranet++,
  16. nombre: '',
  17. extension: '',
  18. tamanio: 0,
  19. icono: '',
  20. data: '',
  21. archivo: null,
  22. estatus: 0
  23. },
  24. getExtension: function() {
  25. var partes = this.get('nombre').split('.');
  26. return partes[partes.length - 1];
  27. },
  28. getTipo: function() {
  29. return tiposArchivoIntranet.getByExtension(this.get("extension"));
  30. }
  31. });
  32. var TipoArchivoIntranet = Backbone.Model.extend({
  33. initialize: function(){
  34. },
  35. defaults: {
  36. tipo: '',
  37. icono: '',
  38. extensiones: []
  39. }
  40. });
  41. //Colecciones
  42. var ArchivosIntranetCollection = Backbone.Collection.extend({
  43. model: ArchivoIntranet,
  44. byEstatus: function(estatus) {
  45. return this.filter(function(item) {
  46. return item.get('estatus') == estatus;
  47. });
  48. },
  49. byNombre: function(nombre){
  50. return this.find(function(item) {
  51. return item.get('nombre').toLowerCase() == nombre.toLowerCase();
  52. });
  53. }
  54. });
  55. var TiposArchivoIntranetCollection = Backbone.Collection.extend({
  56. model: TipoArchivoIntranet,
  57. getByExtension: function(extension) {
  58. return this.find(function(item) {
  59. return 0 <= _.indexOf(item.get("extensiones"), extension)
  60. });
  61. }
  62. });
  63. //Instancias
  64. var archivosIntranet = new ArchivosIntranetCollection();
  65. var tiposArchivoIntranet = new TiposArchivoIntranetCollection([
  66. {
  67. tipo: 'image',
  68. icono: '',
  69. extensiones: ['jpg', 'jpeg', 'png', 'gif']
  70. },
  71. {
  72. tipo: 'video',
  73. icono: 'fa fa-youtube-play',
  74. extensiones: ['mp4']
  75. },
  76. {
  77. tipo: 'pdf',
  78. icono: 'fa fa-file-pdf-o',
  79. extensiones: ['pdf']
  80. }
  81. ]);
  82. //Vistas Lógicas
  83. var ArchivoIntranetView = Backbone.View.extend({
  84. tagName: "li",
  85. className: "document-row box",
  86. template: archivoListaTemplate,
  87. initialize: function(){
  88. this.render();
  89. this.listenTo(this.model, 'change', this.render);
  90. this.listenTo(this.model, 'destroy', this.destroy);
  91. },
  92. render: function(){
  93. //this.el["data-id"] = this.model.get('idArchivo');
  94. this.$el.html(this.template(this.model.toJSON()));
  95. //this.btnAdd = this.$("button");
  96. return this;
  97. },
  98. events: {
  99. 'click .btnBorrar' : 'borrar',
  100. 'click .btnUpload' : 'upload'
  101. },
  102. //Eventos vista
  103. borrar: function(){
  104. var that = this;
  105. if(this.model.get('estatus') == 1)
  106. {
  107. this.model.set({estatus: -1});
  108. var data = {idMenu: menuBD.idMenu, archivo: this.model.get("nombre")};
  109. $.post(urls.intranet.contenidoBorrarArchivos, data, function(data, textStatus, xhr) {
  110. that.model.set({estatus: 1});
  111. if(data.estado == 1)
  112. {
  113. that.model.destroy();
  114. } else {
  115. alert(data.mensaje);
  116. }
  117. });
  118. } else
  119. {
  120. alert('Este elemento no ha sido cargado.');
  121. }
  122. },
  123. upload: function(){
  124. this.model.set({estatus: -1});
  125. var formData = new FormData();
  126. formData.append('idMenu', menuBD.idMenu);
  127. formData.append('idPlantilla', $("#plantilla").val());
  128. formData.append('archivo', this.model.get('archivo'));
  129. formData.append('idArchivo', this.model.get('idArchivo'));
  130. jQuery.ajax({
  131. url: urls.intranet.contenidoGuardarArchivo,
  132. data: formData,
  133. cache: false,
  134. contentType: false,
  135. processData: false,
  136. type: 'POST',
  137. success: function(data){
  138. var estatus = 0;
  139. if(data.estado == 1)
  140. {
  141. estatus = 1;
  142. }
  143. that.model.set({estatus: estatus});
  144. }
  145. }).error(function(){
  146. that.model.set({estatus: 0});
  147. });
  148. },
  149. //Eventos modelo
  150. destroy: function() {
  151. this.remove();
  152. }
  153. });
  154. //Vista Principal
  155. var ArchivosIntranetView = Backbone.View.extend({
  156. events: {
  157. 'dragenter #holder': 'dragEnter',
  158. 'dragleave #holder': 'dragLeave',
  159. 'dragend #holder': 'dragEnd',
  160. 'dragover #holder': 'dragOver',
  161. 'drop #holder': 'drop',
  162. },
  163. //Funciones
  164. initialize: function() {
  165. this.setElement($("#ArchivosIntranet"));
  166. this.$holder = $("#holder");
  167. console.log('initialize ArchivosIntranetView');
  168. this.listenTo(archivosIntranet, 'add', this.addArchivo);
  169. this.listenTo(archivosIntranet, 'reset', this.resetArchivos);
  170. this.listenTo(archivosIntranet, 'change', this.refreshArchivo);
  171. this.render();
  172. },
  173. render: function() {
  174. this.addAllArchivos(archivosIntranet);
  175. },
  176. readFiles: function(files) {
  177. for (var i = 0; i < files.length; i++) {
  178. console.log(files[i])
  179. }
  180. for (var i = 0; i < files.length; i++) {
  181. this.processFile(files[i]);
  182. }
  183. var archivosPorSubir = archivosIntranet.byEstatus(0);
  184. this.uploadFiles(archivosPorSubir);
  185. },
  186. processFile: function(file) {
  187. //Validar que el archivo no exista en la colección
  188. var existe = archivosIntranet.byNombre(file.name);
  189. if(existe !== undefined){
  190. return;
  191. }
  192. var archivo = {
  193. nombre: file.name,
  194. tamanio: file.size > 0 ? file.size/1024 : 0,
  195. data: '',
  196. archivo: file
  197. };
  198. //Si es una imagen obtener el dataURL para mostrarla
  199. var archivo = archivosIntranet.add(archivo);
  200. if(archivo.get('icono') == '')
  201. {
  202. var reader = new FileReader();
  203. reader.onload = function (event) {
  204. archivo.set({data: event.target.result});
  205. };
  206. reader.readAsDataURL(file);
  207. }
  208. },
  209. uploadFiles: function(archivos) {
  210. _.each(archivos, function(item){
  211. item.set({estatus: -1});
  212. });
  213. var formData = new FormData();
  214. for (var i = 0; i < archivos.length; i++) {
  215. var archivo = archivos[i];
  216. formData.append('idMenu', menuBD.idMenu);
  217. formData.append('idPlantilla', $("#plantilla").val());
  218. formData.append('archivos[]', archivo.get('archivo'));
  219. formData.append('idArchivos[]', archivo.get('idArchivo'));
  220. }
  221. jQuery.ajax({
  222. url: urls.intranet.contenidoGuardarArchivos,
  223. data: formData,
  224. cache: false,
  225. contentType: false,
  226. processData: false,
  227. type: 'POST',
  228. success: function(data){
  229. var estatus = 0;
  230. if(data.estado == 1)
  231. {
  232. estatus = 1;
  233. }
  234. _.each(archivos, function(item){
  235. item.set({estatus: estatus});
  236. });
  237. }
  238. }).error(function(){
  239. _.each(archivos, function(item){
  240. item.set({estatus: 0});
  241. });
  242. });
  243. },
  244. //Eventos vista
  245. dragEnter: function(e) {
  246. console.log("Backbone dragEnterFile");
  247. this.$holder.addClass("dragOver");
  248. return false;
  249. },
  250. dragLeave: function(e) {
  251. console.log("Backbone dragLeaveFile");
  252. this.$holder.removeClass("dragOver");
  253. return false;
  254. },
  255. dragOver: function(e) {
  256. e.stopPropagation();
  257. e.preventDefault();
  258. console.log("Backbone dragOver");
  259. return false;
  260. },
  261. dragEnd: function(e) {
  262. console.log("Backbone dragEnd");
  263. this.$holder.removeClass("dragOver");
  264. return false;
  265. },
  266. drop: function(e) {
  267. e.stopPropagation();
  268. e.preventDefault();
  269. console.log('Backbone drop');
  270. this.$holder.removeClass("dragOver");
  271. var files = [];
  272. if(e.originalEvent !== undefined)
  273. {
  274. files = e.originalEvent.dataTransfer.files;
  275. } else {
  276. files = e.dataTransfer.files;
  277. }
  278. this.readFiles(files);
  279. },
  280. //Eventos modelo
  281. addArchivo: function(item)
  282. {
  283. if(item.getTipo() === undefined)
  284. {
  285. item.destroy();
  286. return;
  287. }
  288. if(item.get('nombre').match('__logo[\.]') || item.get('nombre').match('__fondo[\.]'))
  289. {
  290. return;
  291. }
  292. var view = new ArchivoIntranetView({model: item});
  293. this.$holder.prepend(view.render().el);
  294. },
  295. addAllArchivos: function(items)
  296. {
  297. items.each(this.addArchivo, this);
  298. },
  299. resetArchivos: function(items)
  300. {
  301. this.$el.html('');
  302. this.addAllArchivos(items);
  303. },
  304. reset: function()
  305. {
  306. this.render();
  307. },
  308. });