[ Index ]
 

Code source de Typo3 4.1.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/t3lib/ -> jsfunc.inline.js (source)

   1  /*<![CDATA[*/
   2  
   3  /***************************************************************
   4  *  Inline-Relational-Record Editing
   5  *
   6  * $Id: jsfunc.inline.js 2422 2007-07-16 13:09:38Z ohader $
   7  *
   8  *
   9  *
  10  *  Copyright notice
  11  *
  12  *  (c) 2006-2007 Oliver Hader <oh@inpublica.de>
  13  *  All rights reserved
  14  *
  15  *  This script is part of the TYPO3 project. The TYPO3 project is
  16  *  free software; you can redistribute it and/or modify
  17  *  it under the terms of the GNU General Public License as published by
  18  *  the Free Software Foundation; either version 2 of the License, or
  19  *  (at your option) any later version.
  20  *
  21  *  The GNU General Public License can be found at
  22  *  http://www.gnu.org/copyleft/gpl.html.
  23  *
  24  *  This script is distributed in the hope that it will be useful,
  25  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  26  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27  *  GNU General Public License for more details.
  28  *
  29  *  This copyright notice MUST APPEAR in all copies of the script!
  30  ***************************************************************/
  31  
  32  var inline = {
  33      prependFormFieldNames: 'data',
  34      noTitleString: '[No title]',
  35      lockedAjaxMethod: {},
  36      data: {},
  37  
  38      addToDataArray: function(object) { for (var i in object) { this.data[i] = $H(this.data[i]).merge(object[i]); } },
  39      setPrependFormFieldNames: function(value) {    this.prependFormFieldNames = value; },
  40      setNoTitleString: function(value) { this.noTitleString = value; },
  41  
  42      expandCollapseRecord: function(objectId, expandSingle) {
  43          var currentUid = this.parseFormElementName('none', objectId, 1);
  44          var objectPrefix = this.parseFormElementName('full', objectId, 0, 1);
  45  
  46          var currentState = '';
  47          var collapse = new Array();
  48          var expand = new Array();
  49  
  50              // if only a single record should be visibly for that set of records
  51              // and the record clicked itself is no visible, collapse all others
  52          if (expandSingle && !Element.visible(objectId+'_fields'))
  53              collapse = this.collapseAllRecords(objectId, objectPrefix, currentUid);
  54  
  55          Element.toggle(objectId+'_fields');
  56          currentState = Element.visible(objectId+'_fields') ? 1 : 0
  57  
  58          if (this.isNewRecord(objectId))
  59              this.updateExpandedCollapsedStateLocally(objectId, currentState);
  60          else if (currentState)
  61              expand.push(currentUid);
  62          else if (!currentState)
  63              collapse.push(currentUid);
  64  
  65          this.setExpandedCollapsedState(objectId, expand.join(','), collapse.join(','));
  66  
  67          return false;
  68      },
  69  
  70      collapseAllRecords: function(objectId, objectPrefix, callingUid) {
  71              // get the form field, where all records are stored
  72          var objectName = this.prependFormFieldNames+this.parseFormElementName('parts', objectId, 3, 2);
  73          var formObj = document.getElementsByName(objectName);
  74          var collapse = [];
  75  
  76          if (formObj.length) {
  77                  // the uid of the calling object (last part in objectId)
  78              var recObjectId = '';
  79  
  80              var records = formObj[0].value.split(',');
  81              for (var i=0; i<records.length; i++) {
  82                  recObjectId = objectPrefix+'['+records[i]+']';
  83                  if (records[i] != callingUid && Element.visible(recObjectId+'_fields')) {
  84                      Element.hide(recObjectId+'_fields');
  85                      if (this.isNewRecord(recObjectId)) this.updateExpandedCollapsedStateLocally(recObjectId, 0);
  86                      else collapse.push(records[i]);
  87                  }
  88              }
  89          }
  90  
  91          return collapse;
  92      },
  93  
  94      updateExpandedCollapsedStateLocally: function(objectId, value) {
  95          var ucName = 'uc'+this.parseFormElementName('parts', objectId, 3, 2);
  96          var ucFormObj = document.getElementsByName(ucName);
  97          if (ucFormObj.length) ucFormObj[0].value = value;
  98      },
  99  
 100      createNewRecord: function(objectId,prevRecordUid) {
 101          if (this.isBelowMax(objectId)) {
 102              if (prevRecordUid) {
 103                  objectId += '['+prevRecordUid+']';
 104              }
 105              this.makeAjaxCall('createNewRecord', [this.getNumberOfRTE(), objectId], true);
 106          } else {
 107              alert('There are no more relations possible at this moment!');
 108          }
 109          return false;
 110      },
 111  
 112      setExpandedCollapsedState: function(objectId, expand, collapse) {
 113          this.makeAjaxCall('setExpandedCollapsedState', [objectId, expand, collapse]);
 114      },
 115  
 116      makeAjaxCall: function(method, params, lock) {
 117          var max, url='', urlParams='', options={};
 118          if (method && params && params.length && this.lockAjaxMethod(method, lock)) {
 119              url = 'alt_doc_ajax.php';
 120              urlParams += '&ajax[0]='+method;
 121              for (var i=0, max=params.length; i<max; i++) {
 122                  urlParams += '&ajax['+(i+1)+']='+params[i];
 123              }
 124              options = {
 125                  method:        'post',
 126                  parameters:    urlParams,
 127                  onSuccess:    function(xhr) { inline.processAjaxResponse(method, xhr); },
 128                  onFailure:    function(xhr) { inline.showAjaxFailure(method, xhr); }
 129              };
 130  
 131              new Ajax.Request(url, options);
 132          }
 133      },
 134  
 135      lockAjaxMethod: function(method, lock) {
 136          if (!lock || !inline.lockedAjaxMethod[method]) {
 137              inline.lockedAjaxMethod[method] = true;
 138              return true;
 139          } else {
 140              return false;
 141          }
 142      },
 143  
 144      unlockAjaxMethod: function(method) {
 145          inline.lockedAjaxMethod[method] = false;
 146      },
 147  
 148      processAjaxResponse: function(method, xhr) {
 149          inline.unlockAjaxMethod(method);
 150          var json = eval('('+xhr.responseText+')');
 151          for (var i in json.scriptCall) eval(json.scriptCall[i]);
 152      },
 153  
 154      showAjaxFailure: function(method, xhr) {
 155          inline.unlockAjaxMethod(method);
 156          alert('Error: '+xhr.status+"\n"+xhr.statusText);
 157      },
 158  
 159          // foreign_selector: used by selector box (type='select')
 160      importNewRecord: function(objectId) {
 161          var selector = $(objectId+'_selector');
 162          if (selector.selectedIndex != -1) {
 163              var selectedValue = selector.options[selector.selectedIndex].value;
 164              if (!this.data.unique || !this.data.unique[objectId]) {
 165                  selector.options[selector.selectedIndex].selected = false;
 166              }
 167              this.makeAjaxCall('createNewRecord', [this.getNumberOfRTE(), objectId, selectedValue], true);
 168          }
 169          return false;
 170      },
 171  
 172          // foreign_selector: used by element browser (type='group/db')
 173      importElement: function(objectId, table, uid, type) {
 174          window.setTimeout(
 175              function() {
 176                  inline.makeAjaxCall('createNewRecord', [this.getNumberOfRTE(), objectId, uid], true);
 177              },
 178              10
 179          );
 180      },
 181  
 182          // Check uniqueness for element browser:
 183      checkUniqueElement: function(objectId, table, uid, type) {
 184          if (this.checkUniqueUsed(objectId, uid, table)) {
 185              return {passed: false,message: 'There is already a relation to the selected element!'};
 186          } else {
 187              return {passed: true};
 188          }
 189      },
 190  
 191          // Checks if a record was used and should be unique:
 192      checkUniqueUsed: function(objectId, uid, table) {
 193          if (this.data.unique && this.data.unique[objectId]) {
 194              var unique = this.data.unique[objectId];
 195              var values = $H(unique.used).values();
 196  
 197                  // for select: only the uid is stored
 198              if (unique['type'] == 'select') {
 199                  if (values.indexOf(uid) != -1) return true;
 200  
 201                  // for group/db: table and uid is stored in a assoc array
 202              } else if (unique.type == 'groupdb') {
 203                  for (var i=values.length-1; i>=0; i--) {
 204                          // if the pair table:uid is already used:
 205                      if (values[i].table==table && values[i].uid==uid) return true;
 206                  }
 207              }
 208          }
 209          return false;
 210      },
 211  
 212      setUniqueElement: function(objectId, table, uid, type, elName) {
 213          var recordUid = this.parseFormElementName('none', elName, 1, 1);
 214          // alert(objectId+'/'+table+'/'+uid+'/'+recordUid);
 215          this.setUnique(objectId, recordUid, uid);
 216      },
 217  
 218          // this function is applied to a newly inserted record by AJAX
 219          // it removes the used select items, that should be unique
 220      setUnique: function(objectId, recordUid, selectedValue) {
 221          if (this.data.unique && this.data.unique[objectId]) {
 222              var unique = this.data.unique[objectId];
 223  
 224              if (unique.type == 'select') {
 225                      // remove used items from each select-field of the child records
 226                  if (!(unique.selector && unique.max == -1)) {
 227                      var elName = this.parseFormElementName('full', objectId, 1)+'['+recordUid+']['+unique.field+']';
 228                      var formName = this.prependFormFieldNames+this.parseFormElementName('parts', objectId, 3, 1);
 229  
 230                      var fieldObj = document.getElementsByName(elName);
 231                      var values = $H(unique.used).values();
 232  
 233                      if (fieldObj.length) {
 234                              // remove all items from the new select-item which are already used in other children
 235                          for (var i=0; i<values.length; i++) this.removeSelectOption(fieldObj[0], values[i]);
 236                              // set the selected item automatically to the first of the remaining items if no selector is used
 237                          if (!unique.selector) {
 238                              selectedValue = fieldObj[0].options[0].value;
 239                              fieldObj[0].options[0].selected = true;
 240                              this.updateUnique(fieldObj[0], objectId, formName, recordUid);
 241                              this.handleChangedField(fieldObj[0], objectId+'['+recordUid+']');
 242                          }
 243                          if (typeof this.data.unique[objectId].used.length != 'undefined') {
 244                              this.data.unique[objectId].used = {};
 245                          }
 246                          this.data.unique[objectId].used[recordUid] = selectedValue;
 247                      }
 248                  }
 249              } else if (unique.type == 'groupdb') {
 250                      // add the new record to the used items:
 251                  this.data.unique[objectId].used[recordUid] = {'table':unique.elTable, 'uid':selectedValue};
 252              }
 253  
 254                  // remove used items from a selector-box
 255              if (unique.selector == 'select' && selectedValue) {
 256                  var selector = $(objectId+'_selector');
 257                  this.removeSelectOption(selector, selectedValue);
 258                  this.data.unique[objectId]['used'][recordUid] = selectedValue;
 259              }
 260          }
 261      },
 262  
 263      domAddNewRecord: function(method, insertObject, objectPrefix, htmlData) {
 264          if (this.isBelowMax(objectPrefix)) {
 265              if (method == 'bottom')
 266                  new Insertion.Bottom(insertObject, htmlData);
 267              else if (method == 'after')
 268                  new Insertion.After(insertObject, htmlData);
 269          }
 270      },
 271  
 272      changeSorting: function(objectId, direction) {
 273          var objectName = this.prependFormFieldNames+this.parseFormElementName('parts', objectId, 3, 2);
 274          var objectPrefix = this.parseFormElementName('full', objectId, 0, 1);
 275          var formObj = document.getElementsByName(objectName);
 276  
 277          if (formObj.length) {
 278                  // the uid of the calling object (last part in objectId)
 279              var callingUid = this.parseFormElementName('none', objectId, 1);
 280              var records = formObj[0].value.split(',');
 281              var current = records.indexOf(callingUid);
 282              var changed = false;
 283  
 284                  // move up
 285              if (direction > 0 && current > 0) {
 286                  records[current] = records[current-1];
 287                  records[current-1] = callingUid;
 288                  changed = true;
 289  
 290                  // move down
 291              } else if (direction < 0 && current < records.length-1) {
 292                  records[current] = records[current+1];
 293                  records[current+1] = callingUid;
 294                  changed = true;
 295              }
 296  
 297              if (changed) {
 298                  formObj[0].value = records.join(',');
 299                  var cAdj = direction > 0 ? 1 : 0; // adjustment
 300                  $(objectId+'_div').parentNode.insertBefore(
 301                      $(objectPrefix+'['+records[current-cAdj]+']_div'),
 302                      $(objectPrefix+'['+records[current+1-cAdj]+']_div')
 303                  );
 304                  this.redrawSortingButtons(objectPrefix, records);
 305              }
 306          }
 307  
 308          return false;
 309      },
 310  
 311      dragAndDropSorting: function(element) {
 312          var objectId = element.getAttribute('id').replace(/_records$/, '');
 313          var objectName = inline.prependFormFieldNames+inline.parseFormElementName('parts', objectId, 3);
 314          var formObj = document.getElementsByName(objectName);
 315  
 316          if (formObj.length) {
 317              var checked = new Array();
 318              var order = Sortable.sequence(element);
 319              var records = formObj[0].value.split(',');
 320  
 321                  // check if ordered uid is really part of the records
 322                  // virtually deleted items might still be there but ordering shouldn't saved at all on them
 323              for (var i=0; i<order.length; i++) {
 324                  if (records.indexOf(order[i]) != -1) {
 325                      checked.push(order[i]);
 326                  }
 327              }
 328  
 329              formObj[0].value = checked.join(',');
 330  
 331              if (inline.data.config && inline.data.config[objectId]) {
 332                  var table = inline.data.config[objectId].table;
 333                  inline.redrawSortingButtons(objectId+'['+table+']', checked);
 334              }
 335          }
 336      },
 337  
 338      createDragAndDropSorting: function(objectId) {
 339          Sortable.create(
 340              objectId,
 341              {
 342                  format: /^[^_\-](?:[A-Za-z0-9\[\]\-\_]*)\[(.*)\]_div$/,
 343                  onUpdate: inline.dragAndDropSorting,
 344                  tag: 'div',
 345                  handle: 'sortableHandle',
 346                  overlap: 'vertical',
 347                  constraint: 'vertical'
 348              }
 349          );
 350      },
 351  
 352      destroyDragAndDropSorting: function(objectId) {
 353          Sortable.destroy(objectId);
 354      },
 355  
 356      redrawSortingButtons: function(objectPrefix, records) {
 357          var i;
 358          var headerObj;
 359          var sortingObj = new Array();
 360  
 361              // if no records were passed, fetch them from form field
 362          if (typeof records == 'undefined') {
 363              records = new Array();
 364              var objectName = this.prependFormFieldNames+this.parseFormElementName('parts', objectPrefix, 3, 1);
 365              var formObj = document.getElementsByName(objectName);
 366              if (formObj.length) records = formObj[0].value.split(',');
 367          }
 368  
 369          for (i=0; i<records.length; i++) {
 370              if (!records[i].length) continue;
 371  
 372              headerObj = $(objectPrefix+'['+records[i]+']_header');
 373              sortingObj[0] = headerObj.getElementsByClassName('sortingUp');
 374              sortingObj[1] = headerObj.getElementsByClassName('sortingDown');
 375  
 376              if (sortingObj[0].length)
 377                  sortingObj[0][0].style.visibility = i == 0 ? 'hidden' : 'visible';
 378              if (sortingObj[1].length)
 379                  sortingObj[1][0].style.visibility = i == records.length-1 ? 'hidden' : 'visible';
 380          }
 381      },
 382  
 383      memorizeAddRecord: function(objectPrefix, newUid, afterUid, selectedValue) {
 384          if (this.isBelowMax(objectPrefix)) {
 385              var objectName = this.prependFormFieldNames+this.parseFormElementName('parts', objectPrefix, 3, 1);
 386              var formObj = document.getElementsByName(objectName);
 387  
 388              if (formObj.length) {
 389                  var records = new Array();
 390                  if (formObj[0].value.length) records = formObj[0].value.split(',');
 391  
 392                  if (afterUid) {
 393                      var newRecords = new Array();
 394                      for (var i=0; i<records.length; i++) {
 395                          if (records[i].length) newRecords.push(records[i]);
 396                          if (afterUid == records[i]) newRecords.push(newUid);
 397                      }
 398                      records = newRecords;
 399                  } else {
 400                      records.push(newUid);
 401                  }
 402                  formObj[0].value = records.join(',');
 403              }
 404  
 405              this.redrawSortingButtons(objectPrefix, records);
 406  
 407              if (this.data.unique && this.data.unique[objectPrefix]) {
 408                  var unique = this.data.unique[objectPrefix];
 409                  this.setUnique(objectPrefix, newUid, selectedValue);
 410              }
 411          }
 412  
 413              // if we reached the maximum off possible records after this action, hide the new buttons
 414          if (!this.isBelowMax(objectPrefix)) {
 415              this.hideElementsWithClassName('inlineNewButton',  this.parseFormElementName('full', objectPrefix, 0 , 1));
 416          }
 417  
 418          if (TBE_EDITOR) TBE_EDITOR.fieldChanged_fName(objectName, formObj);
 419      },
 420  
 421      memorizeRemoveRecord: function(objectName, removeUid) {
 422          var formObj = document.getElementsByName(objectName);
 423          if (formObj.length) {
 424              var parts = new Array();
 425              if (formObj[0].value.length) {
 426                  parts = formObj[0].value.split(',');
 427                  parts = parts.without(removeUid);
 428                  formObj[0].value = parts.join(',');
 429                  if (TBE_EDITOR) TBE_EDITOR.fieldChanged_fName(objectName, formObj);
 430                  return parts.length;
 431              }
 432          }
 433          return false;
 434      },
 435  
 436      updateUnique: function(srcElement, objectPrefix, formName, recordUid) {
 437          if (this.data.unique && this.data.unique[objectPrefix]) {
 438              var unique = this.data.unique[objectPrefix];
 439              var oldValue = unique.used[recordUid];
 440  
 441              if (unique.selector == 'select') {
 442                  var selector = $(objectPrefix+'_selector');
 443                  this.removeSelectOption(selector, srcElement.value);
 444                  if (typeof oldValue != 'undefined') this.readdSelectOption(selector, oldValue, unique);
 445              }
 446  
 447              if (!(unique.selector && unique.max == -1)) {
 448                  var formObj = document.getElementsByName(formName);
 449                  if (unique && formObj.length) {
 450                      var records = formObj[0].value.split(',');
 451                      var recordObj;
 452                      for (var i=0; i<records.length; i++) {
 453                          recordObj = document.getElementsByName(this.prependFormFieldNames+'['+unique.table+']['+records[i]+']['+unique.field+']');
 454                          if (recordObj.length && recordObj[0] != srcElement) {
 455                              this.removeSelectOption(recordObj[0], srcElement.value);
 456                              if (typeof oldValue != 'undefined') this.readdSelectOption(recordObj[0], oldValue, unique);
 457                          }
 458                      }
 459                      this.data.unique[objectPrefix].used[recordUid] = srcElement.value;
 460                  }
 461              }
 462          }
 463      },
 464  
 465      revertUnique: function(objectPrefix, elName, recordUid) {
 466          var unique = this.data.unique[objectPrefix];
 467          var fieldObj = elName ? document.getElementsByName(elName+'['+unique.field+']') : null;
 468  
 469          if (unique.type == 'select') {
 470              if (fieldObj && fieldObj.length) {
 471                  delete(this.data.unique[objectPrefix].used[recordUid])
 472  
 473                  if (unique.selector == 'select') {
 474                      if (!isNaN(fieldObj[0].value)) {
 475                          var selector = $(objectPrefix+'_selector');
 476                          this.readdSelectOption(selector, fieldObj[0].value, unique);
 477                      }
 478                  }
 479  
 480                  if (!(unique.selector && unique.max == -1)) {
 481                      var formName = this.prependFormFieldNames+this.parseFormElementName('parts', objectPrefix, 3, 1);
 482                      var formObj = document.getElementsByName(formName);
 483                      if (formObj.length) {
 484                          var records = formObj[0].value.split(',');
 485                          var recordObj;
 486                              // walk through all inline records on that level and get the select field
 487                          for (var i=0; i<records.length; i++) {
 488                              recordObj = document.getElementsByName(this.prependFormFieldNames+'['+unique.table+']['+records[i]+']['+unique.field+']');
 489                              if (recordObj.length) this.readdSelectOption(recordObj[0], fieldObj[0].value, unique);
 490                          }
 491                      }
 492                  }
 493              }
 494          } else if (unique.type == 'groupdb') {
 495              // alert(objectPrefix+'/'+recordUid);
 496              delete(this.data.unique[objectPrefix].used[recordUid])
 497          }
 498      },
 499  
 500      enableDisableRecord: function(objectId) {
 501          var elName = this.parseFormElementName('full', objectId, 2);
 502          var imageObj = $(objectId+'_disabled');
 503          var valueObj = document.getElementsByName(elName+'[hidden]');
 504          var formObj = document.getElementsByName(elName+'[hidden]_0');
 505          var imagePath = '';
 506  
 507          if (valueObj && formObj) {
 508              formObj[0].click();
 509              imagePath = this.parsePath(imageObj.src);
 510              imageObj.src = imagePath+(valueObj[0].value > 0 ? 'button_unhide.gif' : 'button_hide.gif');
 511          }
 512  
 513          return false;
 514      },
 515  
 516      deleteRecord: function(objectId) {
 517          var i, j, inlineRecords, records, childObjectId, childTable;
 518          var objectPrefix = this.parseFormElementName('full', objectId, 0 , 1);
 519          var elName = this.parseFormElementName('full', objectId, 2);
 520          var shortName = this.parseFormElementName('parts', objectId, 2);
 521          var recordUid = this.parseFormElementName('none', objectId, 1);
 522          var beforeDeleteIsBelowMax = this.isBelowMax(objectPrefix);
 523  
 524              // revert the unique settings if available
 525          if (this.data.unique && this.data.unique[objectPrefix]) this.revertUnique(objectPrefix, elName, recordUid);
 526  
 527              // if the record is new and was never saved before, just remove it from DOM
 528          if (this.isNewRecord(objectId)) {
 529              new Effect.Fade(objectId+'_div', { afterFinish: function() { Element.remove(objectId+'_div'); }    });
 530              // if the record already exists in storage, mark it to be deleted on clicking the save button
 531          } else {
 532              document.getElementsByName('cmd'+shortName+'[delete]')[0].disabled = false;
 533              new Effect.Fade(objectId+'_div');
 534          }
 535  
 536              // Remove from TBE_EDITOR (required fields, required range, etc.):
 537          if (TBE_EDITOR && TBE_EDITOR.removeElement) {
 538              inlineRecords = document.getElementsByClassName('inlineRecord', objectId+'_div');
 539                  // Remove nested child records from TBE_EDITOR required/range checks:
 540              for (i=inlineRecords.length-1; i>=0; i--) {
 541                  if (inlineRecords[i].value.length) {
 542                      records = inlineRecords[i].value.split(',');
 543                      childObjectId = this.data.map[inlineRecords[i].name];
 544                      childTable = this.data.config[childObjectId].table;
 545                      for (j=records.length-1; j>=0; j--) {
 546                          TBE_EDITOR.removeElement(this.prependFormFieldNames+'['+childTable+']['+records[j]+']');
 547                      }
 548                  }
 549              }
 550              TBE_EDITOR.removeElement(this.prependFormFieldNames+shortName);
 551          }
 552  
 553          var recordCount = this.memorizeRemoveRecord(
 554              this.prependFormFieldNames+this.parseFormElementName('parts', objectId, 3, 2),
 555              recordUid
 556          );
 557  
 558          if (recordCount <= 1) {
 559              this.destroyDragAndDropSorting(this.parseFormElementName('full', objectId, 0 , 2)+'_records');
 560          }
 561          this.redrawSortingButtons(objectPrefix);
 562  
 563              // if the NEW-button was hidden and now we can add again new children, show the button
 564          if (!beforeDeleteIsBelowMax && this.isBelowMax(objectPrefix))
 565              this.showElementsWithClassName('inlineNewButton', this.parseFormElementName('full', objectPrefix, 0 , 1));
 566  
 567          return false;
 568      },
 569  
 570      parsePath: function(path) {
 571          var backSlash = path.lastIndexOf('\\');
 572          var normalSlash = path.lastIndexOf('/');
 573  
 574          if (backSlash > 0)
 575              path = path.substring(0,backSlash+1);
 576          else if (normalSlash > 0)
 577              path = path.substring(0,normalSlash+1);
 578          else
 579              path = '';
 580  
 581          return path;
 582      },
 583  
 584      parseFormElementName: function(wrap, objectId, rightCount, skipRight) {
 585              // remove left and right side "data[...|...]" -> '...|...'
 586          objectId = objectId.substr(0, objectId.lastIndexOf(']')).substr(objectId.indexOf('[')+1);
 587  
 588          if (!wrap) wrap = 'full';
 589          if (!skipRight) skipRight = 0;
 590  
 591          var elReturn;
 592          var elParts = new Array();
 593          var idParts = objectId.split('][');
 594          for (var i=0; i<skipRight; i++) idParts.pop();
 595  
 596          if (rightCount > 0) {
 597              for (var i=0; i<rightCount; i++) elParts.unshift(idParts.pop());
 598          } else {
 599              for (var i=0; i<-rightCount; i++) idParts.shift();
 600              elParts = idParts;
 601          }
 602  
 603          if (wrap == 'full') {
 604              elReturn = this.prependFormFieldNames+'['+elParts.join('][')+']';
 605          } else if (wrap == 'parts') {
 606              elReturn = '['+elParts.join('][')+']';
 607          } else if (wrap == 'none') {
 608              elReturn = elParts.length > 1 ? elParts : elParts.join('');
 609          }
 610  
 611          return elReturn;
 612      },
 613  
 614      handleChangedField: function(formField, objectId) {
 615          var formObj;
 616          if (typeof formField == 'object') {
 617              formObj = formField;
 618          } else {
 619              formObj = document.getElementsByName(formField);
 620              if (formObj.length) formObj = formObj[0];
 621          }
 622  
 623          if (formObj != undefined) {
 624              var value;
 625              if (formObj.nodeName == 'SELECT') value = formObj.options[formObj.selectedIndex].text;
 626              else value = formObj.value;
 627              $(objectId+'_label').innerHTML = value.length ? value : this.noTitleString;
 628          }
 629          return true;
 630      },
 631  
 632      arrayAssocCount: function(object) {
 633          var count = 0;
 634          if (typeof object.length != 'undefined') {
 635              count = object.length;
 636          } else {
 637              for (var i in object) count++;
 638          }
 639          return count;
 640      },
 641  
 642      isBelowMax: function(objectPrefix) {
 643          var isBelowMax = true;
 644          var objectName = this.prependFormFieldNames+this.parseFormElementName('parts', objectPrefix, 3, 1);
 645          var formObj = document.getElementsByName(objectName);
 646  
 647          if (this.data.config && this.data.config[objectPrefix] && formObj.length) {
 648              var recordCount = formObj[0].value ? formObj[0].value.split(',').length : 0;
 649              if (recordCount >= this.data.config[objectPrefix].max) isBelowMax = false;
 650          }
 651          if (isBelowMax && this.data.unique && this.data.unique[objectPrefix]) {
 652              var unique = this.data.unique[objectPrefix];
 653              if (this.arrayAssocCount(unique.used) >= unique.max && unique.max >= 0) isBelowMax = false;
 654          }
 655          return isBelowMax;
 656      },
 657  
 658      getOptionsHash: function(selectObj) {
 659          var optionsHash = {};
 660          for (var i=0; i<selectObj.options.length; i++) optionsHash[selectObj.options[i].value] = i;
 661          return optionsHash;
 662      },
 663  
 664      removeSelectOption: function(selectObj, value) {
 665          var optionsHash = this.getOptionsHash(selectObj);
 666          if (optionsHash[value] != undefined) selectObj.options[optionsHash[value]] = null;
 667      },
 668  
 669      readdSelectOption: function(selectObj, value, unique) {
 670          var index = null;
 671          var optionsHash = this.getOptionsHash(selectObj);
 672          var possibleValues = $H(unique.possible).keys();
 673  
 674          for (var possibleValue in unique.possible) {
 675              if (possibleValue == value) break;
 676              if (optionsHash[possibleValue] != undefined) index = optionsHash[possibleValue];
 677          }
 678  
 679          if (index == null) index = 0;
 680          else if (index < selectObj.options.length) index++;
 681              // recreate the <option> tag
 682          var readdOption = document.createElement('option');
 683          readdOption.text = unique.possible[value];
 684          readdOption.value = value;
 685              // add the <option> at the right position
 686          selectObj.add(readdOption, document.all ? index : selectObj.options[index]);
 687      },
 688  
 689      hideElementsWithClassName: function(className, parentElement) {
 690          this.setVisibilityOfElementsWithClassName('hide', className, parentElement);
 691      },
 692  
 693      showElementsWithClassName: function(className, parentElement) {
 694          this.setVisibilityOfElementsWithClassName('show', className, parentElement);
 695      },
 696  
 697      setVisibilityOfElementsWithClassName: function(action, className, parentElement) {
 698          var domObjects = document.getElementsByClassName(className, parentElement);
 699          for (var i=0; i<domObjects.length; i++) {
 700              if (action == 'hide')
 701                  new Effect.Fade(domObjects[i]);
 702              else if (action = 'show')
 703                  new Effect.Appear(domObjects[i]);
 704          }
 705      },
 706  
 707      fadeOutFadeIn: function(objectId) {
 708          var optIn = { duration:0.5, transition:Effect.Transitions.linear, from:0.50, to:1.00 };
 709          var optOut = { duration:0.5, transition:Effect.Transitions.linear, from:1.00, to:0.50 };
 710          optOut.afterFinish = function() { new Effect.Opacity(objectId, optIn); };
 711          new Effect.Opacity(objectId, optOut);
 712      },
 713  
 714      isNewRecord: function(objectId) {
 715          return $(objectId+'_div') && $(objectId+'_div').hasClassName('inlineIsNewRecord')
 716              ? true
 717              : false;
 718      },
 719  
 720          // Find and fix nested of inline and tab levels if a new element was created dynamically (it doesn't know about its nesting):
 721      findContinuedNestedLevel: function(nested, objectId) {
 722          if (this.data.nested && this.data.nested[objectId]) {
 723                  // Remove the first element from the new nested stack, it's just a hint:
 724              nested.shift();
 725              nested = this.data.nested[objectId].concat(nested);
 726              return nested;
 727          } else {
 728              return nested;
 729          }
 730      },
 731  
 732      getNumberOfRTE: function() {
 733          var number = 0;
 734          if (typeof RTEarea != 'undefined' && RTEarea.length > 0) {
 735              number = RTEarea.length-1;
 736          }
 737          return number;
 738        }
 739  }
 740  
 741  Object.extend(Array.prototype, {
 742      diff: function(current) {
 743          var diff = new Array();
 744          if (this.length == current.length) {
 745              for (var i=0; i<this.length; i++) {
 746                  if (this[i] !== current[i]) diff.push(i);
 747              }
 748          }
 749          return diff;
 750      }
 751  });
 752  
 753  /*]]>*/


Généré le : Sun Nov 25 17:13:16 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics