|
| 1 | +/** |
| 2 | + * @ngdoc directive |
| 3 | + * @name patternfly.form.directive:pfRemainingCharsCount |
| 4 | + * |
| 5 | + * @description |
| 6 | + * Directive for showing a characters remaining count and triggering warning and error</br> |
| 7 | + * behavior when passing specified thresholds. When the <code>chars-warn-remaining</code> threshold is passed, </br> |
| 8 | + * the <code>chars-warn-remaining-pf</code> css class is applied to the <code>count-fld</code>, which by default, turns </br> |
| 9 | + * the remaining count number <font color='red'>red</font>.</br> |
| 10 | + * By default, characters may be entered into the text field after the <code>chars-max-limit</code> limit has been reached,</br> |
| 11 | + * the remaining count number will become a negative value. Setting the <code>blockInputAtMaxLimit</code> to <em>true</em>,</br> |
| 12 | + * will block additional input into the text field after the max has been reached; additionally a right-click 'paste' will only </br> |
| 13 | + * paste characters until the maximum character limit is reached. |
| 14 | + * |
| 15 | + * @param {string} ng-model The scope model variable which contains the initial text for the text field. Required, but</br> |
| 16 | + * can be an emptly string (""). |
| 17 | + * @param {string} count-fld The id of the field to display the 'characters-remaining' count. |
| 18 | + * @param {string} chars-max-limit Number representing the maximum number of characters to allow before dispatching a<br/> |
| 19 | + * 'overCharsMaxLimit' event. When the number of characters falls below <code>chars-max-limit</code>, a 'underCharsMaxLimit'<br/> |
| 20 | + * event is dispatched. |
| 21 | + * @param {string} chars-warn-remaining Number of remaining characters to warn upon. The 'chars-warn-remaining-pf'<br/> |
| 22 | + * class will be applied to the <code>count-fld</code> when the remaining characters is less than the<br/> |
| 23 | + * <code>chars-warn-remaining</code> threshold. When/if the number of remaining characters becomes greater than the<br/> |
| 24 | + * <code>chars-warn-remaining</code> threshold, the 'chars-warn-remaining-pf' class is removed from the <code>count-fld</code> field. |
| 25 | + * @param {boolean=} block-input-at-max-limit If true, no more characters can be entered into the text field when the<br/> |
| 26 | + * <code>chars-max-limit</code> has been reached. If false (the default), characters may be entered into the text field after the<br/> |
| 27 | + * max. limit has been reached, but these additional characters will trigger the 'overCharsMaxLimit' event to be<br/> |
| 28 | + * dispatched. When <code>blockInputAtMaxLimit</code> is <em>true</em>, a right-click 'paste' will only paste<br/> |
| 29 | + * characters until the maximum character limit is reached. |
| 30 | + * |
| 31 | + * @example |
| 32 | + <example module="patternfly.example"> |
| 33 | + <file name="index.html"> |
| 34 | + <div ng-controller="DemoCtrl" style="display:inline-block; width: 100%;"> |
| 35 | +
|
| 36 | + <style> |
| 37 | + textarea { |
| 38 | + resize: none; |
| 39 | + } |
| 40 | + </style> |
| 41 | +
|
| 42 | + <div class="container"> |
| 43 | + <strong>Max limit: 20, warn when 5 or less remaining, disable button after max limit</strong> |
| 44 | + <div class="row"> |
| 45 | + <div class="col-md-4"> |
| 46 | +
|
| 47 | + <form> |
| 48 | + <div class="form-group"> |
| 49 | + <label for="messageArea"></label> |
| 50 | + <textarea class="form-control" pf-remaining-chars-count id="messageArea_1" ng-model="messageArea1text" chars-max-limit="20" chars-warn-remaining="5" |
| 51 | + count-fld="charRemainingCntFld_1" name="text" placeholder="Type in your message" rows="5"></textarea> |
| 52 | + </div> |
| 53 | + <span class="pull-right chars-remaining-pf"> |
| 54 | + <span id="charRemainingCntFld_1"></span> |
| 55 | + <button id="postBtn_1" ng-disabled="charsMaxLimitExceeded" type="submit" class="btn btn-default">Post New Message</button> |
| 56 | + </span> |
| 57 | + </form> |
| 58 | +
|
| 59 | + </div> |
| 60 | + </div> |
| 61 | + <br> |
| 62 | + <strong>Max limit: 10, warn when 2 or less remaining, block input after max limit</strong> |
| 63 | + <div class="row"> |
| 64 | + <div class="col-md-4"> |
| 65 | + <form> |
| 66 | + <div class="form-group"> |
| 67 | + <label for="messageArea"></label> |
| 68 | + <textarea class="form-control" pf-remaining-chars-count id="messageArea_2" ng-model="messageArea2text" chars-max-limit="10" chars-warn-remaining="2" |
| 69 | + block-input-at-max-limit="true" count-fld="charRemainingCntFld_2" name="text" |
| 70 | + placeholder="Type in your message" rows="5"></textarea> |
| 71 | + </div> |
| 72 | + <span class="pull-left"> |
| 73 | + <button id="postBtn_2" type="submit" class="btn btn-default">Submit</button> |
| 74 | + </span> |
| 75 | + <span class="pull-right chars-remaining-pf"> |
| 76 | + <span id="charRemainingCntFld_2"></span> |
| 77 | + </span> |
| 78 | + </form> |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + <br> |
| 82 | + <strong>Max limit: 10, warn when 5 or less remaining, block input after max limit</strong> |
| 83 | + <div class="row"> |
| 84 | + <div class="col-md-4"> |
| 85 | + <input id="input_3" pf-remaining-chars-count chars-max-limit="10" ng-model="messageInput3text" chars-warn-remaining="5" count-fld="charRemainingCntFld_3" |
| 86 | + block-input-at-max-limit="true"/> |
| 87 | + <span class="chars-remaining-pf"><span id="charRemainingCntFld_3" style="padding-left: 5px"></span>Remaining</span> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + </file> |
| 92 | +
|
| 93 | + <file name="script.js"> |
| 94 | + angular.module( 'patternfly.example', ['patternfly.form']); |
| 95 | +
|
| 96 | + angular.module( 'patternfly.example' ).controller( 'DemoCtrl', function( $scope ) { |
| 97 | + $scope.messageArea1text = "Initial Text"; |
| 98 | + $scope.messageArea2text = ""; |
| 99 | + $scope.messageInput3text = ""; |
| 100 | +
|
| 101 | + $scope.charsMaxLimitExceeded = false; |
| 102 | +
|
| 103 | + // 'tfId' will equal the id of the text area/input field which |
| 104 | + // triggered the event |
| 105 | + $scope.$on('overCharsMaxLimit', function (event, tfId) { |
| 106 | + if(!$scope.charsMaxLimitExceeded){ |
| 107 | + $scope.charsMaxLimitExceeded = true; |
| 108 | + } |
| 109 | + }); |
| 110 | +
|
| 111 | + // 'tfId' will equal the id of the text area/input field which |
| 112 | + // triggered the event |
| 113 | + $scope.$on('underCharsMaxLimit', function (event, tfId) { |
| 114 | + if($scope.charsMaxLimitExceeded){ |
| 115 | + $scope.charsMaxLimitExceeded = false; |
| 116 | + } |
| 117 | + }); |
| 118 | +
|
| 119 | + }); |
| 120 | +
|
| 121 | + </file> |
| 122 | + </example> |
| 123 | +*/ |
0 commit comments