All files / components/inlineEdit/js InlineEdit.js

92.86% Statements 273/294
70.94% Branches 83/117
95.16% Functions 59/62
92.86% Lines 273/294

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980                                1x   1x     1x   1x           1x 34x 34x     1x 1x       1x 1x   1x 1x         1x 33x 33x   33x 33x 33x 33x       33x 33x   33x                 1x 58x 54x       6x   58x 58x               58x 6x   6x 1x 1x   5x 5x     5x   58x 54x   58x     1x 59x     59x 1x 1x   1x   59x       1x 93x 59x   59x 59x   59x 58x   59x 59x 59x         1x 34x   34x 34x 34x 34x 34x     34x 34x       34x 20x 20x       34x     1x 70x 70x 70x     1x 13x 13x 13x     1x 2x       2x             1x 70x 70x     1x 85x 85x 26x       1x 71x 71x   71x 26x 26x 26x 26x       1x 34x 34x     34x   34x         1x 59x                               59x   59x 58x       1x 59x 59x                         1x 60x 60x 60x 60x                           1x 60x 60x 60x 60x   60x                     1x 59x 59x 59x     59x 59x   59x     59x                       1x 59x 59x 59x 25x         59x       59x 59x 59x 59x                   1x 60x 60x 60x   60x                   1x 59x   59x                                                 1x 60x 60x 60x   60x               1x         60x 60x 60x                     1x 59x 59x   59x 58x 58x 58x           58x 58x 26x     58x     58x   59x                   1x 84x     84x                 1x 59x 2x   59x 1x   59x                           1x 117x   117x 234x 25x 25x 25x 4x       117x 117x                   1x 117x   117x 117x 4x 4x                     1x 58x 58x   58x 58x 2x                         1x 175x 175x     6x 6x 6x     6x 6x 6x           1x 58x   58x 58x 58x 58x 58x 58x                   1x 58x   58x 58x   58x 58x   58x   58x     1x 118x     1x                                   1x 297x     1x                   1x 85x 70x 15x 13x   2x     85x 2x   2x 2x         1x                   1x 85x 85x       1x   59x 59x       59x 59x           1x 68x     1x 60x         1x 59x 7x 7x 7x 7x   7x 7x 7x                       1x                                                                                                                                                                                                                                                                                                                                                                                                                                                             1x   6x 13x       13x 13x 13x         1x                                                 1x 6x 6x        
/*
Copyright 2008-2009 University of Cambridge
Copyright 2008-2010 University of Toronto
Copyright 2008-2009 University of California, Berkeley
Copyright 2010-2016 OCAD University
Copyright 2012-2013 Raising the Floor - US
Copyright 2015-2016 Raising the Floor - International
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
 
var fluid_3_0_0 = fluid_3_0_0 || {};
 
(function ($, fluid) {
    "use strict";
 
    fluid.registerNamespace("fluid.inlineEdit");
 
    fluid.inlineEdit.sendKey = function (control, event, virtualCode, charCode) {
        var kE = document.createEvent("KeyEvents");
        kE.initKeyEvent(event, 1, 1, null, 0, 0, 0, 0, virtualCode, charCode);
        control.dispatchEvent(kE);
    };
 
    fluid.inlineEdit.switchToViewMode = function (that) {
        that.editContainer.hide();
        that.displayModeRenderer.show();
    };
 
    fluid.inlineEdit.cancel = function (that) {
        Eif (that.isEditing()) {
            // Roll the edit field back to its old value and close it up.
            // This setTimeout is necessary on Firefox, since any attempt to modify the
            // input control value during the stack processing the ESCAPE key will be ignored.
            setTimeout(function () {
                that.editView.value(that.model.value);
            }, 1);
            fluid.inlineEdit.switchToViewMode(that);
            that.events.afterFinishEdit.fire(that.model.value, that.model.value,
                that.editField[0], that.viewEl[0]);
        }
    };
 
    fluid.inlineEdit.finish = function (that) {
        var newValue = that.editView.value();
        var oldValue = that.model.value;
 
        var viewNode = that.viewEl[0];
        var editNode = that.editField[0];
        var ret = that.events.onFinishEdit.fire(newValue, oldValue, editNode, viewNode);
        Iif (ret === false) {
            return;
        }
 
        that.updateModelValue(newValue);
        that.events.afterFinishEdit.fire(newValue, oldValue, editNode, viewNode);
 
        fluid.inlineEdit.switchToViewMode(that);
    };
 
    /**
     * Do not allow the textEditButton to regain focus upon completion unless
     * the keypress is enter or esc.
     *
     * @param {Object} that - The component itself.
     */
    fluid.inlineEdit.bindEditFinish = function (that) {
        if (that.options.submitOnEnter === undefined) {
            that.options.submitOnEnter = "textarea" !== fluid.unwrap(that.editField).nodeName.toLowerCase();
        }
        function keyCode(evt) {
            // Fix for handling arrow key presses. See FLUID-760.
            return evt.keyCode ? evt.keyCode : (evt.which ? evt.which : 0);
        }
        var button = that.textEditButton || $();
        var escHandler = function (evt) {
            var code = keyCode(evt);
            if (code === $.ui.keyCode.ESCAPE) {
                button.focus();
                fluid.inlineEdit.cancel(that);
                return false;
            }
        };
        var finishHandler = function (evt) {
            var code = keyCode(evt);
 
            if (code !== $.ui.keyCode.ENTER) {
                button.blur();
                return true;
            } else {
                fluid.inlineEdit.finish(that);
                button.focus();
            }
 
            return false;
        };
        if (that.options.submitOnEnter) {
            that.editContainer.keypress(finishHandler);
        }
        that.editContainer.keydown(escHandler);
    };
 
    fluid.inlineEdit.bindBlurHandler = function (that) {
        Iif (that.options.blurHandlerBinder) {
            that.options.blurHandlerBinder(that);
        } else {
            var blurHandler = function () {
                Eif (that.isEditing()) {
                    fluid.inlineEdit.finish(that);
                }
                return false;
            };
            that.editField.blur(blurHandler);
        }
    };
 
    fluid.inlineEdit.initializeEditView = function (that, initial) {
        if (!that.editInitialized) {
            fluid.inlineEdit.renderEditContainer(that, !that.options.lazyEditView || !initial);
 
            Eif (!that.options.lazyEditView || !initial) {
                that.events.onCreateEditView.fire();
 
                if (that.textEditButton) {
                    fluid.inlineEdit.bindEditFinish(that);
                }
                fluid.inlineEdit.bindBlurHandler(that);
                that.editView.refreshView(that);
                that.editInitialized = true;
            }
        }
    };
 
    fluid.inlineEdit.edit = function (that) {
        fluid.inlineEdit.initializeEditView(that, false);
 
        var viewEl = that.viewEl;
        var displayText = that.displayView.value();
        that.updateModelValue(that.model.value === "" ? "" : displayText);
        Eif (that.options.applyEditPadding) {
            that.editField.width(Math.max(viewEl.width() + that.options.paddings.edit, that.options.paddings.minimumEdit));
        }
 
        that.displayModeRenderer.hide();
        that.editContainer.show();
 
        // Work around for FLUID-726
        // Without 'setTimeout' the finish handler gets called with the event and the edit field is inactivated.
        setTimeout(function () {
            that.editField.focus();
            Iif (that.options.selectOnEdit) {
                that.editField[0].select();
            }
        }, 0);
        that.events.afterBeginEdit.fire();
    };
 
    fluid.inlineEdit.clearEmptyViewStyles = function (textEl, styles, originalViewPadding) {
        textEl.removeClass(styles.defaultViewStyle);
        textEl.css("padding-right", originalViewPadding);
        textEl.removeClass(styles.emptyDefaultViewText);
    };
 
    fluid.inlineEdit.showDefaultViewText = function (that) {
        that.displayView.value(that.options.strings.defaultViewText);
        that.viewEl.css("padding-right", that.existingPadding);
        that.viewEl.addClass(that.options.styles.defaultViewStyle);
    };
 
    fluid.inlineEdit.showNothing = function (that) {
        that.displayView.value("");
 
        // workaround for FLUID-938:
        // IE can not style an empty inline element, so force element to be display: inline-block
        Iif ($.browser.msie) {
            if (that.viewEl.css("display") === "inline") {
                that.viewEl.css("display", "inline-block");
            }
        }
    };
 
    fluid.inlineEdit.showEditedText = function (that) {
        that.displayView.value(that.model.value);
        fluid.inlineEdit.clearEmptyViewStyles(that.viewEl, that.options.styles, that.existingPadding);
    };
 
    fluid.inlineEdit.refreshView = function (that, source) {
        that.displayView.refreshView(that, source);
        if (that.editView) {
            that.editView.refreshView(that, source);
        }
    };
 
    fluid.inlineEdit.updateModelValue = function (that, newValue, source) {
        var comparator = that.options.modelComparator;
        var unchanged = comparator ? comparator(that.model.value, newValue) :
            that.model.value === newValue;
        if (!unchanged) {
            var oldModel = $.extend(true, {}, that.model);
            that.model.value = newValue;
            that.events.modelChanged.fire(that.model, oldModel, source);
            that.refreshView(source);
        }
    };
 
    fluid.inlineEdit.editHandler = function (that) {
        var prevent = that.events.onBeginEdit.fire();
        Iif (prevent === false) {
            return false;
        }
        fluid.inlineEdit.edit(that);
 
        return true;
    };
 
    // Initialize the tooltip once the document is ready.
    // For more details, see http://issues.fluidproject.org/browse/FLUID-1030
    fluid.inlineEdit.initTooltips = function (that) {
        var tooltipOptions = {
            content: that.options.tooltipText,
            position: {
                my: "left top",
                at: "left bottom+25%", // add a 25% offset to keep the tooltip from overlapping the element it is for
                // setting the "of" property to ensure that the tooltip is positioned relative to that.viewEl
                // even when keyboard focus is on that.textEditButton
                of: that.viewEl
            },
            target: "*",
            delay: that.options.tooltipDelay,
            styles: {
                tooltip: that.options.styles.tooltip
            }
        };
 
        fluid.tooltip(that.viewEl, tooltipOptions);
 
        if (that.textEditButton) {
            fluid.tooltip(that.textEditButton, tooltipOptions);
        }
    };
 
    fluid.inlineEdit.calculateInitialPadding = function (viewEl) {
        var padding = viewEl.css("padding-right");
        return padding ? parseFloat(padding) : 0;
    };
 
    /**
     * Set up and style the edit field.  If an edit field is not provided,
     * default markup is created for the edit field
     *
     * @param {String} editStyle - The default styling for the edit field.
     * @param {Object} editField - The existing edit field.
     * @param {Object} editFieldMarkup - The edit field markup provided by the integrator.
     *
     * @return {Object} The styled edit field.
     */
    fluid.inlineEdit.setupEditField = function (editStyle, editField, editFieldMarkup) {
        var eField = $(editField);
        eField = eField.length ? eField : $(editFieldMarkup);
        eField.addClass(editStyle);
        return eField;
    };
 
    /**
     * Set up the edit container and append the edit field to the container.  If an edit container
     * is not provided, default markup is created.
     *
     * @param {Object} displayContainer - The display mode container
     * @param {Object} editField - The edit field that is to be appended to the edit container
     * @param {Object} editContainer - The edit container markup provided by the integrator
     * @param {Object} editContainerMarkup - The edit container markup provided by the integrator.
     *
     * @return {Object} The edit container containing the edit field
     */
    fluid.inlineEdit.setupEditContainer = function (displayContainer, editField, editContainer, editContainerMarkup) {
        var eContainer = $(editContainer);
        eContainer = eContainer.length ? eContainer : $(editContainerMarkup);
        displayContainer.after(eContainer);
        eContainer.append(editField);
 
        return eContainer;
    };
 
    /**
     * Default renderer for the edit mode view.
     *
     * @param {Object} that - The component itself.
     * @return {Object} An object containing:
     *  - container The edit container containing the edit field
     *  - field The styled edit field
     */
    fluid.inlineEdit.defaultEditModeRenderer = function (that) {
        var editField = fluid.inlineEdit.setupEditField(that.options.styles.edit, that.editField, that.options.markup.editField);
        var editContainer = fluid.inlineEdit.setupEditContainer(that.displayModeRenderer, editField, that.editContainer, that.options.markup.editContainer);
        var editModeInstruction = fluid.inlineEdit.setupEditModeInstruction(that.options.styles.editModeInstruction,
            that.options.strings.editModeInstruction, that.options.markup.editModeInstruction);
 
        var id = fluid.allocateSimpleId(editModeInstruction);
        editField.attr("aria-describedby", id);
 
        fluid.inlineEdit.positionEditModeInstruction(editModeInstruction, editContainer, editField);
 
        // Package up the container and field for the component.
        return {
            container: editContainer,
            field: editField
        };
    };
 
    /** Configures the edit container and view, and uses the component's editModeRenderer to render
     * the edit container.
     *
     * @param {Object} that - The component itself.
     * @param {Boolean} lazyEditView - If true, will delay rendering of the edit container; Default is false
     */
    fluid.inlineEdit.renderEditContainer = function (that, lazyEditView) {
        that.editContainer = that.locate("editContainer");
        that.editField = that.locate("edit");
        if (that.editContainer.length !== 1) {
            Iif (that.editContainer.length > 1) {
                fluid.fail("InlineEdit did not find a unique container for selector " + that.options.selectors.editContainer + ": " + fluid.dumpEl(that.editContainer));
            }
        }
 
        Iif (!lazyEditView) {
            return;
        } // do not invoke the renderer, unless this is the "final" effective time
 
        var editElms = that.options.editModeRenderer(that);
        Eif (editElms) {
            that.editContainer = editElms.container;
            that.editField = editElms.field;
        }
    };
 
    /** Set up the edit mode instruction with aria in edit mode
     * @param {String} editModeInstructionStyle - The default styling for the instruction
     * @param {String} editModeInstructionText - The default instruction text
     * @param {Object} editModeInstructionMarkup - The markup to modify.
     * @return {jQuery} The displayed instruction in edit mode
     */
    fluid.inlineEdit.setupEditModeInstruction = function (editModeInstructionStyle, editModeInstructionText, editModeInstructionMarkup) {
        var editModeInstruction = $(editModeInstructionMarkup);
        editModeInstruction.addClass(editModeInstructionStyle);
        editModeInstruction.text(editModeInstructionText);
 
        return editModeInstruction;
    };
 
    /**
     * Positions the edit mode instruction directly beneath the edit container
     *
     * @param {Object} editModeInstruction - The displayed instruction in edit mode
     * @param {Object} editContainer - The edit container in edit mode
     * @param {Object} editField - The edit field in edit mode
     */
    fluid.inlineEdit.positionEditModeInstruction = function (editModeInstruction, editContainer, editField) {
        editContainer.append(editModeInstruction);
 
        editField.focus(function () {
            editModeInstruction.show();
 
            var editFieldPosition = editField.offset();
            // For FLUID-5980 (https://issues.fluidproject.org/browse/FLUID-5980)
            //
            // From the jQuery height docs (http://api.jquery.com/height/)
            // "As of jQuery 1.8, this may require retrieving the CSS height plus
            // box-sizing property and then subtracting any potential border and
            // padding on each element when the element has box-sizing: border-box.
            // To avoid this penalty, use .css( "height" ) rather than .height()."
            var editFieldHeight = parseInt(editField.css("height"), 10);
            editModeInstruction.css({left: editFieldPosition.left});
            editModeInstruction.css({top: editFieldPosition.top + editFieldHeight + 5});
        });
    };
 
    /**
     * Set up and style the display mode container for the viewEl and the textEditButton
     *
     * @param {Object} styles - The default styling for the display mode container
     * @param {Object} displayModeWrapper - The markup used to generate the display mode container
     *
     * @return {jQuery} The styled display mode container
     */
    fluid.inlineEdit.setupDisplayModeContainer = function (styles, displayModeWrapper) {
        var displayModeContainer = $(displayModeWrapper);
        displayModeContainer = displayModeContainer.length ? displayModeContainer : $("<span></span>");
        displayModeContainer.addClass(styles.displayView);
 
        return displayModeContainer;
    };
 
    /** Retrieve the display text from the DOM.
     * @param {Object} viewEl - The view element.
     * @param {String} textStyle - The classes to apply to the view element.
     * @return {jQuery} The view element.
     */
    fluid.inlineEdit.setupDisplayText = function (viewEl, textStyle) {
        /*  Remove the display from the tab order to prevent users to think they
         *  are able to access the inline edit field, but they cannot since the
         *  keyboard event binding is only on the button.
         */
        viewEl.attr("tabindex", "-1");
        viewEl.addClass(textStyle);
        return viewEl;
    };
 
    /**
     * Set up the textEditButton.  Append a background image with appropriate
     * descriptive text to the button.
     *
     * @param {Object} that - The component itself.
     * @param {Object} model - The model data.
     * @return {jQuery} The accessible button located after the display text
     */
    fluid.inlineEdit.setupTextEditButton = function (that, model) {
        var opts = that.options;
        var textEditButton = that.locate("textEditButton");
 
        if (textEditButton.length === 0) {
            var markup = $(that.options.markup.textEditButton);
            markup.addClass(opts.styles.textEditButton);
            markup.text(opts.tooltipText);
 
            /**
             * Set text for the button and listen
             * for modelChanged to keep it updated
             */
            fluid.inlineEdit.updateTextEditButton(markup, model.value || opts.strings.defaultViewText, opts.strings.textEditButton);
            that.events.modelChanged.addListener(function () {
                fluid.inlineEdit.updateTextEditButton(markup, model.value || opts.strings.defaultViewText, opts.strings.textEditButton);
            });
 
            that.locate("text").after(markup);
 
            // Refresh the textEditButton with the newly appended options
            textEditButton = that.locate("textEditButton");
        }
        return textEditButton;
    };
 
    /**
     * Update the textEditButton text with the current value of the field.
     *
     * @param {Object} textEditButton - the textEditButton
     * @param {String} value - The current value of the inline editable text
     * @param {String} stringTemplate - The string template to use in producing the button text.
     */
    fluid.inlineEdit.updateTextEditButton = function (textEditButton, value, stringTemplate) {
        var buttonText = fluid.stringTemplate(stringTemplate, {
            text: value
        });
        textEditButton.text(buttonText);
    };
 
    /**
     * Bind mouse hover event handler to the display mode container.
     *
     * @param {Object} displayModeRenderer - The display mode container
     * @param {String} invitationStyle - The default styling for the display mode container on mouse hover
     */
    fluid.inlineEdit.bindHoverHandlers = function (displayModeRenderer, invitationStyle) {
        var over = function () {
            displayModeRenderer.addClass(invitationStyle);
        };
        var out = function () {
            displayModeRenderer.removeClass(invitationStyle);
        };
        displayModeRenderer.hover(over, out);
    };
 
    /**
     * Bind keyboard focus and blur event handlers to an element
     *
     * Note: This function is an unsupported, NON-API function
     *
     * @param {Object} element - The element to which the event handlers are bound
     * @param {Object} displayModeRenderer - The display mode container
     * @param {Object} styles - The default styling for the display mode container on mouse hover
     * @param {Object} strings - String messages to use if there is no model value.
     * @param {Object} model - Model data to display.
     */
    fluid.inlineEdit.bindHighlightHandler = function (element, displayModeRenderer, styles, strings, model) {
        element = $(element);
 
        var makeFocusSwitcher = function (focusOn) {
            return function () {
                displayModeRenderer.toggleClass(styles.focus, focusOn);
                displayModeRenderer.toggleClass(styles.invitation, focusOn);
                if (!model || !model.value) {
                    displayModeRenderer.prevObject.text(focusOn ? strings.defaultFocussedViewText : strings.defaultViewText);
                }
            };
        };
        element.focus(makeFocusSwitcher(true));
        element.blur(makeFocusSwitcher(false));
    };
 
    /**
     * Bind mouse click handler to an element
     *
     * @param {Object} element - The element to which the event handler is bound
     * @param {Object} edit - Function to invoke the edit mode
     *
     */
    fluid.inlineEdit.bindMouseHandlers = function (element, edit) {
        element = $(element);
 
        var triggerGuard = fluid.inlineEdit.makeEditTriggerGuard(element, edit);
        element.click(function (e) {
            triggerGuard(e);
            return false;
        });
    };
 
    /**
     * Bind keyboard press handler to an element
     *
     * @param {Object} element - The element to which the event handler is bound
     * @param {Object} edit - Function to invoke the edit mode
     *
     */
    fluid.inlineEdit.bindKeyboardHandlers = function (element, edit) {
        element = $(element);
        element.attr("role", "button");
 
        var guard = fluid.inlineEdit.makeEditTriggerGuard(element, edit);
        fluid.activatable(element, function (event) {
            return guard(event);
        });
    };
 
    /**
     * Creates an event handler that will trigger the edit mode if caused by something other
     * than standard HTML controls. The event handler will return false if entering edit mode.
     *
     * @param {Object} jElement - The element to trigger the edit mode
     * @param {Object} edit - Function to invoke the edit mode
     *
     * @return {Function} The event handler function
     */
    fluid.inlineEdit.makeEditTriggerGuard = function (jElement, edit) {
        var element = fluid.unwrap(jElement);
        return function (event) {
            // FLUID-2017 - avoid triggering edit mode when operating standard HTML controls. Ultimately this
            // might need to be extensible, in more complex authouring scenarios.
            var outer = fluid.findAncestor(event.target, function (elem) {
                Eif (/input|select|textarea|button|a/i.test(elem.nodeName) || elem === element) {
                    return true;
                }
            });
            Eif (outer === element) {
                edit();
                return false;
            }
        };
    };
 
    /* Bind all user-facing event handlers required by the component */
    fluid.inlineEdit.bindEventHandlers = function (that, edit, displayModeContainer) {
        var styles = that.options.styles;
 
        fluid.inlineEdit.bindHoverHandlers(displayModeContainer, styles.invitation);
        fluid.inlineEdit.bindMouseHandlers(that.viewEl, edit);
        fluid.inlineEdit.bindMouseHandlers(that.textEditButton, edit);
        fluid.inlineEdit.bindKeyboardHandlers(that.textEditButton, edit);
        fluid.inlineEdit.bindHighlightHandler(that.viewEl, displayModeContainer, that.options.styles, that.options.strings, that.model);
        fluid.inlineEdit.bindHighlightHandler(that.textEditButton, displayModeContainer, that.options.styles, that.options.strings, that.model);
    };
 
    /** Render the display mode view.
     *
     * @param {Object} that - The component itself.
     * @param {Object} edit - Function to invoke the edit mode
     * @param {Object} model - Model data to display.
     * @return {jQuery} The display container containing the display text and textEditbutton for display mode view.
     */
    fluid.inlineEdit.defaultDisplayModeRenderer = function (that, edit, model) {
        var styles = that.options.styles;
 
        var displayModeWrapper = fluid.inlineEdit.setupDisplayModeContainer(styles);
        var displayModeContainer = that.viewEl.wrap(displayModeWrapper).parent();
 
        that.textEditButton = fluid.inlineEdit.setupTextEditButton(that, model);
        displayModeContainer.append(that.textEditButton);
 
        fluid.inlineEdit.bindEventHandlers(that, edit, displayModeContainer);
 
        return displayModeContainer;
    };
 
    fluid.inlineEdit.getNodeName = function (element) {
        return fluid.unwrap(element).nodeName.toLowerCase();
    };
 
    fluid.defaults("fluid.inlineEdit.standardAccessor", {
        gradeNames: ["fluid.viewComponent"],
        members: {
            nodeName: {
                expander: {
                    funcName: "fluid.inlineEdit.getNodeName",
                    args: "{that}.container"
                }
            }
        },
        invokers: {
            value: {
                funcName: "fluid.inlineEdit.standardAccessor.value",
                args: ["{that}.nodeName", "{that}.container", "{arguments}.0"]
            }
        }
    });
 
    fluid.inlineEdit.standardAccessor.value = function (nodeName, element, newValue) {
        return fluid[nodeName === "input" || nodeName === "textarea" ? "value" : "text"]($(element), newValue);
    };
 
    fluid.defaults("fluid.inlineEdit.standardDisplayView", {
        gradeNames: ["fluid.viewComponent"],
        invokers: {
            refreshView: {
                funcName: "fluid.inlineEdit.standardDisplayView.refreshView",
                args: ["{fluid.inlineEdit}", "{that}.container", "{arguments}.0"]
            }
        }
    });
 
    fluid.inlineEdit.standardDisplayView.refreshView = function (componentThat) {
        if (componentThat.model.value) {
            fluid.inlineEdit.showEditedText(componentThat);
        } else if (componentThat.options.strings.defaultViewText) {
            fluid.inlineEdit.showDefaultViewText(componentThat);
        } else {
            fluid.inlineEdit.showNothing(componentThat);
        }
        // If necessary, pad the view element enough that it will be evident to the user.
        if ($.trim(componentThat.viewEl.text()).length === 0) {
            componentThat.viewEl.addClass(componentThat.options.styles.emptyDefaultViewText);
 
            Eif (componentThat.existingPadding < componentThat.options.paddings.minimumView) {
                componentThat.viewEl.css("padding-right", componentThat.options.paddings.minimumView);
            }
        }
    };
 
    fluid.defaults("fluid.inlineEdit.standardEditView", {
        gradeNames: ["fluid.viewComponent"],
        invokers: {
            refreshView: {
                funcName: "fluid.inlineEdit.standardEditView.refreshView",
                args: ["{fluid.inlineEdit}", "{that}.container", "{arguments}.0"]
            }
        }
    });
 
    fluid.inlineEdit.standardEditView.refreshView = function (componentThat, editField, source) {
        Eif (!source || (editField && editField.index(source) === -1)) {
            componentThat.editView.value(componentThat.model.value);
        }
    };
 
    fluid.inlineEdit.setup = function (that) {
        // Hide the edit container to start
        Eif (that.editContainer) {
            that.editContainer.hide();
        }
 
        // Add tooltip handler if required and available
        Eif (that.tooltipEnabled()) {
            fluid.inlineEdit.initTooltips(that);
        }
    };
 
 
    // TODO: Should really be part of a "collateral" or "shadow model"
    fluid.inlineEdit.setIsEditing = function (that, state) {
        that.isEditingState = state;
    };
 
    fluid.inlineEdit.tooltipEnabled = function (useTooltip) {
        return useTooltip && $.fn.tooltip;
    };
 
    // Backwards compatibility for users of the 1.4.x and below Infusion API - new users are recommended to directly attach
    // a "fluid.undo" as a subcomponent with appropriate configuration - express this using FLUID-5022 system when it is available
    fluid.inlineEdit.processUndoDecorator = function (that) {
        if (that.options.componentDecorators) {
            var decorators = fluid.makeArray(that.options.componentDecorators);
            var decorator = decorators[0];
            Eif (typeof(decorator) === "string") {
                decorator = {type: decorator};
            }
            Eif (decorator.type === "fluid.undoDecorator") {
                fluid.set(that.options, ["components", "undo"], { type: "fluid.undo", options: decorator.options});
                that.decorators = [ fluid.initDependent(that, "undo")];
            }
        }
    };
 
    /**
     * Instantiates a new Inline Edit component
     *
     * @param {Object} componentContainer - a selector, jQuery, or a DOM element representing the component's container
     * @param {Object} options - a collection of options settings
     */
 
    fluid.defaults("fluid.inlineEdit", {
        gradeNames: ["fluid.undoable", "fluid.viewComponent"],
        mergePolicy: {
            "strings.defaultViewText": "defaultViewText"
        },
        members: {
            isEditingState: false,
            viewEl: {
                expander: {
                    funcName: "fluid.inlineEdit.setupDisplayText",
                    args: ["{that}.dom.text", "{that}.options.styles.text"]
                }
            },
            existingPadding: {
                expander: {
                    funcName: "fluid.inlineEdit.calculateInitialPadding",
                    args: "{that}.viewEl"
                }
            },
            displayModeRenderer: {
                expander: {
                    func: "{that}.options.displayModeRenderer",
                    args: ["{that}", "{that}.edit", "{that}.model"]
                }
            }
        },
        invokers: {
            /** Switches to edit mode. */
            edit: {
                funcName: "fluid.inlineEdit.editHandler",
                args: "{that}"
            },
            /** Determines if the component is currently in edit mode.
              * @return true if edit mode shown, false if view mode is shown
              */
            isEditing: {
                funcName: "fluid.identity",
                args: "{that}.isEditingState"
            },
            /** Finishes editing, switching back to view mode. */
            finish: {
                funcName: "fluid.inlineEdit.finish",
                args: "{that}"
            },
            /** Cancels the in-progress edit and switches back to view mode */
            cancel: {
                funcName: "fluid.inlineEdit.cancel",
                args: "{that}"
            },
            /** Determines if the tooltip feature is enabled.
              * @return true if the tooltip feature is turned on, false if not
              */
            tooltipEnabled: {
                funcName: "fluid.inlineEdit.tooltipEnabled",
                args: "{that}.options.useTooltip"
            },
            /** Updates the state of the inline editor in the DOM, based on changes that may have
              * happened to the model.
              * @param {Object} [source] - An optional source object identifying the source of the change (see ChangeApplier documentation)
              */
            refreshView: {
                funcName: "fluid.inlineEdit.refreshView",
                args: ["{that}", "{arguments}.0"]
            },
            /** Pushes external changes to the model into the inline editor, refreshing its
              * rendering in the DOM. The modelChanged event will fire.
              * @param {String} newValue - The bare value of the model, that is, the string being edited
              * @param {Object} [source] - An optional "source" (perhaps a DOM element) which triggered this event
              */
            updateModelValue: {
                funcName: "fluid.inlineEdit.updateModelValue",
                args: ["{that}", "{arguments}.0", "{arguments}.1"] // newValue, source
            },
            /** Pushes external changes to the model into the inline editor, refreshing its
              * rendering in the DOM. The modelChanged event will fire. This honours the "fluid.undoable" contract
              * @param {Object} newValue - The full value of the new model, that is, a model object which contains the editable value as the element named "value"
              * @param {Object} [source] - An optional "source" (perhaps a DOM element) which triggered this event
              */
            updateModel: {
                funcName: "fluid.inlineEdit.updateModelValue",
                args: ["{that}", "{arguments}.0.value", "{arguments}.1"] // newModel, source
            }
        },
        components: {
            displayView: {
                type: "{that}.options.displayView.type",
                container: "{that}.viewEl",
                options: {
                    gradeNames: "{fluid.inlineEdit}.options.displayAccessor.type"
                }
            },
            editView: {
                type: "{that}.options.editView.type",
                createOnEvent: "onCreateEditView",
                container: "{that}.editField",
                options: {
                    gradeNames: "{fluid.inlineEdit}.options.editAccessor.type"
                }
            }
        },
        model: {
            value: {
                expander: { func: "{that}.displayView.value"}
            }
        },
        selectors: {
            text: ".flc-inlineEdit-text",
            editContainer: ".flc-inlineEdit-editContainer",
            edit: ".flc-inlineEdit-edit",
            textEditButton: ".flc-inlineEdit-textEditButton"
        },
 
        styles: {
            text: "fl-inlineEdit-text",
            edit: "fl-inlineEdit-edit",
            invitation: "fl-inlineEdit-invitation",
            defaultViewStyle: "fl-inlineEdit-emptyText-invitation",
            emptyDefaultViewText: "fl-inlineEdit-emptyDefaultViewText",
            focus: "fl-inlineEdit-focus",
            tooltip: "fl-inlineEdit-tooltip",
            editModeInstruction: "fl-inlineEdit-editModeInstruction",
            displayView: "fl-inlineEdit-simple-editableText fl-inlineEdit-textContainer",
            textEditButton: "fl-hidden-accessible"
        },
 
        events: {
            modelChanged: null,
            onBeginEdit: "preventable",
            afterBeginEdit: null,
            onFinishEdit: "preventable",
            afterFinishEdit: null,
            afterInitEdit: null,
            onCreateEditView: null
        },
        listeners: {
            onCreate: [{
                func: "{that}.refreshView"
            }, {
                funcName: "fluid.inlineEdit.initializeEditView",
                args: ["{that}", true]
            }, {
                funcName: "fluid.inlineEdit.setup",
                args: "{that}"
            }, {
                funcName: "fluid.inlineEdit.processUndoDecorator",
                args: "{that}"
            }],
            onBeginEdit: {
                funcName: "fluid.inlineEdit.setIsEditing",
                args: ["{that}", true]
            },
            afterFinishEdit: {
                funcName: "fluid.inlineEdit.setIsEditing",
                args: ["{that}", false]
            }
        },
 
        strings: {
            textEditButton: "Edit text %text",
            editModeInstruction: "Escape to cancel, Enter or Tab when finished",
            defaultViewText: "Click here to edit", /* this will override the direct option */
            defaultFocussedViewText: "Click here or press enter to edit"
        },
 
        markup: {
            editField: "<input type='text' class='flc-inlineEdit-edit'/>",
            editContainer: "<span></span>",
            editModeInstruction: "<p></p>",
            textEditButton: "<a href='#_' class='flc-inlineEdit-textEditButton'></a>"
        },
 
        paddings: {
            edit: 10,
            minimumEdit: 80,
            minimumView: 60
        },
 
        applyEditPadding: true,
 
        blurHandlerBinder: null,
 
        // set this to true or false to cause unconditional submission, otherwise it will
        // be inferred from the edit element tag type.
        submitOnEnter: undefined,
 
        modelComparator: null,
 
        displayAccessor: {
            type: "fluid.inlineEdit.standardAccessor"
        },
 
        displayView: {
            type: "fluid.inlineEdit.standardDisplayView"
        },
 
        editAccessor: {
            type: "fluid.inlineEdit.standardAccessor"
        },
 
        editView: {
            type: "fluid.inlineEdit.standardEditView"
        },
 
        displayModeRenderer: fluid.inlineEdit.defaultDisplayModeRenderer,
 
        editModeRenderer: fluid.inlineEdit.defaultEditModeRenderer,
 
        lazyEditView: false,
 
        /** View Mode Tooltip Settings **/
        useTooltip: true,
 
        // this is here for backwards API compatibility, but should be in the strings block
        tooltipText: "Select or press Enter to edit",
 
        tooltipDelay: 1000,
 
        selectOnEdit: false
    });
 
    /*
     * Creates a whole list of inline editors as subcomponents of the supplied component
     */
    fluid.setupInlineEdits = function (that, editables) {
        // TODO: create useful framework for automated construction of component definitions, possibly using Model Transformation - FLUID-5022
        return fluid.transform(editables, function (editable, i) {
            var componentDef = {
                type: "fluid.inlineEdit",
                container: editable
            };
            var name = "inlineEdit-" + i;
            fluid.set(that.options, ["components", name], componentDef);
            return fluid.initDependent(that, name);
 
        });
    };
 
    fluid.defaults("fluid.inlineEditsComponent", {
        gradeNames: ["fluid.viewComponent"],
        distributeOptions: {
            source: "{that}.options",
            // TODO: Appalling requirement to evade FLUID-5887 check - otherwise all of this fluid.modelComponent material is broadcast down to each component.
            // "source" distributions are silly and dangerous in any case, but they have become fairly widely used, together with the expectation that the
            // material from "defaults" can be broadcast too. But clearly material that is from base grade defaults is unwelcome to be distributed.
            // This seems to imply that we've got no option but to start supporting "provenance" in options and defaults - highly expensive.
            exclusions: ["members.inlineEdits", "members.modelRelay", "members.applier", "members.model", "selectors.editables", "events"],
            removeSource: true,
            target: "{that > fluid.inlineEdit}.options"
        },
        members: {
            inlineEdits: {
                expander: {
                    funcName: "fluid.setupInlineEdits",
                    args: ["{that}", "{that}.dom.editables"]
                }
            }
        },
        selectors: {
            editables: ".flc-inlineEditable"
        }
    });
 
    fluid.inlineEdits = function (container, options) {
        var that = fluid.inlineEditsComponent(container, options);
        return that.inlineEdits;
    };
 
})(jQuery, fluid_3_0_0);