emposha / FCBKcomplete

Jquery facebook like(fancy) multi-select
http://www.emposha.com
383 stars 116 forks source link

Can you look at my suggestions for improving the code #130

Open chumachkin opened 12 years ago

chumachkin commented 12 years ago

Change function addItem

  function addItem(title, value, preadded, locked, focusme) {
    if (!maxItems()) {
      return false;
    }
    var liclass = "bit-box" + (locked ? " locked": "");
    var id = randomId();
    var txt = document.createTextNode(xssDisplay(title));
    var aclose = $('<a class="closebutton" href="#"></a>');
    var li = $('<li class="'+liclass+'" rel="'+value+'" id="pt_'+id+'"></li>').prepend(txt).append(aclose);

    if (preadded) {
        holder.append(li);

        aclose.click( function() {
          removeItem($(this).parent("li"));
          return false;
        });
    }

    if (!preadded) {
        $("#" + elemid + "_annoninput").remove();
        var _item = $('<option value="'+xssDisplay(value, 1)+'" id="opt_'+id+'" class="selected" selected="selected">'+xssDisplay(title)+'</option>');
        if (options.onselect) {
          if ( funCall(options.onselect, _item) == true ) {
              holder.append(li);

              aclose.click( function() {
              removeItem($(this).parent("li"));
                 return false;
              });

              addInput(focusme);

              element.append(_item);
              element.change();
            }else {
              addInput(focusme);
              element.change();
            }
        }
    }

    holder.children("li.bit-box.deleted").removeClass("deleted");
    clear_feed(1);
    return id;
  }

Why these changes, example:

                    onselect : function(item) {
                               if ( $("#stopwordselect option:selected[value="+item._value+"]").length >0 ) {
                                    $('#stopwords_warnings').empty();
                                    $('#stopwords_warnings').append('Word already in list<br/>');
                                    return false;
                               }
                                $.post('<?php echo bloginfo('url') .'/wp-admin/admin-ajax.php?action=vk_ajax_stopwords_search'; ?>',
                                    { 
                                        tag: item._value
                                    },
                                    function(json_data) {
                                       $('#stopwords_warnings').empty();
                                        if ( json_data.err == undefined ) {
                                            for (var suc in json_data.suc) {
                                                $('#stopwords_warnings').append(json_data.suc+'<br/>');
                                            }
                                        } else {
                                            for (var err in json_data.err) {
                                                $('#stopwords_warnings').append(json_data.err+'<br/>');
                                            }
                                        }
                                    },
                                    "json"
                                );

                        return true;
                    },