Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ramanan12345/e2bd57b6758aedea4052 to your computer and use it in GitHub Desktop.

Select an option

Save ramanan12345/e2bd57b6758aedea4052 to your computer and use it in GitHub Desktop.
AngularJS Dialog Service, with i18n

AngularJS Dialog Service, with i18n

Demonstration of a dialog service using AngularJS, Bootstrap, Angular-UI-Bootstrap and Angular-Translate. Shows off Angular-Dialogs-Service version 4.2.0.

A Pen by Michael E Conroy on CodePen.

License.

.container{:'ng-app' => 'modalTest',:'ng-controller' => 'dialogServiceTest'}
.row
.col-md-12
%h1
Dialog Service Demo
%small (with i18n capabilities)
%p.text-muted
%small (Bootstrap 3.1.1, AngularJS 1.2.16, Angular UI Bootstrap 0.11.0, Angular-Translate)
%p Demo of Angular dialog service using Bootstrap 3.1.1 with Angular & Angular UI Bootstrap's modal service (0.11.0).
%p
As well as the use of
%a{:href => 'https://github.com/angular-translate'} Angular-Translate (https://github.com/angular-translate)
%p
This demo shows the ability to use different languages with your dialogs as well as switch lanugages on the fly (This works for the preset dialogs only, for the custom ones you need to set that up yourself - for obvious reasons). You can also now set the size of the modal/dialog either small (sm) or large (lg), requires Bootstrap 3.1.1 and Angular-UI-Bootstrap 0.11.0. In previous versions of the "angular-dialog-service"
%a{:href=>'https://github.com/m-e-conroy/angular-dialog-service'} (https://github.com/m-e-conroy/angular-dialog-service)
the two way "binding" between a custom dialog and its calling controller was always decoupled, now the two way "binding" can be retained by using the "dialogsProvider.useCopy(false)."
.row
.col-md-12
%button.btn.btn-danger{:'ng-click'=>"launch('error')"} Error Dialog
%button.btn.btn-primary{:'ng-click'=>"launch('wait')"} Wait Dialog
%button.btn.btn-default{:'ng-click'=>"launch('notify')"} Notify Dialog
%button.btn.btn-success{:'ng-click'=>"launch('confirm')"} Confirm Dialog
%button.btn.btn-warning{:'ng-click'=>"launch('custom')"} Custom Dialog
%button.btn.btn-warning{:'ng-click'=>"launch('custom2')"} Custom Dialog 2
.row
.col-md-12
%br
%p
%strong Confirmation:
{{confirmed}}
%p
%strong Name Enter(From Custom):
{{name}}
%p
%strong Custom 2 Value:
{{custom.val}}
%p
%strong Current Language:
{{language}}
.row
.col-md-12
%button.btn.btn-default{:'ng-click'=>"setLanguage('en-US')"} English
%button.btn.btn-default{:'ng-click'=>"setLanguage('es')"} Spanish
angular.module('modalTest',['ui.bootstrap','dialogs.main','pascalprecht.translate'])
.controller('dialogServiceTest',function($scope,$rootScope,$timeout,$translate,dialogs){
//-- Variables --//
$scope.lang = 'en-US';
$scope.language = 'English';
var _progress = 33;
$scope.name = '';
$scope.confirmed = 'No confirmation yet!';
$scope.custom = {
val: 'Initial Value'
};
//-- Listeners & Watchers --//
$scope.$watch('lang',function(val,old){
switch(val){
case 'en-US':
$scope.language = 'English';
break;
case 'es':
$scope.language = 'Spanish';
break;
}
});
//-- Methods --//
$scope.setLanguage = function(lang){
$scope.lang = lang;
$translate.use(lang);
};
$scope.launch = function(which){
switch(which){
case 'error':
dialogs.error();
break;
case 'wait':
var dlg = dialogs.wait(undefined,undefined,_progress);
_fakeWaitProgress();
break;
case 'notify':
dialogs.notify();
break;
case 'confirm':
var dlg = dialogs.confirm();
dlg.result.then(function(btn){
$scope.confirmed = 'You confirmed "Yes."';
},function(btn){
$scope.confirmed = 'You confirmed "No."';
});
break;
case 'custom':
var dlg = dialogs.create('/dialogs/custom.html','customDialogCtrl',{},'lg');
dlg.result.then(function(name){
$scope.name = name;
},function(){
if(angular.equals($scope.name,''))
$scope.name = 'You did not enter in your name!';
});
break;
case 'custom2':
var dlg = dialogs.create('/dialogs/custom2.html','customDialogCtrl2',$scope.custom,'lg');
break;
}
}; // end launch
var _fakeWaitProgress = function(){
$timeout(function(){
if(_progress < 100){
_progress += 33;
$rootScope.$broadcast('dialogs.wait.progress',{'progress' : _progress});
_fakeWaitProgress();
}else{
$rootScope.$broadcast('dialogs.wait.complete');
}
},1000);
};
}) // end controller(dialogsServiceTest)
.controller('customDialogCtrl',function($scope,$modalInstance,data){
//-- Variables --//
$scope.user = {name : ''};
//-- Methods --//
$scope.cancel = function(){
$modalInstance.dismiss('Canceled');
}; // end cancel
$scope.save = function(){
$modalInstance.close($scope.user.name);
}; // end save
$scope.hitEnter = function(evt){
if(angular.equals(evt.keyCode,13) && !(angular.equals($scope.user.name,null) || angular.equals($scope.user.name,'')))
$scope.save();
};
}) // end controller(customDialogCtrl)
.controller('customDialogCtrl2',function($scope,$modalInstance,data){
$scope.data = data;
//-- Methods --//
$scope.done = function(){
$modalInstance.close($scope.data);
}; // end done
})
.config(['dialogsProvider','$translateProvider',function(dialogsProvider,$translateProvider){
dialogsProvider.useBackdrop('static');
dialogsProvider.useEscClose(false);
dialogsProvider.useCopy(false);
dialogsProvider.setSize('sm');
$translateProvider.translations('es',{
DIALOGS_ERROR: "Error",
DIALOGS_ERROR_MSG: "Se ha producido un error desconocido.",
DIALOGS_CLOSE: "Cerca",
DIALOGS_PLEASE_WAIT: "Espere por favor",
DIALOGS_PLEASE_WAIT_ELIPS: "Espere por favor...",
DIALOGS_PLEASE_WAIT_MSG: "Esperando en la operacion para completar.",
DIALOGS_PERCENT_COMPLETE: "% Completado",
DIALOGS_NOTIFICATION: "Notificacion",
DIALOGS_NOTIFICATION_MSG: "Notificacion de aplicacion Desconocido.",
DIALOGS_CONFIRMATION: "Confirmacion",
DIALOGS_CONFIRMATION_MSG: "Se requiere confirmacion.",
DIALOGS_OK: "Bueno",
DIALOGS_YES: "Si",
DIALOGS_NO: "No"
});
$translateProvider.preferredLanguage('en-US');
}])
.run(['$templateCache',function($templateCache){
$templateCache.put('/dialogs/custom.html','<div class="modal-header"><h4 class="modal-title"><span class="glyphicon glyphicon-star"></span> User\'s Name</h4></div><div class="modal-body"><ng-form name="nameDialog" novalidate role="form"><div class="form-group input-group-lg" ng-class="{true: \'has-error\'}[nameDialog.username.$dirty && nameDialog.username.$invalid]"><label class="control-label" for="course">Name:</label><input type="text" class="form-control" name="username" id="username" ng-model="user.name" ng-keyup="hitEnter($event)" required><span class="help-block">Enter your full name, first &amp; last.</span></div></ng-form></div><div class="modal-footer"><button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button><button type="button" class="btn btn-primary" ng-click="save()" ng-disabled="(nameDialog.$dirty && nameDialog.$invalid) || nameDialog.$pristine">Save</button></div>');
$templateCache.put('/dialogs/custom2.html','<div class="modal-header"><h4 class="modal-title"><span class="glyphicon glyphicon-star"></span> Custom Dialog 2</h4></div><div class="modal-body"><label class="control-label" for="customValue">Custom Value:</label><input type="text" class="form-control" id="customValue" ng-model="data.val" ng-keyup="hitEnter($event)"><span class="help-block">Using "dialogsProvider.useCopy(false)" in your applications config function will allow data passed to a custom dialog to retain its two-way binding with the scope of the calling controller.</span></div><div class="modal-footer"><button type="button" class="btn btn-default" ng-click="done()">Done</button></div>')
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment