OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('options', function() { | 5 cr.define('options', function() { |
6 /** @const */ var DeletableItem = options.DeletableItem; | 6 /** @const */ var DeletableItem = options.DeletableItem; |
7 /** @const */ var DeletableItemList = options.DeletableItemList; | 7 /** @const */ var DeletableItemList = options.DeletableItemList; |
8 | 8 |
9 /** | 9 /** |
10 * Creates a new list item with support for inline editing. | 10 * Creates a new list item with support for inline editing. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 DeletableItem.prototype.decorate.call(this); | 78 DeletableItem.prototype.decorate.call(this); |
79 | 79 |
80 this.editFields_ = []; | 80 this.editFields_ = []; |
81 this.addEventListener('mousedown', this.handleMouseDown_); | 81 this.addEventListener('mousedown', this.handleMouseDown_); |
82 this.addEventListener('keydown', this.handleKeyDown_); | 82 this.addEventListener('keydown', this.handleKeyDown_); |
83 this.addEventListener('focusin', this.handleFocusIn_); | 83 this.addEventListener('focusin', this.handleFocusIn_); |
84 }, | 84 }, |
85 | 85 |
86 /** @override */ | 86 /** @override */ |
87 selectionChanged: function() { | 87 selectionChanged: function() { |
88 this.updateEditState(); | 88 if (!this.parentNode.eventsDisabled) |
89 this.updateEditState(); | |
89 }, | 90 }, |
90 | 91 |
91 /** | 92 /** |
92 * Called when this element gains or loses 'lead' status. Updates editing | 93 * Called when this element gains or loses 'lead' status. Updates editing |
93 * mode accordingly. | 94 * mode accordingly. |
94 */ | 95 */ |
95 updateLeadState: function() { | 96 updateLeadState: function() { |
96 // Add focusability before call to updateEditState. | 97 // Add focusability before call to updateEditState. |
97 if (this.lead) { | 98 if (this.lead) { |
98 this.setEditableValuesFocusable(true); | 99 this.setEditableValuesFocusable(true); |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 | 571 |
571 /** | 572 /** |
572 * @constructor | 573 * @constructor |
573 * @extends {options.DeletableItemList} | 574 * @extends {options.DeletableItemList} |
574 */ | 575 */ |
575 var InlineEditableItemList = cr.ui.define('list'); | 576 var InlineEditableItemList = cr.ui.define('list'); |
576 | 577 |
577 InlineEditableItemList.prototype = { | 578 InlineEditableItemList.prototype = { |
578 __proto__: DeletableItemList.prototype, | 579 __proto__: DeletableItemList.prototype, |
579 | 580 |
581 // Event handling is disabled if > 0. | |
582 // @type {number} | |
James Hawkins
2015/01/06 18:54:51
It's legit now to do:
// @private {number}
But t
bondd
2015/01/06 23:07:24
Done. I made it a JSDoc block, but left it as
*
| |
583 // @private | |
584 eventsDisabledCount_: 0, | |
585 | |
580 /** | 586 /** |
581 * Focuses the input element of the placeholder if true. | 587 * Focuses the input element of the placeholder if true. |
582 * @type {boolean} | 588 * @type {boolean} |
583 * @private | 589 * @private |
584 */ | 590 */ |
585 needsToFocusPlaceholder_: false, | 591 needsToFocusPlaceholder_: false, |
586 | 592 |
587 /** @override */ | 593 /** @override */ |
588 decorate: function() { | 594 decorate: function() { |
589 DeletableItemList.prototype.decorate.call(this); | 595 DeletableItemList.prototype.decorate.call(this); |
590 this.setAttribute('inlineeditable', ''); | 596 this.setAttribute('inlineeditable', ''); |
591 this.addEventListener('hasElementFocusChange', | 597 this.addEventListener('hasElementFocusChange', |
592 this.handleListFocusChange_); | 598 this.handleListFocusChange_); |
593 // <list> isn't focusable by default, but cr.ui.List defaults tabindex to | 599 // <list> isn't focusable by default, but cr.ui.List defaults tabindex to |
594 // 0 if it's not set. | 600 // 0 if it's not set. |
595 this.tabIndex = -1; | 601 this.tabIndex = -1; |
596 }, | 602 }, |
597 | 603 |
598 /** | 604 /** |
605 * Whether events are disabled. | |
606 * Used to modify the list without triggering a bunch of unnecessary events. | |
607 * @type {boolean} | |
608 */ | |
609 get eventsDisabled() { | |
610 return this.eventsDisabledCount_ > 0; | |
611 }, | |
612 set eventsDisabled(disabled) { | |
613 this.eventsDisabledCount_ += disabled ? 1 : -1; | |
614 }, | |
615 | |
616 /** | |
599 * Called when the list hierarchy as a whole loses or gains focus; starts | 617 * Called when the list hierarchy as a whole loses or gains focus; starts |
600 * or ends editing for the lead item if necessary. | 618 * or ends editing for the lead item if necessary. |
601 * @param {Event} e The change event. | 619 * @param {Event} e The change event. |
602 * @private | 620 * @private |
603 */ | 621 */ |
604 handleListFocusChange_: function(e) { | 622 handleListFocusChange_: function(e) { |
623 if (this.eventsDisabled) | |
624 return; | |
625 | |
605 var leadItem = this.getListItemByIndex(this.selectionModel.leadIndex); | 626 var leadItem = this.getListItemByIndex(this.selectionModel.leadIndex); |
606 if (leadItem) { | 627 if (leadItem) { |
607 if (e.newValue) { | 628 if (e.newValue) { |
608 // Add focusability before making other changes. | 629 // Add focusability before making other changes. |
609 leadItem.setEditableValuesFocusable(true); | 630 leadItem.setEditableValuesFocusable(true); |
610 leadItem.setCloseButtonFocusable(true); | 631 leadItem.setCloseButtonFocusable(true); |
611 leadItem.focusedColumnIndex = -1; | 632 leadItem.focusedColumnIndex = -1; |
612 leadItem.updateEditState(); | 633 leadItem.updateEditState(); |
613 // Remove focusability after making other changes. | 634 // Remove focusability after making other changes. |
614 leadItem.setStaticValuesFocusable(false); | 635 leadItem.setStaticValuesFocusable(false); |
615 } else { | 636 } else { |
616 // Add focusability before making other changes. | 637 // Add focusability before making other changes. |
617 leadItem.setStaticValuesFocusable(true); | 638 leadItem.setStaticValuesFocusable(true); |
618 leadItem.setCloseButtonFocusable(true); | 639 leadItem.setCloseButtonFocusable(true); |
619 leadItem.editing = false; | 640 leadItem.editing = false; |
620 // Remove focusability after making other changes. | 641 // Remove focusability after making other changes. |
621 if (!leadItem.isPlaceholder) | 642 if (!leadItem.isPlaceholder) |
622 leadItem.setEditableValuesFocusable(false); | 643 leadItem.setEditableValuesFocusable(false); |
623 } | 644 } |
624 } | 645 } |
625 }, | 646 }, |
626 | 647 |
627 /** @override */ | 648 /** @override */ |
628 handleLeadChange: function(e) { | 649 handleLeadChange: function(e) { |
650 if (this.eventsDisabled) | |
651 return; | |
652 | |
629 DeletableItemList.prototype.handleLeadChange.call(this, e); | 653 DeletableItemList.prototype.handleLeadChange.call(this, e); |
630 | 654 |
631 var focusedColumnIndex = -1; | 655 var focusedColumnIndex = -1; |
632 if (e.oldValue != -1) { | 656 if (e.oldValue != -1) { |
633 var element = this.getListItemByIndex(e.oldValue); | 657 var element = this.getListItemByIndex(e.oldValue); |
634 if (element) { | 658 if (element) { |
635 focusedColumnIndex = element.focusedColumnIndex; | 659 focusedColumnIndex = element.focusedColumnIndex; |
636 element.updateLeadState(); | 660 element.updateLeadState(); |
637 } | 661 } |
638 } | 662 } |
(...skipping 19 matching lines...) Expand all Loading... | |
658 if (item) { | 682 if (item) { |
659 item.setStaticValuesFocusable(true); | 683 item.setStaticValuesFocusable(true); |
660 item.setCloseButtonFocusable(true); | 684 item.setCloseButtonFocusable(true); |
661 if (item.isPlaceholder) | 685 if (item.isPlaceholder) |
662 item.setEditableValuesFocusable(true); | 686 item.setEditableValuesFocusable(true); |
663 } | 687 } |
664 } | 688 } |
665 }, | 689 }, |
666 | 690 |
667 /** | 691 /** |
692 * Set the selected index without changing the focused element on the page. | |
693 * Used to change the selected index when the list doesn't have focus (and | |
694 * doesn't want to take focus). | |
695 */ | |
696 setSelectedIndexWithoutFocusing: function(index) { | |
697 this.eventsDisabled = true; | |
698 // Remove focusability from old item. | |
699 var oldItem = this.getListItemByIndex(this.selectionModel.leadIndex) || | |
700 this.getInitialFocusableItem(); | |
701 if (oldItem) { | |
702 oldItem.setEditableValuesFocusable(false); | |
703 oldItem.setStaticValuesFocusable(false); | |
704 oldItem.setCloseButtonFocusable(false); | |
705 oldItem.lead = false; | |
706 } | |
707 | |
708 this.selectionModel.selectedIndex = index; | |
709 // Add focusability to new item. | |
710 var newItem = this.getListItemByIndex(index); | |
711 if (newItem) { | |
712 if (newItem.isPlaceholder) | |
713 newItem.setEditableValuesFocusable(true); | |
714 else | |
715 newItem.setStaticValuesFocusable(true); | |
716 | |
717 newItem.setCloseButtonFocusable(true); | |
718 newItem.lead = true; | |
719 } | |
720 this.eventsDisabled = false; | |
721 }, | |
722 | |
723 /** | |
668 * Focus the placeholder's first input field. | 724 * Focus the placeholder's first input field. |
669 * Should only be called immediately after the list has been repopulated. | 725 * Should only be called immediately after the list has been repopulated. |
670 */ | 726 */ |
671 focusPlaceholder: function() { | 727 focusPlaceholder: function() { |
672 // Remove focusability from initial item. | 728 // Remove focusability from initial item. |
673 var item = this.getInitialFocusableItem(); | 729 var item = this.getInitialFocusableItem(); |
674 if (item) { | 730 if (item) { |
675 item.setStaticValuesFocusable(false); | 731 item.setStaticValuesFocusable(false); |
676 item.setCloseButtonFocusable(false); | 732 item.setCloseButtonFocusable(false); |
677 } | 733 } |
(...skipping 29 matching lines...) Expand all Loading... | |
707 this.getListItemByIndex(0)); | 763 this.getListItemByIndex(0)); |
708 }, | 764 }, |
709 }; | 765 }; |
710 | 766 |
711 // Export | 767 // Export |
712 return { | 768 return { |
713 InlineEditableItem: InlineEditableItem, | 769 InlineEditableItem: InlineEditableItem, |
714 InlineEditableItemList: InlineEditableItemList, | 770 InlineEditableItemList: InlineEditableItemList, |
715 }; | 771 }; |
716 }); | 772 }); |
OLD | NEW |