Watch Ionic menu(left or right)

.directive('watchMenu', function($timeout, $ionicSideMenuDelegate, $ionicScrollDelegate, $rootScope) {
  return {
    restrict: 'A',
    link: function($scope, $element, $attr) {
      // Run in the next scope digest
      $timeout(function() {
        // Watch for changes to the openRatio which is a value between 0 and 1 that says how "open" the side menu is
       
        $scope.$watch(function() { 
          return $ionicSideMenuDelegate.isOpenRight();
          //return $ionicSideMenuDelegate.isOpenLeft();
        }, 
          function(ratio) {
            $scope.data=ratio;
            if( ratio == 1){
              $rootScope.$broadcast('rightMenu.open');
              $ionicScrollDelegate.$getByHandle('rightMenuScroll').scrollTop();
            }else{}

          });
      });
    }
  };

});

On Click of anchor (a) open a url in inappbrowser and show loading

I've a write some functions for this :



document.onclick = function(e) {
e = e || window.event;
var element = e.target || e.srcElement;

if (element.tagName == 'A' && ((element.href.indexOf('http://') >= || element.href.indexOf('https://') >= 0)) && element.href.indexOf('//localhost:') < 0 ) {
e.preventDefault();
var ref = window.open(element.href, "_blank", "location=yes");;
ref.addEventListener('loadstart', inAppBrowserbLoadStart);
ref.addEventListener('loadstop', inAppBrowserbLoadStop);
ref.addEventListener('loaderror', inAppBrowserbLoadError);
ref.addEventListener('exit', inAppBrowserbClose);
return false;
}
}

function inAppBrowserbLoadStart(event) {
   navigator.notification.activityStart("Please Wait", "It'll only take a moment...");
}

function inAppBrowserbLoadStop(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbLoadError(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbClose(event) {
   navigator.notification.activityStop();
   ref.removeEventListener('loadstart', inAppBrowserbLoadStart);
   ref.removeEventListener('loadstop', inAppBrowserbLoadStop);
   ref.removeEventListener('loaderror', inAppBrowserbLoadError);
   ref.removeEventListener('exit', inAppBrowserbClose);

}

Convert time form AM/PM to 24 Hours fromat

I've made a function for this, it'll return the array of hour and minute :


function am_pm_to_hours(time) {
    var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
//console.log(sHours + ":" + sMinutes);
    return [sHours, sMinutes];

}

Add icon in android lollipop push notification

Use this code :


PushNotification.init({
"android" : {
"senderID" : "292145204069",
"icon" : "notification_icon", // It will search for notification_icon.png in your android drawable folder
"iconColor": "#0076cb"
}

});

Play a video in iframe


Write the following code :

<iframe width="100%" height="auto" ng-if="video_url" onerror="this.style.display = 'none'" src="{{video_url | VideoUrl}}"></iframe> 
.filter('VideoUrl', ['$sce', function($sce) {
    return function(val) {
        return $sce.trustAsResourceUrl(val);
    };
}])

Show loader on InAppBrowser while loading

For InAppBrowser You've to install the plugin 'cordova-plugin-inappbrowser' :

cordova plugin add cordova-plugin-inappbrowser

For Dialogs You've to install the plugin 'cordova-plugin-dialogs' :

cordova plugin add cordova-plugin-dialogs
Then write this code for open inappbrowser :


var ref = window.open(encodeURI('http://www.google.com', '_blank', 'location=yes');

ref.addEventListener('loadstart', inAppBrowserbLoadStart);
ref.addEventListener('loadstop', inAppBrowserbLoadStop);
ref.addEventListener('loaderror', inAppBrowserbLoadError);
ref.addEventListener('exit', inAppBrowserbClose);


function inAppBrowserbLoadStart(event) {
   navigator.notification.activityStart("Please Wait", "It'll only take a moment...");
}

function inAppBrowserbLoadStop(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbLoadError(event) {
   navigator.notification.activityStop();
}

function inAppBrowserbClose(event) {
   navigator.notification.activityStop();
   ref.removeEventListener('loadstart', inAppBrowserbLoadStart);
   ref.removeEventListener('loadstop', inAppBrowserbLoadStop);
   ref.removeEventListener('loaderror', inAppBrowserbLoadError);
   ref.removeEventListener('exit', inAppBrowserbClose);
}



Check GPS state (on/off) in ionic/cordova

Fist you've to install the plugin https://github.com/dpa99c/cordova-diagnostic-plugin then simply execute this code after platform ready :

Check GPS state (on/off) in ionic/cordova

cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
    console.log("Location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
});


Check WiFi is enable or not?

cordova.plugins.diagnostic.isWifiEnabled(function(enabled){
    console.log("WiFi is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
});

Check device has a camera AND the application is authorized to use it. or not?

cordova.plugins.diagnostic.isCameraEnabled(function(exists){
    console.log("Device " + (exists ? "does" : "does not") + " have a camera");
}, function(error){
    console.error("The following error occurred: "+error);
});

Enables/disables WiFi on the device

cordova.plugins.diagnostic.setWifiState(function(){
    console.log("Wifi was enabled");
}, function(error){
    console.error("The following error occurred: "+error);
},
true);

Checks the device has Bluetooth capabilities and if it has then Bluetooth is on or off?

cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){
    console.log("Bluetooth is " + (enabled ? "enabled" : "disabled"));
}, function(error){
    console.error("The following error occurred: "+error);
});


Enables/disables Bluetooth on the device

cordova.plugins.diagnostic.setBluetoothState(function(){
    console.log("Bluetooth was enabled");
}, function(error){
    console.error("The following error occurred: "+error);
},
true);