PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/js/model.js

http://lazycms.googlecode.com/
JavaScript | 682 lines | 463 code | 40 blank | 179 comment | 94 complexity | 88b6970edec6edfd4112d83e606e0322 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. /**
  2. * +---------------------------------------------------------------------------+
  3. * | LL LLLL LL L LLLL LLLL |
  4. * | LL LL L LLL LL LL L LL LL |
  5. * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
  6. * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
  7. * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
  8. * | LL LL LL LL LLLL LL L LL LL LLLL LL |
  9. * | LL LL LL LL LL LL L L LL L LL LLLL LL |
  10. * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
  11. * | LL |
  12. * | LL |
  13. * +---------------------------------------------------------------------------+
  14. * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
  15. * +---------------------------------------------------------------------------+
  16. * | LazyCMS is free software. See LICENSE for copyright notices and details. |
  17. * +---------------------------------------------------------------------------+
  18. */
  19. function model_list_init() {
  20. // ??????
  21. $('#modellist').actions();
  22. }
  23. // ????
  24. function model_delete(modelid){
  25. LazyCMS.confirm(_('Confirm Delete?'),function(r){
  26. if (r) {
  27. LazyCMS.postAction('model.php', {method:'bulk', action:'delete'}, modelid);
  28. }
  29. });
  30. }
  31. // ????????
  32. function model_state(action,modelid){
  33. return LazyCMS.postAction('model.php', {method:'bulk', action:action}, modelid);
  34. }
  35. // ????????
  36. function model_manage_init() {
  37. var wrap = $('#fields'),
  38. // ????
  39. table_drag = function(){
  40. $('table.data-table',wrap).tableDnD({
  41. onDragClass: 'Drag'
  42. }).find('tr').hover(function(){
  43. $(this).addClass('Over');
  44. },function(){
  45. $(this).removeClass('Over');
  46. });
  47. },
  48. // ????
  49. field_empty = function(){
  50. if ($('tbody tr',wrap).size()==0) {
  51. $('tbody',wrap).append('<tr class="empty"><td colspan="5" class="tc">' + _('No record!') + '</td></tr>');
  52. }
  53. },
  54. // ????
  55. actions = function(){
  56. $('tbody tr',wrap).hover(function(){
  57. $('.row-actions',this).css({'visibility': 'visible'});
  58. },function(){
  59. $('.row-actions',this).css({'visibility': 'hidden'});
  60. });
  61. // ??????
  62. $('span.edit a,strong.edit a',wrap).click(function(){
  63. var id = this.href.replace(self.location,'').replace('#',''),
  64. field = $('#field-index-' + id + ' textarea[name^=field]').val();
  65. model_field_manage(id,LazyCMS.parse_str(field));
  66. return false;
  67. });
  68. // ??????
  69. $('span.delete a',wrap).click(function(){
  70. var id = this.href.replace(self.location,'').replace('#','');
  71. LazyCMS.confirm(_('Confirm Delete?'),function(r){
  72. if (r) {
  73. model_field_delete(id,field_empty);
  74. }
  75. });
  76. return false;
  77. });
  78. },
  79. // ??????
  80. set_rules = function(type){
  81. var v = $('#field_v');
  82. var s = $('#field_sv').val();
  83. if (type=='+') {
  84. v.val(v.val() + s + ';\n');
  85. } else if (type=='-') {
  86. v.val(v.val().replace( s +';\n',''));
  87. }
  88. v.scrollTop(2000);
  89. },
  90. // ??????
  91. switch_type = function(type) {
  92. var wrap = $('form#modelmanage');
  93. switch (type) {
  94. case 'Category':
  95. $('#listtemplate', wrap).parents('tr').show();
  96. $('#pagetemplate,#path', wrap).parents('tr').hide();
  97. break;
  98. case 'Post': default:
  99. $('#listtemplate', wrap).parents('tr').hide();
  100. $('#pagetemplate,#path', wrap).parents('tr').show();
  101. break;
  102. }
  103. };
  104. // ??????
  105. $('#modelmanage input[name=type]').click(function(){
  106. switch_type(this.value);
  107. });
  108. // ??????
  109. switch_type($('#modelmanage input[name=type]:checked').val());
  110. // ????????
  111. if ($('tbody tr.empty',wrap).is('tr')===false) {
  112. table_drag(); actions();
  113. }
  114. // ??????
  115. $('#modelmanage div.rules > a').click(function(){
  116. var val = this.href.replace(self.location,'').replace('#','');
  117. $('#modelmanage input[name=path]').insertVal(val); return false;
  118. });
  119. // ??????
  120. $('#fields').each(function(i){
  121. var fieldset = $(this);
  122. $('a.toggle,h3',this).click(function(){
  123. fieldset.toggleClass('closed');
  124. });
  125. });
  126. // ??????
  127. $('input[name=select]',wrap).click(function(){
  128. $('input[name^=list]:checkbox,input[name=select]:checkbox',wrap).attr('checked',this.checked);
  129. });
  130. // ??????
  131. $('button.delete',wrap).click(function(){
  132. LazyCMS.confirm(_('Confirm Delete?'),function(r){
  133. if (r) {
  134. $('input:checkbox[name^=listids]:checked',wrap).each(function(){
  135. model_field_delete(this.value,field_empty);
  136. });
  137. }
  138. });
  139. });
  140. // ????
  141. $('form#modelmanage').ajaxSubmit();
  142. // ??????
  143. $('button.addnew',wrap).click(function(){
  144. model_field_manage();
  145. });
  146. // ??????
  147. arguments.callee.wrap = wrap;
  148. arguments.callee.actions = actions;
  149. arguments.callee.table_drag = table_drag;
  150. arguments.callee.set_rules = set_rules;
  151. }
  152. // ????
  153. function model_field_manage(id,params) {
  154. var wrap = model_manage_init.wrap, actions = model_manage_init.actions, table_drag = model_manage_init.table_drag, set_rules = model_manage_init.set_rules;
  155. id = id || '', params = $.extend(params||{},(params?{id:id}:{}));
  156. $.post(LazyCMS.ADMIN + 'model.php?method=field',params,function(r){
  157. var title = id ? _('Edit') : _('Add New');
  158. LazyCMS.dialog({
  159. name:'field', title:title, styles:{ 'top':-100, 'width':'440px' }, body:r, remove:function() {
  160. LazyCMS.removeDialog('field'); $('#field-index-' + id + ' textarea').removeClass('edit');
  161. }
  162. },function(r){
  163. if ($('form#model-field-table',this).is('form')==false) return ;
  164. $('#field-index-' + id + ' textarea').addClass('edit');
  165. var dialog = this, switch_type = function(type){
  166. $('#field_serialize,#field_toolbar,#field_length,#field_default',dialog).hide();
  167. switch (type) {
  168. case 'input':
  169. $('#field_length,#field_default',dialog).show();
  170. break;
  171. case 'textarea':
  172. $('#field_default',dialog).show();
  173. break;
  174. case 'radio': case 'checkbox': case 'select':
  175. $('#field_serialize,#field_default',dialog).show();
  176. break;
  177. case 'basic': case 'editor':
  178. $('#field_toolbar,#field_default',dialog).show();
  179. break;
  180. case 'date':
  181. $('#field_default',dialog).show();
  182. break;
  183. }
  184. },
  185. // ??????
  186. switch_help = function(){
  187. $('#field_help',dialog).toggle();
  188. },
  189. // ????????
  190. switch_verify = function(){
  191. $('#field_verify',dialog).toggle();
  192. };
  193. switch_type($('#field_t').val());
  194. // ??????
  195. $('#field_is_help').click(switch_help);
  196. if ($('#field_is_help').attr('checked')) {
  197. switch_help();
  198. }
  199. // ??????
  200. $('#field_is_verify').click(switch_verify);
  201. if ($('#field_is_verify').attr('checked')) {
  202. switch_verify();
  203. }
  204. // ?????????
  205. $('#field_verify td a[rule]',dialog).click(function(){
  206. set_rules($(this).attr('rule'));
  207. });
  208. // ????????
  209. $('#field_t').change(function(){
  210. switch_type(this.value);
  211. });
  212. LazyCMS.eselect();
  213. // ??????
  214. $('button[rel=save]',this).click(function(){
  215. var error = [],fields = [], type = $('#field_t').val(), index = 0, selector = 'input[name=l],input[name=n],input[name=so],select[name=t],[name=w]',
  216. label = $.trim($('input[name=l]',dialog).val()), name = $.trim($('input[name=n]',dialog).val()),
  217. mtype = $('form#modelmanage input[name=type]:checked').val();
  218. // ?????????
  219. if (mtype == 'Category') fields = ['path','page','list','model','description'];
  220. // ????
  221. $('.input_error,.textarea_error',dialog).removeClass('input_error').removeClass('textarea_error');
  222. // ?????????
  223. $('td textarea:not(.edit)',wrap).each(function(){
  224. fields.push(LazyCMS.parse_str(this.value).n);
  225. });
  226. // ????
  227. if (label=='') error.push({'id':'l','text':_('The label field is empty.')});
  228. if (name=='') {
  229. error.push({'id':'n','text':_('The name field is empty.')});
  230. } else if ($.inArray(name,fields)!=-1) {
  231. error.push({'id':'n','text':_('The name already exists or system field.')});
  232. }
  233. if (error.length > 0) {
  234. $(dialog).error(error);
  235. return false;
  236. }
  237. $('tbody tr.empty',wrap).remove();
  238. if ($('#field_is_help').attr('checked') == true) {
  239. selector+= ',textarea[name=h]';
  240. }
  241. if ($('#field_is_verify').attr('checked') == true) {
  242. selector+= ',textarea[name=v]';
  243. }
  244. switch (type) {
  245. case 'input':
  246. selector+= ',[name=c],input[name=d]';
  247. break;
  248. case 'textarea':
  249. selector+= ',input[name=d]';
  250. break;
  251. case 'radio': case 'checkbox': case 'select':
  252. selector+= ',textarea[name=s],input[name=d]';
  253. break;
  254. case 'basic': case 'editor':
  255. selector+= ',input[name^=a],input[name=d]';
  256. break;
  257. case 'date':
  258. selector+= ',input[name=d]';
  259. break;
  260. }
  261. var row = function(index) {
  262. var tr = '<tr id="field-index-' + index + '" index="' + index + '">';
  263. tr+= '<td class="check-column"><input type="checkbox" value="' + index + '" name="listids[]"></td>';
  264. tr+= '<td><strong><a href="javascript:;">' + $('input[name=l]',dialog).val() + '</a></strong><br>';
  265. tr+= '<div class="row-actions" style="visibility: hidden;"><span class="edit"><a href="#' + index + '">' + _('Edit') + '</a> | </span><span class="delete"><a href="#' + index + '">' + _('Delete') + '</a></span></div>';
  266. tr+= '<textarea class="hide" name="field[]">' + $(selector,dialog).serialize() + '</textarea></td>';
  267. tr+= '<td>' + $('input[name=n]',dialog).val() + '</td>';
  268. tr+= '<td>' + $('select[name=t]',dialog).val() + '</td>';
  269. tr+= '<td>' + $('input[name=d]',dialog).val() + '</td>';
  270. tr+= '</tr>';
  271. return tr;
  272. };
  273. var id = $('input:hidden[name=id]',dialog).val();
  274. if (id) {
  275. $('#field-index-' + id,wrap).replaceWith(row(id));
  276. } else {
  277. $('tbody tr',wrap).each(function(){ index = Math.max(0,parseInt($(this).attr('index'))); }); index++;
  278. $('tbody',wrap).append(row(index));
  279. }
  280. table_drag(); actions(); LazyCMS.removeDialog('field');
  281. });
  282. });
  283. });
  284. }
  285. // ????
  286. function model_field_delete(id,callback) {
  287. $('#field-index-' + id).remove(); if ($.isFunction(callback)) callback();
  288. }
  289. /**
  290. * TableDnD plug-in for JQuery, allows you to drag and drop table rows
  291. * You can set up various options to control how the system will work
  292. * Copyright (c) Denis Howlett <denish@isocra.com>
  293. * Licensed like jQuery, see http://docs.jquery.com/License.
  294. *
  295. * Configuration options:
  296. *
  297. * onDragStyle
  298. * This is the style that is assigned to the row during drag. There are limitations to the styles that can be
  299. * associated with a row (such as you can't assign a border--well you can, but it won't be
  300. * displayed). (So instead consider using onDragClass.) The CSS style to apply is specified as
  301. * a map (as used in the jQuery css(...) function).
  302. * onDropStyle
  303. * This is the style that is assigned to the row when it is dropped. As for onDragStyle, there are limitations
  304. * to what you can do. Also this replaces the original style, so again consider using onDragClass which
  305. * is simply added and then removed on drop.
  306. * onDragClass
  307. * This class is added for the duration of the drag and then removed when the row is dropped. It is more
  308. * flexible than using onDragStyle since it can be inherited by the row cells and other content. The default
  309. * is class is tDnD_whileDrag. So to use the default, simply customise this CSS class in your
  310. * stylesheet.
  311. * onDrop
  312. * Pass a function that will be called when the row is dropped. The function takes 2 parameters: the table
  313. * and the row that was dropped. You can work out the new order of the rows by using
  314. * table.rows.
  315. * onDragStart
  316. * Pass a function that will be called when the user starts dragging. The function takes 2 parameters: the
  317. * table and the row which the user has started to drag.
  318. * onAllowDrop
  319. * Pass a function that will be called as a row is over another row. If the function returns true, allow
  320. * dropping on that row, otherwise not. The function takes 2 parameters: the dragged row and the row under
  321. * the cursor. It returns a boolean: true allows the drop, false doesn't allow it.
  322. * scrollAmount
  323. * This is the number of pixels to scroll if the user moves the mouse cursor to the top or bottom of the
  324. * window. The page should automatically scroll up or down as appropriate (tested in IE6, IE7, Safari, FF2,
  325. * FF3 beta
  326. * dragHandle
  327. * This is the name of a class that you assign to one or more cells in each row that is draggable. If you
  328. * specify this class, then you are responsible for setting cursor: move in the CSS and only these cells
  329. * will have the drag behaviour. If you do not specify a dragHandle, then you get the old behaviour where
  330. * the whole row is draggable.
  331. *
  332. * Other ways to control behaviour:
  333. *
  334. * Add class="nodrop" to any rows for which you don't want to allow dropping, and class="nodrag" to any rows
  335. * that you don't want to be draggable.
  336. *
  337. * Inside the onDrop method you can also call $.tableDnD.serialize() this returns a string of the form
  338. * <tableID>[]=<rowID1>&<tableID>[]=<rowID2> so that you can send this back to the server. The table must have
  339. * an ID as must all the rows.
  340. *
  341. * Other methods:
  342. *
  343. * $("...").tableDnDUpdate()
  344. * Will update all the matching tables, that is it will reapply the mousedown method to the rows (or handle cells).
  345. * This is useful if you have updated the table rows using Ajax and you want to make the table draggable again.
  346. * The table maintains the original configuration (so you don't have to specify it again).
  347. *
  348. * $("...").tableDnDSerialize()
  349. * Will serialize and return the serialized string as above, but for each of the matching tables--so it can be
  350. * called from anywhere and isn't dependent on the currentTable being set up correctly before calling
  351. *
  352. * Known problems:
  353. * - Auto-scoll has some problems with IE7 (it scrolls even when it shouldn't), work-around: set scrollAmount to 0
  354. *
  355. * Version 0.2: 2008-02-20 First public version
  356. * Version 0.3: 2008-02-07 Added onDragStart option
  357. * Made the scroll amount configurable (default is 5 as before)
  358. * Version 0.4: 2008-03-15 Changed the noDrag/noDrop attributes to nodrag/nodrop classes
  359. * Added onAllowDrop to control dropping
  360. * Fixed a bug which meant that you couldn't set the scroll amount in both directions
  361. * Added serialize method
  362. * Version 0.5: 2008-05-16 Changed so that if you specify a dragHandle class it doesn't make the whole row
  363. * draggable
  364. * Improved the serialize method to use a default (and settable) regular expression.
  365. * Added tableDnDupate() and tableDnDSerialize() to be called when you are outside the table
  366. */
  367. jQuery.tableDnD = {
  368. /** Keep hold of the current table being dragged */
  369. currentTable : null,
  370. /** Keep hold of the current drag object if any */
  371. dragObject: null,
  372. /** The current mouse offset */
  373. mouseOffset: null,
  374. /** Remember the old value of Y so that we don't do too much processing */
  375. oldY: 0,
  376. /** Actually build the structure */
  377. build: function(options) {
  378. // Set up the defaults if any
  379. this.each(function() {
  380. // This is bound to each matching table, set up the defaults and override with user options
  381. this.tableDnDConfig = jQuery.extend({
  382. onDragStyle: null,
  383. onDropStyle: null,
  384. // Add in the default class for whileDragging
  385. onDragClass: "tDnD_whileDrag",
  386. onDrop: null,
  387. onDragStart: null,
  388. scrollAmount: 5,
  389. serializeRegexp: /[^\-]*$/, // The regular expression to use to trim row IDs
  390. serializeParamName: null, // If you want to specify another parameter name instead of the table ID
  391. dragHandle: null // If you give the name of a class here, then only Cells with this class will be draggable
  392. }, options || {});
  393. // Now make the rows draggable
  394. jQuery.tableDnD.makeDraggable(this);
  395. });
  396. // Now we need to capture the mouse up and mouse move event
  397. // We can use bind so that we don't interfere with other event handlers
  398. jQuery(document)
  399. .bind('mousemove', jQuery.tableDnD.mousemove)
  400. .bind('mouseup', jQuery.tableDnD.mouseup);
  401. // Don't break the chain
  402. return this;
  403. },
  404. /** This function makes all the rows on the table draggable apart from those marked as "NoDrag" */
  405. makeDraggable: function(table) {
  406. var config = table.tableDnDConfig;
  407. if (table.tableDnDConfig.dragHandle) {
  408. // We only need to add the event to the specified cells
  409. var cells = jQuery("td."+table.tableDnDConfig.dragHandle, table);
  410. cells.each(function() {
  411. // The cell is bound to "this"
  412. jQuery(this).mousedown(function(ev) {
  413. jQuery.tableDnD.dragObject = this.parentNode;
  414. jQuery.tableDnD.currentTable = table;
  415. jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
  416. if (config.onDragStart) {
  417. // Call the onDrop method if there is one
  418. config.onDragStart(table, this);
  419. }
  420. return false;
  421. });
  422. })
  423. } else {
  424. // For backwards compatibility, we add the event to the whole row
  425. var rows = jQuery("tr", table); // get all the rows as a wrapped set
  426. rows.each(function() {
  427. // Iterate through each row, the row is bound to "this"
  428. var row = jQuery(this);
  429. if (! row.hasClass("nodrag")) {
  430. row.mousedown(function(ev) {
  431. if (ev.target.tagName == "TD") {
  432. jQuery.tableDnD.dragObject = this;
  433. jQuery.tableDnD.currentTable = table;
  434. jQuery.tableDnD.mouseOffset = jQuery.tableDnD.getMouseOffset(this, ev);
  435. if (config.onDragStart) {
  436. // Call the onDrop method if there is one
  437. config.onDragStart(table, this);
  438. }
  439. return false;
  440. }
  441. }).css("cursor", "move"); // Store the tableDnD object
  442. }
  443. });
  444. }
  445. },
  446. updateTables: function() {
  447. this.each(function() {
  448. // this is now bound to each matching table
  449. if (this.tableDnDConfig) {
  450. jQuery.tableDnD.makeDraggable(this);
  451. }
  452. })
  453. },
  454. /** Get the mouse coordinates from the event (allowing for browser differences) */
  455. mouseCoords: function(ev){
  456. if(ev.pageX || ev.pageY){
  457. return {x:ev.pageX, y:ev.pageY};
  458. }
  459. return {
  460. x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
  461. y:ev.clientY + document.body.scrollTop - document.body.clientTop
  462. };
  463. },
  464. /** Given a target element and a mouse event, get the mouse offset from that element.
  465. To do this we need the element's position and the mouse position */
  466. getMouseOffset: function(target, ev) {
  467. ev = ev || window.event;
  468. var docPos = this.getPosition(target);
  469. var mousePos = this.mouseCoords(ev);
  470. return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
  471. },
  472. /** Get the position of an element by going up the DOM tree and adding up all the offsets */
  473. getPosition: function(e){
  474. var left = 0;
  475. var top = 0;
  476. /** Safari fix -- thanks to Luis Chato for this! */
  477. if (e.offsetHeight == 0) {
  478. /** Safari 2 doesn't correctly grab the offsetTop of a table row
  479. this is detailed here:
  480. http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/
  481. the solution is likewise noted there, grab the offset of a table cell in the row - the firstChild.
  482. note that firefox will return a text node as a first child, so designing a more thorough
  483. solution may need to take that into account, for now this seems to work in firefox, safari, ie */
  484. e = e.firstChild; // a table cell
  485. }
  486. while (e.offsetParent){
  487. left += e.offsetLeft;
  488. top += e.offsetTop;
  489. e = e.offsetParent;
  490. }
  491. left += e.offsetLeft;
  492. top += e.offsetTop;
  493. return {x:left, y:top};
  494. },
  495. mousemove: function(ev) {
  496. if (jQuery.tableDnD.dragObject == null) {
  497. return;
  498. }
  499. var dragObj = jQuery(jQuery.tableDnD.dragObject);
  500. var config = jQuery.tableDnD.currentTable.tableDnDConfig;
  501. var mousePos = jQuery.tableDnD.mouseCoords(ev);
  502. var y = mousePos.y - jQuery.tableDnD.mouseOffset.y;
  503. //auto scroll the window
  504. var yOffset = window.pageYOffset;
  505. if (document.all) {
  506. // Windows version
  507. //yOffset=document.body.scrollTop;
  508. if (typeof document.compatMode != 'undefined' &&
  509. document.compatMode != 'BackCompat') {
  510. yOffset = document.documentElement.scrollTop;
  511. }
  512. else if (typeof document.body != 'undefined') {
  513. yOffset=document.body.scrollTop;
  514. }
  515. }
  516. if (mousePos.y-yOffset < config.scrollAmount) {
  517. window.scrollBy(0, -config.scrollAmount);
  518. } else {
  519. var windowHeight = window.innerHeight ? window.innerHeight
  520. : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
  521. if (windowHeight-(mousePos.y-yOffset) < config.scrollAmount) {
  522. window.scrollBy(0, config.scrollAmount);
  523. }
  524. }
  525. if (y != jQuery.tableDnD.oldY) {
  526. // work out if we're going up or down...
  527. var movingDown = y > jQuery.tableDnD.oldY;
  528. // update the old value
  529. jQuery.tableDnD.oldY = y;
  530. // update the style to show we're dragging
  531. if (config.onDragClass) {
  532. dragObj.addClass(config.onDragClass);
  533. } else {
  534. dragObj.css(config.onDragStyle);
  535. }
  536. // If we're over a row then move the dragged row to there so that the user sees the
  537. // effect dynamically
  538. var currentRow = jQuery.tableDnD.findDropTargetRow(dragObj, y);
  539. if (currentRow) {
  540. // TODO worry about what happens when there are multiple TBODIES
  541. if (movingDown && jQuery.tableDnD.dragObject != currentRow) {
  542. jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow.nextSibling);
  543. } else if (! movingDown && jQuery.tableDnD.dragObject != currentRow) {
  544. jQuery.tableDnD.dragObject.parentNode.insertBefore(jQuery.tableDnD.dragObject, currentRow);
  545. }
  546. }
  547. }
  548. return false;
  549. },
  550. /** We're only worried about the y position really, because we can only move rows up and down */
  551. findDropTargetRow: function(draggedRow, y) {
  552. var rows = jQuery.tableDnD.currentTable.rows;
  553. for (var i=0; i<rows.length; i++) {
  554. var row = rows[i];
  555. var rowY = this.getPosition(row).y;
  556. var rowHeight = parseInt(row.offsetHeight)/2;
  557. if (row.offsetHeight == 0) {
  558. rowY = this.getPosition(row.firstChild).y;
  559. rowHeight = parseInt(row.firstChild.offsetHeight)/2;
  560. }
  561. // Because we always have to insert before, we need to offset the height a bit
  562. if ((y > rowY - rowHeight) && (y < (rowY + rowHeight))) {
  563. // that's the row we're over
  564. // If it's the same as the current row, ignore it
  565. if (row == draggedRow) {return null;}
  566. var config = jQuery.tableDnD.currentTable.tableDnDConfig;
  567. if (config.onAllowDrop) {
  568. if (config.onAllowDrop(draggedRow, row)) {
  569. return row;
  570. } else {
  571. return null;
  572. }
  573. } else {
  574. // If a row has nodrop class, then don't allow dropping (inspired by John Tarr and Famic)
  575. var nodrop = jQuery(row).hasClass("nodrop");
  576. if (! nodrop) {
  577. return row;
  578. } else {
  579. return null;
  580. }
  581. }
  582. return row;
  583. }
  584. }
  585. return null;
  586. },
  587. mouseup: function(e) {
  588. if (jQuery.tableDnD.currentTable && jQuery.tableDnD.dragObject) {
  589. var droppedRow = jQuery.tableDnD.dragObject;
  590. var config = jQuery.tableDnD.currentTable.tableDnDConfig;
  591. // If we have a dragObject, then we need to release it,
  592. // The row will already have been moved to the right place so we just reset stuff
  593. if (config.onDragClass) {
  594. jQuery(droppedRow).removeClass(config.onDragClass);
  595. } else {
  596. jQuery(droppedRow).css(config.onDropStyle);
  597. }
  598. jQuery.tableDnD.dragObject = null;
  599. if (config.onDrop) {
  600. // Call the onDrop method if there is one
  601. config.onDrop(jQuery.tableDnD.currentTable, droppedRow);
  602. }
  603. jQuery.tableDnD.currentTable = null; // let go of the table too
  604. }
  605. },
  606. serialize: function() {
  607. if (jQuery.tableDnD.currentTable) {
  608. return jQuery.tableDnD.serializeTable(jQuery.tableDnD.currentTable);
  609. } else {
  610. return "Error: No Table id set, you need to set an id on your table and every row";
  611. }
  612. },
  613. serializeTable: function(table) {
  614. var result = "";
  615. var tableId = table.id;
  616. var rows = table.rows;
  617. for (var i=0; i<rows.length; i++) {
  618. if (result.length > 0) result += "&";
  619. var rowId = rows[i].id;
  620. if (rowId && rowId && table.tableDnDConfig && table.tableDnDConfig.serializeRegexp) {
  621. rowId = rowId.match(table.tableDnDConfig.serializeRegexp)[0];
  622. }
  623. result += tableId + '[]=' + rowId;
  624. }
  625. return result;
  626. },
  627. serializeTables: function() {
  628. var result = "";
  629. this.each(function() {
  630. // this is now bound to each matching table
  631. result += jQuery.tableDnD.serializeTable(this);
  632. });
  633. return result;
  634. }
  635. }
  636. jQuery.fn.extend(
  637. {
  638. tableDnD : jQuery.tableDnD.build,
  639. tableDnDUpdate : jQuery.tableDnD.updateTables,
  640. tableDnDSerialize: jQuery.tableDnD.serializeTables
  641. }
  642. );