jquery.jeditable.original.js
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 15k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2. +-----------------------------------------------------------------------+
  3. | Copyright (c) 2006-2007 Mika Tuupola, Dylan Verheul                   |
  4. | All rights reserved.                                                  |  
  5. |                                                                       |
  6. | Redistribution and use in source and binary forms, with or without    |
  7. | modification, are permitted provided that the following conditions    |
  8. | are met:                                                              |
  9. |                                                                       | 
  10. | o Redistributions of source code must retain the above copyright      |
  11. |   notice, this list of conditions and the following disclaimer.       |
  12. | o Redistributions in binary form must reproduce the above copyright   |
  13. |   notice, this list of conditions and the following disclaimer in the |
  14. |   documentation and/or other materials provided with the distribution.|
  15. |                                                                       |
  16. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
  17. | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
  18. | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
  19. | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
  20. | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
  21. | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
  22. | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
  23. | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
  24. | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
  25. | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
  26. | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
  27. |                                                                       |
  28. +-----------------------------------------------------------------------+
  29. */
  30. /* $Id: jquery.jeditable.original.js,v 1.1 2007/09/02 16:57:20 rafaelsteil Exp $ */
  31. /**
  32.   * jQuery inplace editor plugin (version 1.4.x)
  33.   *
  34.   * Based on editable by Dylan Verheul <dylan@dyve.net>
  35.   * http://www.dyve.net/jquery/?editable
  36.   *
  37.   * @name  jEditable
  38.   * @type  jQuery
  39.   * @param String  target             POST URL or function name to send edited content
  40.   * @param Hash    options            additional options 
  41.   * @param String  options[name]      POST parameter name of edited content
  42.   * @param String  options[id]        POST parameter name of edited div id
  43.   * @param Hash    options[submitdata] Extra parameters to send when submitting edited content.
  44.   * @param String  options[type]      text, textarea or select
  45.   * @param Integer options[rows]      number of rows if using textarea
  46.   * @param Integer options[cols]      number of columns if using textarea
  47.   * @param Mixed   options[height]    'auto' or height in pixels
  48.   * @param Mixed   options[width]     'auto' or width in pixels 
  49.   * @param String  options[loadurl]   URL to fetch external content before editing
  50.   * @param String  options[loadtype]  Request type for load url. Should be GET or POST.
  51.   * @param String  options[loadtext]  Text to display while loading external content.
  52.   * @param Hash    options[loaddata]  Extra parameters to pass when fetching content before editing.
  53.   * @param String  options[data]      Or content given as paramameter.
  54.   * @param String  options[indicator] indicator html to show when saving
  55.   * @param String  options[tooltip]   optional tooltip text via title attribute
  56.   * @param String  options[event]     jQuery event such as 'click' of 'dblclick'
  57.   * @param String  options[onblur]    'cancel', 'submit' or 'ignore'
  58.   * @param String  options[submit]    submit button value, empty means no button
  59.   * @param String  options[cancel]    cancel button value, empty means no button
  60.   * @param String  options[cssclass]  CSS class to apply to input form. 'inherit' to copy from parent.
  61.   * @param String  options[style]     Style to apply to input form 'inherit' to copy from parent.
  62.   * @param String  options[select]    true or false, when true text is highlighted
  63.   *             
  64.   */
  65. jQuery.fn.editable = function(target, options, callback) {
  66.     /* prevent elem has no properties error */
  67.     if (this.length === 0) { 
  68.         return(this); 
  69.     }
  70.     
  71.     var settings = {
  72.         target     : target,
  73.         name       : 'value',
  74.         id         : 'id',
  75.         type       : 'text',
  76.         width      : 'auto',
  77.         height     : 'auto',
  78.         event      : 'click',
  79.         onblur     : 'cancel',
  80.         loadtype   : 'GET',
  81.         loadtext   : 'Loading...',
  82.         loaddata   : {},
  83.         submitdata : {}
  84.     };
  85.         
  86.     if(options) {
  87.         jQuery.extend(settings, options);
  88.     }
  89.     
  90.     /* setup some functions */
  91.     var plugin   = jQuery.editable.types[settings.type].plugin || function() { };
  92.     var submit   = jQuery.editable.types[settings.type].submit || function() { };
  93.     var buttons  = jQuery.editable.types[settings.type].buttons 
  94.                 || jQuery.editable.types['defaults'].buttons;
  95.     var content  = jQuery.editable.types[settings.type].content 
  96.                 || jQuery.editable.types['defaults'].content;
  97.     var element  = jQuery.editable.types[settings.type].element 
  98.                 || jQuery.editable.types['defaults'].element;
  99.     callback = callback || function() { };
  100.           
  101.     jQuery(this).attr('title', settings.tooltip);
  102.     jQuery(this)[settings.event](function(e) {
  103.         /* save this to self because this changes when scope changes */
  104.         var self = this;
  105.         /* prevent throwing an exeption if edit field is clicked again */
  106.         if (self.editing) {
  107.             return;
  108.         }
  109.         /* figure out how wide and tall we are */
  110.         /* TODO: this is a bit of PHPism */
  111.         // var width =         
  112.         settings.width = 
  113.             ('auto' == settings.width)  ? jQuery(self).width()  : settings.width;
  114.         // var height = 
  115.         settings.height = 
  116.             ('auto' == settings.height) ? jQuery(self).height() : settings.height;
  117.             
  118.         self.editing    = true;
  119.         self.revert     = jQuery(self).html();
  120.         self.innerHTML  = '';
  121.         /* create the form object */
  122.         var f = document.createElement('form');
  123.         
  124.         /* apply css or style or both */
  125.         if (settings.cssclass) {
  126.             if ('inherit' == settings.cssclass) {
  127.                 jQuery(f).attr('class', jQuery(self).attr('class'));
  128.             } else {
  129.                 jQuery(f).attr('class', settings.cssclass);
  130.             }
  131.         }
  132.         
  133.         if (settings.style) {
  134.             if ('inherit' == settings.style) {
  135.                 jQuery(f).attr('style', jQuery(self).attr('style'));
  136.                 /* IE needs the second line or display wont be inherited */
  137.                 jQuery(f).css('display', jQuery(self).css('display'));                
  138.             } else {
  139.                 jQuery(f).attr('style', settings.style);
  140.             }
  141.         }
  142.         
  143.         /*  Add main input element to form and store it in i. */
  144.         var i = element.apply(f, [settings, self]);
  145.         /* maintain bc with 1.1.1 and earlier versions */        
  146.         if (settings.getload) {
  147.             settings.loadurl    = settings.getload;
  148.             settings.loadtype = 'GET';
  149.         } else if (settings.postload) {
  150.             settings.loadurl    = settings.postload;
  151.             settings.loadtype = 'POST';
  152.         }
  153.         /* set input content via POST, GET, given data or existing value */
  154.         if (settings.loadurl) {
  155.             var t = setTimeout(function() {
  156.                 i.disabled = true;
  157.                 content.apply(f, [settings.loadtext, settings, self]);
  158.             }, 100);
  159.                 
  160.             var loaddata = {};
  161.             loaddata[settings.id] = self.id;
  162.             if (jQuery.isFunction(settings.loaddata)) {
  163.                 jQuery.extend(loaddata, settings.loaddata.apply(self, [self.revert, settings]));
  164.             } else {
  165.                 jQuery.extend(loaddata, settings.loaddata);
  166.             }
  167.             jQuery.ajax({
  168.                type : settings.loadtype,
  169.                url  : settings.loadurl,
  170.                data : loaddata,
  171.                success: function(string) {
  172.                   window.clearTimeout(t);                
  173.                   content.apply(f, [string, settings, self]);
  174.                   i.disabled = false;
  175.                }
  176.             });
  177.         } else if (settings.data) {
  178.             content.apply(f, [settings.data, settings, self]);
  179.         } else { 
  180.             content.apply(f, [self.revert, settings, self]);
  181.         }
  182.         i.name  = settings.name;
  183.         
  184.         /* add buttons to the form */
  185.         buttons.apply(f, [settings, self]);
  186.         /* add created form to self */
  187.         self.appendChild(f);
  188.         
  189.         /* highlight input contents when requested */
  190.         if (settings.select) {
  191.             i.select();
  192.         }
  193.          
  194.         /* attach 3rd party plugin if requested */
  195.         plugin.apply(f, [settings, self]);            
  196.         /* focus to first visible form element */
  197.         jQuery(":input:visible:enabled:first", f).focus();
  198.         
  199.         /* discard changes if pressing esc */
  200.         jQuery(i).keydown(function(e) {
  201.             if (e.keyCode == 27) {
  202.                 e.preventDefault();
  203.                 reset();
  204.             }
  205.         });
  206.         /* discard, submit or nothing with changes when clicking outside */
  207.         /* do nothing is usable when navigating with tab */
  208.         var t;
  209.         if ('cancel' == settings.onblur) {
  210.             jQuery(i).blur(function(e) {
  211.                 t = setTimeout(reset, 500);
  212.             });
  213.         } else if ('submit' == settings.onblur) {
  214.             jQuery(i).blur(function(e) {
  215.                 jQuery(f).submit();
  216.             });
  217.         } else {
  218.             jQuery(i).blur(function(e) {
  219.               /* TODO: maybe something here */
  220.             });
  221.         }
  222.         jQuery(f).submit(function(e) {
  223.             if (t) { 
  224.                 clearTimeout(t);
  225.             }
  226.             /* do no submit */
  227.             e.preventDefault(); 
  228.             
  229.             /* if this input type has a call before submit hook, call it */
  230.             submit.apply(f, [settings, self]);            
  231.             /* check if given target is function */
  232.             if (jQuery.isFunction(settings.target)) {
  233.                 var str = settings.target.apply(self, [jQuery(i).val(), settings]);
  234.                 self.innerHTML = str;
  235.                 self.editing = false;
  236.                 callback.apply(self, [self.innerHTML, settings]);
  237.             } else {
  238.                 /* add edited content and id of edited element to POST */
  239.                 var submitdata = {};
  240.                 submitdata[i.name] = jQuery(i).val();
  241.                 submitdata[settings.id] = self.id;
  242.                 /* add extra data to be POST:ed */
  243.                 if (jQuery.isFunction(settings.submitdata)) {
  244.                     jQuery.extend(submitdata, settings.submitdata.apply(self, [self.revert, settings]));
  245.                 } else {
  246.                     jQuery.extend(submitdata, settings.submitdata);
  247.                 }          
  248.                 /* show the saving indicator */
  249.                 jQuery(self).html(settings.indicator);
  250.                 jQuery.post(settings.target, submitdata, function(str) {
  251.                     self.innerHTML = str;
  252.                     self.editing = false;
  253.                     callback.apply(self, [self.innerHTML, settings]);
  254.                 });
  255.             }
  256.                         
  257.             return false;
  258.         });
  259.         function reset() {
  260.             self.innerHTML = self.revert;
  261.             self.editing   = false;
  262.         }
  263.     });
  264.     
  265.     return(this);
  266. };
  267. /**
  268.   *
  269.   */
  270.  
  271. jQuery.editable = {
  272.     types: {
  273.         defaults: {
  274.             element : function(settings, original) {
  275.                 var input = jQuery('<input type="hidden">');                
  276.                 jQuery(this).append(input);
  277.                 return(input);
  278.             },
  279.             content : function(string, settings, original) {
  280.                 jQuery(':input:first', this).val(string);
  281.             },
  282.             buttons : function(settings, original) {
  283.                 if (settings.submit) {
  284.                     var submit = jQuery('<input type="submit">');
  285.                     submit.val(settings.submit);
  286.                     jQuery(this).append(submit);
  287.                 }
  288.                 if (settings.cancel) {
  289.                     var cancel = jQuery('<input type="button">');
  290.                     cancel.val(settings.cancel);
  291.                     jQuery(this).append(cancel);
  292.                     jQuery(cancel).click(function() {
  293.                         jQuery(original).html(original.revert);
  294.                         original.editing = false;
  295.                     });
  296.                 }
  297.             }
  298.         },
  299.         text: {
  300.             element : function(settings, original) {
  301.                 var input = jQuery('<input>');
  302.                 input.width(settings.width);
  303.                 input.height(settings.height);
  304.                 /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
  305.                 //input[0].setAttribute('autocomplete','off');
  306.                 input.attr('autocomplete','off');
  307.                 jQuery(this).append(input);
  308.                 return(input);
  309.             }
  310.         },
  311.         textarea: {
  312.             element : function(settings, original) {
  313.                 var textarea = jQuery('<textarea>');
  314.                 if (settings.rows) {
  315.                     textarea.attr('rows', settings.rows);
  316.                 } else {
  317.                     textarea.height(settings.height);
  318.                 }
  319.                 if (settings.cols) {
  320.                     textarea.attr('cols', settings.cols);
  321.                 } else {
  322.                     textarea.width(settings.width);
  323.                 }
  324.                 jQuery(this).append(textarea);
  325.                 return(textarea);
  326.             }
  327.         },
  328.         select: {
  329.             element : function(settings, original) {
  330.                 var select = jQuery('<select>');
  331.                 jQuery(this).append(select);
  332.                 return(select);
  333.             },
  334.             content : function(string, settings, original) {
  335.                 if (String == string.constructor) {   
  336.                     eval ("var json = " + string);
  337.                     for (var key in json) {
  338.                         if ('selected' == key) {
  339.                             continue;
  340.                         } 
  341.                         var option = $('<option>').val(key).append(json[key]);
  342.                         if (key == json['selected']) {
  343.                             console.log(key);
  344.                             /* TODO: why does not this work? */
  345.                             //option.attr('selected', 'selected');
  346.                             option[0].selected = true;
  347.                         }
  348.                         jQuery("select", this).append(option);   
  349.                     }
  350.                 }
  351.             }
  352.         }
  353.     },
  354.     
  355.     /* Add new input type */
  356.     addInputType: function(name, input) {
  357.         jQuery.editable.types[name] = input;
  358.     }
  359. };