Skip to content
This repository was archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Add the option to define the element's attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmcode committed Nov 30, 2015
1 parent d771898 commit 22fe903
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ <h2>Define minimum and maximum dates</h2>
<h2>Change strings for months</h2>
<p>Choose a date: <ng-combo-date-picker ng-model="names" ng-months="Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre"></ng-combo-date-picker></p>
<p>Current selection is: <strong>{{ names }}</strong>

<h2>Define attributtes</h2>
<p>Choose a date: <ng-combo-date-picker ng-model="attrs" ng-attrs-date='{"style": "color:red"}' ng-attrs-month='{"style": "color:green"}' ng-attrs-year='{"style": "color:blue"}'></ng-combo-date-picker></p>
<p>Current selection is: <strong>{{ attrs }}</strong>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Name | Description
`ngMaxDate` | A _string_ representing the maximum date that can be picked. By default the maximum date is the current day.
`ngMonths` | A _string_ with the names of the twelve months, separated by comma.
`ngOrder` | A _string_ with the characters "d", "m" and "y" indicating in which order the combo boxes must be displayed. By default, the combo boxes are displayed in the order "dmy".
`ngAttrsDate` | A JSON object with the attributes to add to the `select` element for the date.
`ngAttrsMonth` | A JSON object with the attributes to add to the `select` element for the month.
`ngAttrsYear` | A JSON object with the attributes to add to the `select` element for the year.

License
-------
Expand Down
28 changes: 26 additions & 2 deletions source/ngComboDatePicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* ngComboDatePicker v1.0.0
* ngComboDatePicker v1.1.0
* http://github.com/jfmdev/ngComboDatePicker
* «Copyright 2015 Jose F. Maldonado»
* License: LGPLv3 (http://www.gnu.org/licenses/lgpl-3.0.html)
Expand All @@ -18,7 +18,10 @@ angular.module("ngComboDatePicker", [])
ngMinDate : '@',
ngMaxDate : '@',
ngMonths : '@',
ngOrder: '@'
ngOrder: '@',
ngAttrsDate: '@',
ngAttrsMonth: '@',
ngAttrsYear: '@'
},
controller: ['$scope', function($scope) {
// Define function for parse dates.
Expand Down Expand Up @@ -48,10 +51,25 @@ angular.module("ngComboDatePicker", [])
return res;
}

// Function to parse a JSON object.
function parseJsonPlus(jsonObj) {
var res = null;
if(jsonObj != null) {
try{ res = JSON.parse(jsonObj); }catch(ex) {}
if(res == null) try{ res = JSON.parse(jsonObj.replace(/'/g, '"')); }catch(ex) {}
}
return res;
}

// Initialize model.
$scope.ngModel = parseDate($scope.ngModel);
if($scope.ngModel == null) $scope.ngModel = new Date();

// Initialize attributes variables.
$scope.ngAttrsDate = parseJsonPlus($scope.ngAttrsDate);
$scope.ngAttrsMonth = parseJsonPlus($scope.ngAttrsMonth);
$scope.ngAttrsYear = parseJsonPlus($scope.ngAttrsYear);

// Verify if initial date was defined.
var initDate = parseDate($scope.ngDate);
if(initDate != null) $scope.ngModel = initDate;
Expand Down Expand Up @@ -160,13 +178,19 @@ angular.module("ngComboDatePicker", [])
$scope.updateDateList();
};

$scope.getSomething = function() { return 'color:red;'; };
} ],
link: function(scope, element, attrs) {
// Initialize variables.
var jqLite = angular.element;
var children = jqLite(element[0]).children();
var order = scope.ngOrder.split('');

// Add attributes.
if(scope.ngAttrsDate != null) jqLite(children[0]).attr( scope.ngAttrsDate );
if(scope.ngAttrsMonth != null) jqLite(children[1]).attr( scope.ngAttrsMonth );
if(scope.ngAttrsYear != null) jqLite(children[2]).attr( scope.ngAttrsYear );

// Reorder elements.
for(var i=0; i<order.length; i++) {
if(order[i] == 'd') jqLite(element[0]).append(children[0]);
Expand Down
2 changes: 1 addition & 1 deletion source/ngComboDatePicker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22fe903

Please sign in to comment.