PageRenderTime 66ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 1ms

/js/sthlm-admin-page.js

https://github.com/Jonatan/Sthlm-Gallery
JavaScript | 639 lines | 158 code | 74 blank | 407 comment | 5 complexity | 1caa3805f8e7abf4e82342db24b698b3 MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. jQuery(document).ready(function($){
  6. var $aGallery = $('.sthlm-gallery'), // all gellery items
  7. $currGallerySettings = $('#sthlm_current_gallery_images_settings'), // wrapper for curr gallery images save button
  8. $currImages = $('#sthlm_current_gallery_images'), // wrapper for the gallery images
  9. $currImgContainer = $('#sthlm_current_gallery_images'), // the wrapper for the curr gallery images
  10. $allImagesContainer = $('#sthlm_all_images'), // the wrapper for all images
  11. dropState = false, // used for drag/drop on init?
  12. dummy = 'var';
  13. // Add class to all img in the current gallery
  14. var imgInGallery = function(){
  15. var imagesString = $currImages.attr('data-gallery-thumbs'),
  16. images = imagesString.split(','),
  17. numImg = (images.length - 1);
  18. for(var i = 0; i <= numImg;i++){
  19. $allImagesContainer.find(".sthlm-thumb[data-thumb-id='"+ images[i] +"']").addClass('sthlm-thumb-exist');
  20. }
  21. }
  22. // prevent manual submit
  23. $('#sthlm_lightbox').find('form').live('submit', function(){return false;});
  24. /**
  25. *
  26. * ADD
  27. *
  28. */
  29. // load and show the form
  30. $('#sthlm_add_gallery_button').click(function(){
  31. // set post data
  32. var data = {
  33. action: 'sthlm_load_lightbox_form_ajax',
  34. form: 'add_gallery'
  35. };
  36. $.post(ajaxurl, data, function(response) {
  37. $('#sthlm_lightbox').html(response);
  38. $.colorbox({inline:true, href:"#sthlm_lightbox"});
  39. });
  40. });
  41. // Save the gallery
  42. $('#sthlm_submit_new_gallery').live('click', function(){
  43. var $this = $(this),
  44. title = $('#sthlm_gallery_title_input').val(),
  45. desc = $('#sthlm_gallery_desc_textarea').val();
  46. // no title
  47. if(title === ''){
  48. alert('No title');
  49. return false;
  50. }
  51. // set post data
  52. var data = {
  53. action: 'sthlm_gallery_ajax_add',
  54. title: title,
  55. desc: desc
  56. };
  57. // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  58. $.post(ajaxurl, data, function(response) {
  59. $('#sthlm_galleries').prepend(response);
  60. $.colorbox.close();
  61. });
  62. return false;
  63. });
  64. /**
  65. *
  66. * Select gallery
  67. *
  68. */
  69. $aGallery.live('click', function(e){
  70. // stop click on remove to fire an event
  71. if($(e.target).is('.remove-sthlm-gallery') ) {return;}
  72. var $this = $(this),
  73. dataID = $this.attr('data-gallery-id');
  74. // remove slected from all galleries and add selected to this gallery
  75. $aGallery.removeClass('selected-sthlm-gallery');
  76. $this.addClass('selected-sthlm-gallery');
  77. $currGallerySettings.attr('data-gallery-id', dataID);
  78. // set post data
  79. var data = {
  80. action: 'sthlm_get_current_gallery_images_ajax',
  81. id: dataID
  82. };
  83. $.post(ajaxurl, data, function(response) {
  84. $currImages.html(response);
  85. // remove class for used images
  86. $allImagesContainer.find('.sthlm-thumb').removeClass('sthlm-thumb-exist');
  87. // Put all img id's in an array
  88. var id = '',
  89. i = 0;
  90. $currImages.find('.sthlm-thumb').each(function(){
  91. var thumbID = $(this).attr('data-thumb-id');
  92. if(i === 0){
  93. id += thumbID;
  94. }else{
  95. id += ','+thumbID;
  96. }
  97. $allImagesContainer.find(".sthlm-thumb[data-thumb-id='"+thumbID+"']").addClass('sthlm-thumb-exist');
  98. i++;
  99. });
  100. $currImages.attr('data-gallery-thumbs', id);
  101. });
  102. });
  103. /**
  104. *
  105. * DELETE GALLERY
  106. *
  107. */
  108. $('.remove-sthlm-gallery').live('dblclick', function(){
  109. var $this = $(this),
  110. $gallery = $this.parent(),
  111. dataID = $gallery.attr('data-id');
  112. // set post data
  113. var data = {
  114. action: 'sthlm_delete_gallery_ajax',
  115. id: dataID
  116. };
  117. // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  118. $.post(ajaxurl, data, function(response) {
  119. if(response === 'error'){
  120. alert('Please try again!');
  121. }else{
  122. // remove the object
  123. $gallery.remove();
  124. }
  125. });
  126. return false;
  127. });
  128. /*
  129. *
  130. * UPDATE GALLERY IMAGES/ORDER
  131. *
  132. */
  133. $('#sthlm_uppdate_gallery_images').click(function(){
  134. var $this = $(this),
  135. $loader = $this.find('.sthlm-gallery-loader'),
  136. order = [],
  137. id = $currGallerySettings.attr('data-gallery-id'), // data-gallery-id
  138. i = 0;
  139. // show loader
  140. $loader.removeClass('hidden');
  141. // get all images in order and there id
  142. $('#sthlm_current_gallery_images .sthlm-thumb').each(function(){
  143. order[i] = $(this).attr('data-thumb-id');
  144. i++;
  145. });
  146. // set post data
  147. var data = {
  148. action: 'sthlm_gallery_ajax_save_order',
  149. order: order,
  150. id: id
  151. };
  152. $.post(ajaxurl, data, function(response) {
  153. // hide loader
  154. $loader.addClass('hidden');
  155. });
  156. return false;
  157. });
  158. /**
  159. *
  160. * Remove img
  161. *
  162. */
  163. $('.remove-sthlm-thumb').live('click', function(){
  164. var $this = $(this),
  165. $thumb = $this.closest('.sthlm-thumb'),
  166. thumbID = $thumb.attr('data-thumb-id');
  167. $thumb.remove();
  168. $allImagesContainer.find(".sthlm-thumb[data-thumb-id='"+thumbID+"']").removeClass('sthlm-thumb-exist');
  169. });
  170. /*
  171. *
  172. * SORTER
  173. *
  174. *
  175. */
  176. var sthlmDragDrop = function(){
  177. //$('.sthlm-images-container .sthlm-thumb').draggable({
  178. $allImagesContainer.find('.sthlm-thumb').not('.sthlm-thumb-exist').draggable({
  179. //appendTo: ".sthlm-images-container",
  180. helper: "clone",
  181. opacity: 1,
  182. cancel: '.sthlm-thumb-exist',
  183. disabled: dropState/*,
  184. start: function(event, ui){
  185. var data = ui.helper.attr('data-thumb-id');
  186. if($currImgContainer.find("div[data-thumb-id='"+data+"']").length != 0){
  187. $(this).addClass('sthlm-img-exist');
  188. }
  189. },
  190. stop: function(event, ui){
  191. $(this).removeClass('sthlm-img-exist');
  192. }*/
  193. });
  194. }
  195. sthlmDragDrop();
  196. $currImgContainer.droppable({
  197. activeClass: "ui-state-default",
  198. hoverClass: "ui-state-hover",
  199. accept: ":not(.ui-sortable-helper)",
  200. drop: function( event, ui ) {
  201. var $this = $(this);
  202. // clone the img to the gallery
  203. $this.append(ui.draggable.clone());
  204. // add class selected to the img
  205. ui.draggable.addClass('sthlm-thumb-exist');
  206. }
  207. });
  208. $currImgContainer.sortable({
  209. placeholder: 'ui-state-highlight',
  210. forcePlaceholderSize: true,
  211. items: '.sthlm-thumb',
  212. helper: 'orginal'//'clone'
  213. }).disableSelection();
  214. /*
  215. *
  216. * FILTER AND SUCH
  217. *
  218. */
  219. $('#sthlm-gallery-datepicker select, #sthlm-gallery-filter input').change( function(){
  220. var data = {
  221. query: $(this).val(),
  222. context: $(this).attr('id'),
  223. action: 'sthlm_gallery_query_attachments'
  224. }
  225. $.get(ajaxurl, data, function(data) {
  226. $allImagesContainer.html(data);
  227. sthlmDragDrop();
  228. imgInGallery();
  229. });
  230. });
  231. /*
  232. var dropState = true; // prevent drag n drop if no gallery is set
  233. // all objects
  234. var $galleryImages = $('#images_in_sthlm_gallery_wrapper'), // the wrapper div for gallery images -> .sthlm-gallery-inside
  235. $selectedImgWrap = $galleryImages.find('.sthlm-gallery-inside'),
  236. $allImagesWrapp = $('.sthlm-gallery-inside-all'),
  237. $gallery = $('.sthlm-gallery-folder');
  238. // sort the thumbs
  239. var $thumb = $('.sthlm-thumb'),
  240. $placeholder = $('.sthlm-gallery-inside');
  241. /**
  242. *
  243. * Select gallery/folder
  244. *
  245. *
  246. $gallery.live('click', function(e){
  247. // stop click on remove to fire an event
  248. if($(e.target).is('.remove-sthlm-gallery') ) { return; }
  249. var $this = $(this),
  250. dataID = $this.attr('data-id');
  251. $('.sthlm-gallery-folder').removeClass('selected-sthlm-gallery');
  252. $this.addClass('selected-sthlm-gallery');
  253. $galleryImages.attr('data-id', dataID);
  254. // set post data
  255. var data = {
  256. action: 'sthlm_gallery_ajax_folder_chosen',
  257. id: dataID
  258. };
  259. $.post(ajaxurl, data, function(response) {
  260. $selectedImgWrap.html(response);
  261. });
  262. // sort fields
  263. $('.sthlm-thumb').draggable( "option", "disabled", false );
  264. dropState = false;
  265. });
  266. /*
  267. *
  268. * SORTER
  269. *
  270. *
  271. *
  272. var sthlmDragDrop = function(){
  273. $('.sthlm-gallery-inside-all .sthlm-thumb').draggable({
  274. appendTo: ".sthlm-gallery-inside-all",
  275. helper: "clone",
  276. opacity: 1,
  277. disabled: dropState
  278. });
  279. }
  280. sthlmDragDrop();
  281. $('.sthlm-gallery-inside-current').droppable({
  282. activeClass: "ui-state-default",
  283. hoverClass: "ui-state-hover",
  284. accept: ":not(.ui-sortable-helper)",
  285. drop: function( event, ui ) {
  286. var $this = $(this),
  287. data = ui.draggable.attr('data-thumb-id');
  288. // if not allready exist
  289. if($this.find("div[data-thumb-id='"+data+"']").length === 0){
  290. $(this).append(ui.draggable.clone());
  291. }else{
  292. //alert('Bilden finns redan');
  293. }
  294. }
  295. });
  296. $('.sthlm-gallery-inside-current').sortable({
  297. placeholder: 'ui-state-highlight',
  298. forcePlaceholderSize: true,
  299. items: '.sthlm-thumb',
  300. helper: 'clone'
  301. }).disableSelection();
  302. /*
  303. *
  304. * UPDATE GALLERY IMAGES/ORDER
  305. *
  306. *
  307. $('#uppdate_sthlm_gallery_images').click(function(){
  308. var $this = $(this),
  309. $loader = $this.find('.sthlm-gallery-loader'),
  310. order = new Array,
  311. id = $('#images_in_sthlm_gallery_wrapper').attr('data-id'), // data-gallery-id
  312. i = 0;
  313. // show loader
  314. $loader.removeClass('hidden');
  315. // get all images in order and there id
  316. $('#images_in_sthlm_gallery_wrapper .sthlm-thumb').each(function(){
  317. order[i] = $(this).attr('data-thumb-id');
  318. i++;
  319. });
  320. // set post data
  321. var data = {
  322. action: 'sthlm_gallery_ajax_save_order',
  323. order: order,
  324. id: id
  325. };
  326. $.post(ajaxurl, data, function(response) {
  327. // hide loader
  328. $loader.addClass('hidden');
  329. });
  330. return false;
  331. });
  332. /**
  333. *
  334. * Remove img
  335. *
  336. *
  337. $('.remove-sthlm-thumb').live('click', function(){
  338. $(this).closest('.sthlm-thumb').remove();
  339. });
  340. // equal height on the columns
  341. var gallery_box_height = 0;
  342. $('#wrapp_all_sthlm_images .sthlm-gallery-inside').each(function(){
  343. var box_height = $(this).height();
  344. if(box_height > gallery_box_height) gallery_box_height = box_height;
  345. //console.log(gallery_box_height);
  346. }).css({"min-height": gallery_box_height+'px'});
  347. /*
  348. *
  349. * ADD A GALLERY
  350. *
  351. *
  352. // open lightbox to add gallery
  353. $('#add_sthlm_gallery_button').colorbox({inline:true});
  354. // prevent manual submit
  355. $('#add_sthlm_gallery_form').submit(function(){ return false; });
  356. //add gallery
  357. $('#submit_new_sthlm_gallery').click(function(){
  358. var $this = $(this),
  359. title = $('#sthlm_gallery_title_input').val(),
  360. desc = $('#sthlm_gallery_desc_textarea').val();
  361. // no title
  362. if(title === ''){
  363. alert('No title');
  364. return false;
  365. }
  366. // set post data
  367. var data = {
  368. action: 'sthlm_gallery_ajax_add',
  369. title: title,
  370. desc: desc
  371. };
  372. // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  373. $.post(ajaxurl, data, function(response) {
  374. $('#gallery_dir_wrapper').prepend(response);
  375. $('#add_sthlm_gallery_button').colorbox.close();
  376. });
  377. return false;
  378. });
  379. /**
  380. *
  381. * DELETE GALLERY
  382. *
  383. *
  384. $('.remove-sthlm-gallery').live('dblclick', function(){
  385. var $this = $(this),
  386. $gallery = $this.parent(),
  387. dataID = $gallery.attr('data-id');
  388. // set post data
  389. var data = {
  390. action: 'sthlm_gallery_ajax_delete',
  391. id: dataID
  392. };
  393. // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  394. $.post(ajaxurl, data, function(response) {
  395. if(response === 'error'){
  396. alert('Please try again!');
  397. }else{
  398. // remove the object
  399. $gallery.remove();
  400. }
  401. });
  402. return false;
  403. });
  404. /*
  405. *
  406. * FILTER AND SUCH
  407. *
  408. *
  409. $('#sthlm-gallery-datepicker select, #sthlm-gallery-filter input').change( function(){
  410. var data = {
  411. query: $(this).val(),
  412. context: $(this).attr('id'),
  413. action: 'sthlm_gallery_query_attachments'
  414. }
  415. $.get(ajaxurl, data, function(data) {
  416. $allImagesWrapp.html(data);
  417. sthlmDragDrop();
  418. editImageData();
  419. });
  420. });
  421. /*
  422. *
  423. * Show and hide
  424. *
  425. *
  426. $('.sthlm-thumb').live('mouseover mouseout', function(event) {
  427. if (event.type == 'mouseover') {
  428. $('.sthlm-thumb-data', this).show();
  429. } else {
  430. $('.sthlm-thumb-data', this).hide();
  431. }
  432. });
  433. /*
  434. *
  435. * Edit Image data
  436. *
  437. *
  438. var editImageData = function(){
  439. // Open Cbox to edit image data
  440. $('.sthlm-gallery-edit-image-data').live('click', function(){
  441. // save for performance and this as value futher down
  442. var $this = $(this);
  443. $.fn.colorbox({inline:true, href: $this.attr('href')}, function(){
  444. // Populate fields in the callback
  445. $('#sthlm_edit_preview').attr('src', $this.parent().parent().find('img').attr('src'));
  446. $('input[name="sthlm-gallery-id"]').val($this.siblings('.sthlm-thumb-id').text());
  447. $('input[name="sthlm-gallery-title"]').val($this.siblings('.sthlm-thumb-title').text());
  448. $('textarea[name="sthlm-gallery-excerpt"]').val($this.siblings('.sthlm-thumb-excerpt').text());
  449. $('textarea[name="sthlm-gallery-content"]').val($this.siblings('.sthlm-thumb-content').text());
  450. });
  451. });
  452. // Prevent manual submit
  453. $('#sthlm-gallery-image-data').submit(function(){ return false; });
  454. //add gallery
  455. $('#submit-sthlm-gallery-image-data').click(function(){
  456. // set post data
  457. var data = {
  458. action: 'sthlm_gallery_edit_image_data',
  459. id: $('input[name="sthlm-gallery-id"]').val(),
  460. title: $('input[name="sthlm-gallery-title"]').val(),
  461. excerpt: $('textarea[name="sthlm-gallery-excerpt"]').val(),
  462. content: $('textarea[name="sthlm-gallery-content"]').val()
  463. };
  464. // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
  465. $.post(ajaxurl, data, function(response) {
  466. //console.log(response)
  467. //$('#gallery_dir_wrapper').prepend(response);
  468. $('#add_sthlm_gallery_button').colorbox.close();
  469. });
  470. return false;
  471. });
  472. }
  473. editImageData();
  474. */
  475. });