PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/js/jquery.inlineEdit.js

https://gitlab.com/blackhawkelectronics/enventory
JavaScript | 304 lines | 235 code | 36 blank | 33 comment | 46 complexity | 0fb291b8e5e228a038a45e0afcd56c15 MD5 | raw file
  1. /**
  2. * jQuery Inline Edit
  3. * Copyright (c) 2009 Markus Hedlund, Mimmin AB
  4. * Version 1.0
  5. * Licensed under MIT license
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://labs.mimmin.com/inlineedit
  8. *
  9. *
  10. * Adds inline edit to html elements with classes editableSingle and editableMulti.
  11. * Elements must have class to identify type of data and id.
  12. * Types are linked to urls
  13. *
  14. * Example:
  15. * <li class="editableSingle categoryName id3">
  16. *
  17. * Javascript:
  18. * $.inlineEdit({categoryName: 'category/edit/id/'});
  19. *
  20. *
  21. * Or:
  22. * <td class="editableSingle videoTitle id3"></td>
  23. * <td class="editableMulti videoDescription id3"></td>
  24. *
  25. * Javascript:
  26. * $.inlineEdit({
  27. * videoTitle: '/video/edit/title/',
  28. * videoDescription: '/video/edit/description/'
  29. * });
  30. */
  31. (function($){
  32. $.inlineEdit = function(urls, options){
  33. var editableUrls = urls;
  34. var options = jQuery.extend({
  35. afterSave: function(){},
  36. afterRemove: function(){},
  37. getId: getId,
  38. filterElementValue: function($o){return $o.html();},
  39. animate: true,
  40. colors: {
  41. success: 'green',
  42. error: 'red'/*,
  43. standard: '#000'*/
  44. }
  45. }, options);
  46. var initialValues = {};
  47. var editableFields = false;
  48. var linkClicked = false;
  49. if ($('.editableSingle, .editableMulti').length > 0) {
  50. var simpleMode = $('.editableSingle, .editableMulti')[0].tagName.toLowerCase() != 'td';
  51. options.colors.standard = $('.editableSingle, .editableMulti').eq(0).css('color');
  52. }
  53. $('.editableSingle').click(function(){
  54. if (linkClicked) {
  55. linkClicked = false;
  56. return;
  57. }
  58. if (!editableFields || $('.editField').length < editableFields) {
  59. var value = options.filterElementValue($(this));
  60. saveInitialValue($(this));
  61. $(this).addClass('isEditing').css('color', options.colors.standard).stop();
  62. if ($('.editFieldFirst').length == 0) {
  63. editableFields = $(this).siblings('.editableSingle, .editableMulti').length + 1;
  64. $(this).html('<div class="editFieldWrapper"><input type="text" value="' + value + '" class="editField editFieldFirst" /></div>');
  65. if (!simpleMode) {
  66. $(this).siblings('.editableSingle, .editableMulti').click();
  67. } else {
  68. editableFields = 1;
  69. }
  70. addSaveControllers(function(){
  71. $('.editFieldFirst').focus();
  72. });
  73. } else {
  74. $(this).html('<div class="editFieldWrapper"><input type="text" value="' + value + '" class="editField" /></div>');
  75. }
  76. }
  77. });
  78. $('.editableMulti').click(function(){
  79. if (linkClicked) {
  80. linkClicked = false;
  81. return false;
  82. }
  83. if (!editableFields || $('.editField').length < editableFields) {
  84. var value = options.filterElementValue($(this));
  85. saveInitialValue($(this));
  86. $(this).addClass('isEditing').css('color', options.colors.standard).stop();
  87. if ($('.editFieldFirst').length == 0) {
  88. editableFields = $(this).siblings('.editableSingle, .editableMulti').length + 1;
  89. $(this).html('<div class="editFieldWrapper"><textarea class="editField editFieldFirst">' + value + '</textarea></div>');
  90. if (!simpleMode) {
  91. $(this).siblings('.editableSingle, .editableMulti').click();
  92. }
  93. addSaveControllers(function(){
  94. $('.editFieldFirst').focus();
  95. });
  96. } else {
  97. $(this).html('<div class="editFieldWrapper"><textarea class="editField">' + value + '</textarea></div>');
  98. }
  99. }
  100. });
  101. $('.editableSingle a, .editableMulti a').click(function(){
  102. linkClicked = true;
  103. });
  104. function addSaveControllers(callback)
  105. {
  106. if ($('.editFieldWrapper:last').parent().hasClass('removable')) {
  107. $('.editFieldWrapper:last').append('<div class="editFieldSaveControllers"><button>Save</button>' +
  108. ', <a href="javascript:;" class="editFieldRemove">Remove</a> or ' +
  109. '<a href="javascript:;" class="editFieldCancel">Cancel</a></div>');
  110. } else {
  111. $('.editFieldWrapper:last').append('<div class="editFieldSaveControllers"><button>Save</button> or ' +
  112. '<a href="javascript:;" class="editFieldCancel">Cancel</a></div>');
  113. }
  114. $('.editFieldSaveControllers > button').click(editSave);
  115. $('.editFieldSaveControllers > a.editFieldCancel').click(editCancel);
  116. $('.editFieldSaveControllers > a.editFieldRemove').click(editRemove);
  117. $('input.editField').keydown(function(e){
  118. if (e.keyCode == 13) {
  119. // Enter
  120. editSave();
  121. } else if (e.keyCode == 27) {
  122. // Escape
  123. editCancel();
  124. }
  125. });
  126. if (options.animate) {
  127. $('.editFieldWrapper').slideDown(500, callback);
  128. } else {
  129. $('.editFieldWrapper').show();
  130. callback();
  131. }
  132. }
  133. function editCancel(e)
  134. {
  135. linkClicked = typeof(e) != 'undefined'; // If e is set, call comes from mouse click
  136. $('.editField').each(function(){
  137. var $td = $(this).parents('.editableSingle, .editableMulti');
  138. removeEditField($td, getInitialValue($td), false);
  139. });
  140. }
  141. function editRemove()
  142. {
  143. linkClicked = true;
  144. if (!confirm('Are you sure that you want to remove this?')) {
  145. return false;
  146. }
  147. $('.editFieldSaveControllers > button, .editField').attr('disabled', 'disabled').html('Removing...');
  148. var $td = $('.editField').eq(0).parents('.editableSingle, .editableMulti');
  149. var url = editableUrls.remove;
  150. var id = options.getId($td);
  151. $.ajax({
  152. url: url + id,
  153. type: 'POST',
  154. success: function(msg){
  155. $('.editField').each(function(){
  156. var $td = $(this).parents('.editableSingle, .editableMulti');
  157. if (msg == 1) {
  158. if (options.animate) {
  159. $td.slideUp(500, function(){
  160. $(this).remove();
  161. });
  162. } else {
  163. $td.remove();
  164. }
  165. } else {
  166. removeEditField($td, getInitialValue($td), false, options.colors.error);
  167. }
  168. });
  169. options.afterRemove({
  170. success: msg == 1,
  171. id: id
  172. });
  173. },
  174. error: function(){
  175. $('.editField').each(function(){
  176. var $td = $(this).parents('.editableSingle, .editableMulti');
  177. removeEditField($td, getInitialValue($td), false, options.colors.error);
  178. });
  179. }
  180. });
  181. }
  182. function removeEditField($td, value, animateColor, fromColor)
  183. {
  184. var f = function(){
  185. $td.html(value).removeClass('isEditing');
  186. if (animateColor) {
  187. $td.css('color', fromColor)/*.animate({color: options.colors.standard},5000)*/;
  188. setTimeout(function(){
  189. $td.css('color', options.colors.standard);
  190. }, 5000);
  191. } else if (typeof(fromColor) != 'undefined') {
  192. $td.css('color', fromColor);
  193. }
  194. };
  195. if (options.animate) {
  196. $td.children('.editFieldWrapper').slideUp(500, f);
  197. } else {
  198. $td.children('.editFieldWrapper').hide();
  199. f();
  200. }
  201. }
  202. function saveInitialValue($td)
  203. {
  204. var index = options.getId($td) + getTypeAndUrl($td).type;
  205. initialValues[index] = $td.html();
  206. }
  207. function getInitialValue($td)
  208. {
  209. var index = options.getId($td) + getTypeAndUrl($td).type;
  210. return initialValues[index];
  211. }
  212. function getId($td)
  213. {
  214. var id;
  215. $.each($td.attr('class').split(' '), function(index, name){
  216. if (name.match(/^id[0-9]*$/)) {
  217. id = name.match(/^id([0-9]*)$/)[1];
  218. return false;
  219. }
  220. });
  221. return id;
  222. }
  223. function getTypeAndUrl($td)
  224. {
  225. var typeAndUrl;
  226. $.each(editableUrls, function(index, name){
  227. if ($td.hasClass(index)) {
  228. typeAndUrl = {type: index, url: name};
  229. return false;
  230. }
  231. });
  232. return typeAndUrl;
  233. }
  234. function editSave()
  235. {
  236. $('.editFieldSaveControllers > button, .editField').attr('disabled', 'disabled');
  237. $('.editField').each(function(){
  238. var $td = $(this).parents('.editableSingle, .editableMulti');
  239. var typeAndUrl = getTypeAndUrl($td);
  240. var url = typeAndUrl.url; // Get save URL
  241. var id = options.getId($td);
  242. var value = $(this).val();
  243. var color = options.colors.standard;
  244. $.ajax({
  245. url: url + id,
  246. data: {data: value},
  247. type: 'POST',
  248. success: function(msg){
  249. if (msg == 1) {
  250. removeEditField($td, value, true, options.colors.success);
  251. } else {
  252. removeEditField($td, value, false, options.colors.error);
  253. }
  254. options.afterSave({
  255. success: msg == 1,
  256. type: typeAndUrl.type,
  257. id: id,
  258. value: value
  259. });
  260. },
  261. error: function(){
  262. removeEditField($td, value, false, options.colors.error);
  263. }
  264. });
  265. });
  266. }
  267. };
  268. })(jQuery);