ENDRPrint/web/js/directive/sharedDirective.js

347 lines
11 KiB
JavaScript
Raw Permalink Normal View History

2024-08-14 10:33:27 +07:00
scotchApp.directive('datepickers', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$(el).datepicker(
{
dateFormat : 'dd/mm/yy',
changeMonth : true,
changeYear : true,
showButtonPanel : false,
showAnim : 'show',
onSelect: function(dateText) {
scope.$apply(function() {
//alert("");
console.log(dateText);
ngModel.$setViewValue(dateText);
});
}
} );
}
};
});
scotchApp.directive('ngEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
};
});
scotchApp.directive('keybinding', function () {
return {
restrict: 'E',
scope: {
invoke: '&'
},
link: function (scope, el, attr) {
Mousetrap.bind(attr.on, scope.invoke);
}
};
});
scotchApp.directive('format', ['$filter', function ($filter) {
return {
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
ctrl.$formatters.unshift(function (a) {
return $filter(attrs.format)(ctrl.$modelValue)
});
ctrl.$parsers.unshift(function (viewValue) {
var plainNumber = viewValue.replace(/[^\d|\-+|\.+]/g, '');
elem.val($filter(attrs.format)(plainNumber));
return plainNumber;
});
}
};
}]);
scotchApp.directive('tabs', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
controller: [ "$scope", function($scope) {
var panes = $scope.panes = [];
$scope.select = function(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
}
this.addPane = function(pane) {
if (panes.length == 0) $scope.select(pane);
panes.push(pane);
}
}],
template:
'<div class="tabbable">' +
'<ul class="nav nav-tabs">' +
'<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
'<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
'</li>' +
'</ul>' +
'<div class="tab-content" ng-transclude></div>' +
'</div>',
replace: true
};
});
scotchApp.directive('pane', function() {
return {
require: '^tabs',
restrict: 'E',
transclude: true,
scope: { title: '@' },
link: function(scope, element, attrs, tabsCtrl) {
tabsCtrl.addPane(scope);
},
template:
'<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
'</div>',
replace: true
};
});
scotchApp.directive('toNumber', function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
ctrl.$parsers.push(function (value) {
//alert("11");
return parseFloat(value || '');
});
}
};
});
scotchApp.directive('yearDrop', function () {
var currentYear = new Date().getFullYear();
return {
link: function (scope, element, attrs) {
scope.yearsList = [];
for (var i = +attrs.offset; i < +attrs.range + 1; i++) {
scope.yearsList.push({"id":currentYear + i,"name":currentYear+543 + i});
}
scope.currentYear = currentYear;
}
}
});
scotchApp.directive('monthDrop', function () {
//var currentYear = new Date().getFullYear();
return {
link: function (scope, element, attrs) {
//scope.MonthsList = [];
if("EN" == attrs.mode){
scope.monthsList = [
//{id: "",name: "--Select Month--"},
{id: "01",name: "January"},
{id: "02",name: "February"},
{id: "03",name: "March"},
{id: "04",name: "April"},
{id: "05",name: "May"},
{id: "06",name: "June"},
{id: "07",name: "July"},
{id: "08",name: "August"},
{id: "09",name: "September"},
{id: "10",name: "October"},
{id: "11",name: "November"},
{id: "12",name: "December"}
];
} else if("TH" == attrs.mode){
scope.monthsList = [
//{id: "",name: "--Select Month--"},
{id: "01",name: "มกราคม"},
{id: "02",name: "กุมภาพันธ์"},
{id: "03",name: "มีนาคม"},
{id: "04",name: "เมษายน"},
{id: "05",name: "พฤษภาคม"},
{id: "06",name: "มิถุนายน"},
{id: "07",name: "กรกฎาคม"},
{id: "08",name: "สิงหาคม"},
{id: "09",name: "กันยายน"},
{id: "10",name: "ตุลาคม"},
{id: "11",name: "พฤศจิกายน"},
{id: "12",name: "ธันวาคม"}
];
}
}
}
});
scotchApp.directive('autoFocus', function() {
return {
link: {
pre: function(scope, element, attr) {
//console.log('prelink executed for');
},
post: function(scope, element, attr) {
//console.log('postlink executed');
element[0].focus();
}
}
}
});
scotchApp.directive('focusOn', function() {
return function(scope, elem, attr) {
scope.$on('focusOn', function(e, name) {
if(name === attr.focusOn) {
elem[0].focus();
}
});
};
});
scotchApp.directive('focus',
function($timeout) {
return {
scope : {
trigger : '@focus'
},
link : function(scope, element) {
scope.$watch('trigger', function(value) {
if (value === "true") {
$timeout(function() {
element[0].focus();
element.trigger('chosen:activate');
});
}
});
}
};
}
);
scotchApp.directive('capitalize', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if(inputValue == undefined) inputValue = '';
var capitalized = inputValue.toUpperCase();
if(capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
return capitalized;
}
modelCtrl.$parsers.push(capitalize);
capitalize(scope[attrs.ngModel]); // capitalize initial value
}
};
});
var secretEmptyKey = '[$empty$]';
scotchApp.directive('emptyTypeahead', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
// this parser run before typeahead's parser
modelCtrl.$parsers.unshift(function (inputValue) {
var value = (inputValue ? inputValue : secretEmptyKey); // replace empty string with secretEmptyKey to bypass typeahead-min-length check
modelCtrl.$viewValue = value; // this $viewValue must match the inputValue pass to typehead directive
return value;
});
// this parser run after typeahead's parser
modelCtrl.$parsers.push(function (inputValue) {
return inputValue === secretEmptyKey ? '' : inputValue; // set the secretEmptyKey back to empty string
});
}
}
});
scotchApp.directive('clickOut', function($document){
return {
restrict: 'A',
link: function(scope, elem, attr, ctrl) {
elem.bind('click', function(e) {
e.stopPropagation();
});
$document.bind('click', function() {
scope.$apply(attr.clickOut);
})
}
}
});
scotchApp.directive('unselect', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$parsers.push(function(value) {
//console.log(value);
if ( value === null ) {
value = '';
}
//console.log(value);
return value;
});
}
};
});
scotchApp.directive('focusMe', function($timeout, $parse) {
return {
link: function(scope, element, attrs) {
var model = $parse(attrs.focusMe);
scope.$watch(model, function(value) {
//console.log('value=',value);
if(value === true) {
$timeout(function() {
element[0].focus();
});
}
});
}
};
});
scotchApp.directive('uppercase', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if(inputValue == undefined) inputValue = '';
var capitalized = inputValue.toUpperCase();
if(capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
return capitalized;
}
modelCtrl.$parsers.push(capitalize);
capitalize(scope[attrs.ngModel]); // capitalize initial value
}
};
});
//
scotchApp.filter('slices', function() {
return function(arr, start, end) {
return arr.slice(start, end);
};
});