@@ -777,6 +777,121 @@ module.service('gridUtil', ['$log', '$window', '$document', '$http', '$templateC
777
777
778
778
} ;
779
779
780
+ /**
781
+ * @ngdoc object
782
+ * @name focus
783
+ * @propertyOf ui.grid.service:GridUtil
784
+ * @description Provies a set of methods to set the document focus inside the grid.
785
+ * See {@link ui.grid.service:GridUtil.focus} for more information.
786
+ */
787
+
788
+ /**
789
+ * @ngdoc object
790
+ * @name ui.grid.service:GridUtil.focus
791
+ * @description Provies a set of methods to set the document focus inside the grid.
792
+ * Timeouts are utilized to ensure that the focus is invoked after any other event has been triggered.
793
+ * e.g. click events that need to run before the focus or
794
+ * inputs elements that are in a disabled state but are enabled when those events
795
+ * are triggered.
796
+ */
797
+ s . focus = {
798
+ queue : [ ] ,
799
+ //http://stackoverflow.com/questions/25596399/set-element-focus-in-angular-way
800
+ /**
801
+ * @ngdoc method
802
+ * @methodOf ui.grid.service:GridUtil.focus
803
+ * @name byId
804
+ * @description Sets the focus of the document to the given id value.
805
+ * If provided with the grid object it will automatically append the grid id.
806
+ * This is done to encourage unique dom id's as it allows for multiple grids on a
807
+ * page.
808
+ * @param {String } id the id of the dom element to set the focus on
809
+ * @param {Object= } Grid the grid object for this grid instance. See: {@link ui.grid.class:Grid}
810
+ * @param {Number } Grid.id the unique id for this grid. Already set on an initialized grid object.
811
+ * @returns {Promise } The `$timeout` promise that will be resolved once focus is set. If another focus is requested before this request is evaluated.
812
+ * then the promise will fail with the `'canceled'` reason.
813
+ */
814
+ byId : function ( id , Grid ) {
815
+ this . _purgeQueue ( ) ;
816
+ var promise = $timeout ( function ( ) {
817
+ var elementID = ( Grid && Grid . id ? Grid . id + '-' : '' ) + id ;
818
+ var element = $window . document . getElementById ( elementID ) ;
819
+ if ( element ) {
820
+ element . focus ( ) ;
821
+ } else {
822
+ s . logWarn ( '[focus.byId] Element id ' + elementID + ' was not found.' ) ;
823
+ }
824
+ } ) ;
825
+ this . queue . push ( promise ) ;
826
+ return promise ;
827
+ } ,
828
+
829
+ /**
830
+ * @ngdoc method
831
+ * @methodOf ui.grid.service:GridUtil.focus
832
+ * @name byElement
833
+ * @description Sets the focus of the document to the given dom element.
834
+ * @param {(element|angular.element) } element the DOM element to set the focus on
835
+ * @returns {Promise } The `$timeout` promise that will be resolved once focus is set. If another focus is requested before this request is evaluated.
836
+ * then the promise will fail with the `'canceled'` reason.
837
+ */
838
+ byElement : function ( element ) {
839
+ if ( ! angular . isElement ( element ) ) {
840
+ s . logWarn ( "Trying to focus on an element that isn\'t an element." ) ;
841
+ return $q . reject ( 'not-element' ) ;
842
+ }
843
+ element = angular . element ( element ) ;
844
+ this . _purgeQueue ( ) ;
845
+ var promise = $timeout ( function ( ) {
846
+ if ( element ) {
847
+ element [ 0 ] . focus ( ) ;
848
+ }
849
+ } ) ;
850
+ this . queue . push ( promise ) ;
851
+ return promise ;
852
+ } ,
853
+ /**
854
+ * @ngdoc method
855
+ * @methodOf ui.grid.service:GridUtil.focus
856
+ * @name bySelector
857
+ * @description Sets the focus of the document to the given dom element.
858
+ * @param {(element|angular.element) } parentElement the parent/ancestor of the dom element that you are selecting using the query selector
859
+ * @param {String } querySelector finds the dom element using the {@link http://www.w3schools.com/jsref/met_document_queryselector.asp querySelector}
860
+ * @param {boolean } [aSync=false] If true then the selector will be querried inside of a timeout. Otherwise the selector will be querried imidately
861
+ * then the focus will be called.
862
+ * @returns {Promise } The `$timeout` promise that will be resolved once focus is set. If another focus is requested before this request is evaluated.
863
+ * then the promise will fail with the `'canceled'` reason.
864
+ */
865
+ bySelector : function ( parentElement , querySelector , aSync ) {
866
+ var self = this ;
867
+ if ( ! angular . isElement ( parentElement ) ) {
868
+ throw new Error ( "The parent element is not an element." ) ;
869
+ }
870
+ // Ensure that this is an angular element.
871
+ // It is fine if this is already an angular element.
872
+ parentElement = angular . element ( parentElement ) ;
873
+ var focusBySelector = function ( ) {
874
+ var element = parentElement [ 0 ] . querySelector ( querySelector ) ;
875
+ return self . byElement ( element ) ;
876
+ } ;
877
+ this . _purgeQueue ( ) ;
878
+ if ( aSync ) { //Do this asynchronysly
879
+ var promise = $timeout ( focusBySelector ) ;
880
+ this . queue . push ( $timeout ( focusBySelector ) ) ;
881
+ return promise ;
882
+ } else {
883
+ return focusBySelector ( ) ;
884
+ }
885
+ } ,
886
+ _purgeQueue : function ( ) {
887
+ this . queue . forEach ( function ( element ) {
888
+ $timeout . cancel ( element ) ;
889
+ } ) ;
890
+ this . queue = [ ] ;
891
+ }
892
+ } ;
893
+
894
+
780
895
[ 'width' , 'height' ] . forEach ( function ( name ) {
781
896
var capsName = angular . uppercase ( name . charAt ( 0 ) ) + name . substr ( 1 ) ;
782
897
s [ 'element' + capsName ] = function ( elem , extra ) {
0 commit comments