/* * Tagedit - jQuery Plugin * The Plugin can be used to edit tags from a database the easy way * * Examples and documentation at: tagedit.webwork-albrecht.de * * License: * This work is licensed under a MIT License * * @licstart The following is the entire license notice for the * JavaScript code in this file. * * Copyright (c) 2010 Oliver Albrecht * Copyright (c) 2014 Thomas BrĂ¼derli * * Licensed under the MIT licenses * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @licend The above is the entire license notice * for the JavaScript code in this file. * * @author Oliver Albrecht Mial: info@webwork-albrecht.de Twitter: @webworka * @version 1.5.2 (06/2014) * Requires: jQuery v1.4+, jQueryUI v1.8+, jQuerry.autoGrowInput * * Example of usage: * * $( "input.tag" ).tagedit(); * * Possible options: * * autocompleteURL: '', // url for a autocompletion * deleteEmptyItems: true, // Deletes items with empty value * deletedPostfix: '-d', // will be put to the Items that are marked as delete * addedPostfix: '-a', // will be put to the Items that are choosem from the database * additionalListClass: '', // put a classname here if the wrapper ul shoud receive a special class * allowEdit: true, // Switch on/off edit entries * allowDelete: true, // Switch on/off deletion of entries. Will be ignored if allowEdit = false * allowAdd: true, // switch on/off the creation of new entries * direction: 'ltr' // Sets the writing direction for Outputs and Inputs * animSpeed: 500 // Sets the animation speed for effects * autocompleteOptions: {}, // Setting Options for the jquery UI Autocomplete (http://jqueryui.com/demos/autocomplete/) * breakKeyCodes: [ 13, 44 ], // Sets the characters to break on to parse the tags (defaults: return, comma) * checkNewEntriesCaseSensitive: false, // If there is a new Entry, it is checked against the autocompletion list. This Flag controlls if the check is (in-)casesensitive * texts: { // some texts * removeLinkTitle: 'Remove from list.', * saveEditLinkTitle: 'Save changes.', * deleteLinkTitle: 'Delete this tag from database.', * deleteConfirmation: 'Are you sure to delete this entry?', * deletedElementTitle: 'This Element will be deleted.', * breakEditLinkTitle: 'Cancel' * } */ (function($) { $.fn.tagedit = function(options) { /** * Merge Options with defaults */ options = $.extend(true, { // default options here autocompleteURL: null, checkToDeleteURL: null, deletedPostfix: '-d', addedPostfix: '-a', additionalListClass: '', allowEdit: true, allowDelete: true, allowAdd: true, direction: 'ltr', animSpeed: 500, autocompleteOptions: { select: function( event, ui ) { $(this).val(ui.item.value).trigger('transformToTag', [ui.item.id]); return false; } }, breakKeyCodes: [ 13, 44 ], checkNewEntriesCaseSensitive: false, texts: { removeLinkTitle: 'Remove from list.', saveEditLinkTitle: 'Save changes.', deleteLinkTitle: 'Delete this tag from database.', deleteConfirmation: 'Are you sure to delete this entry?', deletedElementTitle: 'This Element will be deleted.', breakEditLinkTitle: 'Cancel', forceDeleteConfirmation: 'There are more records using this tag, are you sure do you want to remove it?' }, tabindex: false }, options || {}); // no action if there are no elements if(this.length == 0) { return; } // set the autocompleteOptions source if(options.autocompleteURL) { options.autocompleteOptions.source = options.autocompleteURL; } // Set the direction of the inputs var direction= this.attr('dir'); if(direction && direction.length > 0) { options.direction = this.attr('dir'); } var elements = this; var focusItem = null; var baseNameRegexp = new RegExp("^(.*)\\[([0-9]*?("+options.deletedPostfix+"|"+options.addedPostfix+")?)?\]$", "i"); var baseName = elements.eq(0).attr('name').match(baseNameRegexp); if(baseName && baseName.length == 4) { baseName = baseName[1]; } else { // Elementname does not match the expected format, exit alert('elementname dows not match the expected format (regexp: '+baseNameRegexp+')') return; } // read tabindex from source element var ti; if (!options.tabindex && (ti = elements.eq(0).attr('tabindex'))) options.tabindex = ti; // init elements inputsToList(); /** * Creates the tageditinput from a list of textinputs * */ function inputsToList() { var html = ''; elements .append(html) .attr('tabindex', options.tabindex) // set tabindex to