(function ($) { $.jgrid.defaults.width = 950; $.jgrid.defaults.responsive = true; $.jgrid.defaults.styleUI = 'Bootstrap'; $.jgrid.styleUI.Bootstrap.base.rowTable = "table table-bordered table-striped" $.fn.outerHTML = function (calc) { if (this.length > 0) { return $(this)[0].outerHTML; } } $.fn.geturl = function (url, prm) { var regexS = "[\\?&]page=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(url); if (results != null) { url = url.replace("featured", "list"); url = url.replace("page=" + results[1], "page=1"); } window.location.href = url; } })(jQuery); var myApp = angular.module('myApp', ['ngAnimate','ngCookies', 'ui.bootstrap', 'ngFileUpload', 'ngRoute', 'ngTouch', 'ngSanitize', 'ngMessages', 'ui.select', 'ui.grid', 'ui.grid.pagination', 'ui.grid.resizeColumns', 'ui.grid.moveColumns', 'ui.grid.pinning']); myApp.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest'; //var antiForgeryToken = document.getElementsByName('__RequestVerificationToken')[1].value; //$httpProvider.defaults.headers.post['__RequestVerificationToken'] = antiForgeryToken; } ]); myApp.filter('unsafe', function ($sce) { return $sce.trustAsHtml; }) myApp.filter("dateFilter", function () { return function (item) { if (item != null) { return new Date(item); } return null; }; }); myApp.service("$myService", ['$http', '$q', 'uiGridConstants', '$uibModal', function ($http, $q, uiGridConstants, $uibModal) { this.Get = function (url, data) { var q = url + "?" + ($.param(data || '')); window.location.href = q; } this.post = function (url, data) { var form = $('
', { action: url, method: "post" }); for (var prop in data) { $(form).append($('', { name: prop, value: data[prop] })); } form.appendTo('body').submit().remove(); } this.$save = function (url, data, err) { var defer = $q.defer(); $http.post(url, data).success(function (d) { defer.resolve(d); }).error(function (d) { err = d; defer.reject(d); }); return defer.promise } this.$delete = function (url, data, err) { var that = this; if (!confirm("Want to Delete?")) return; var defer = $q.defer(); $http.post(url, data).success(function (d) { defer.resolve(d); }).error(function (d) { if (err) that.$showerror(err); defer.reject(d); }); return defer.promise } this.$loader = function () { var container = $('#ajax-loader'); if (container.length == 0) container = $('
Wait....
').appendTo('body') var r = { close: function () { container.remove(); } } return r; } }]); myApp.filter('unique', function () { return function (items, filterOn) { if (filterOn === false) { return items; } if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) { var hashCheck = {}, newItems = []; var extractValueToCompare = function (item) { if (angular.isObject(item) && angular.isString(filterOn)) { return item[filterOn]; } else { return item; } }; angular.forEach(items, function (item) { var valueToCheck, isDuplicate = false; for (var i = 0; i < newItems.length; i++) { if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) { isDuplicate = true; break; } } if (!isDuplicate) { newItems.push(item); } }); items = newItems; } return items; } }); myApp.filter('urlencode', function () { return function (input) { return window.encodeURIComponent(input); } }); function todate(v) { if (!v) return null; var values = v.replace(/\//g, '-').split("-"); var dt = new Date(values[2], +values[1] - 1, values[0]); if ((dt.getDate() == values[0]) && (dt.getMonth() + 1 == values[1]) && (dt.getFullYear() == values[2])) { return dt; } return null; } myApp.directive('validationSummary', ['$interval', '$timeout', function ($interval, $timeout) { return { replace: true, restrict: 'E', scope: { myErrorSummary: '=' }, link: function (scope, elm, attr, ngModel) { scope.$watchCollection('myErrorSummary', function () { if (angular.isDefined(scope.myErrorSummary)) { scope.modal = Object.keys(scope.myErrorSummary.modal).join(', '); scope.other = scope.myErrorSummary.other || []; } else { scope.modal = null; scope.other = null; } }); }, template: '
Following error occured!
' }; }]); myApp.directive('ckEditor', ['$timeout', function ($timeout) { return { require: '?ngModel', link: function (scope, elm, attr, ngModel) { var ck = CKEDITOR.replace(elm[0]); if (!ngModel) return; ck.on('instanceReady', function () { ck.setData(ngModel.$viewValue); }); function updateModel() { ngModel.$setViewValue(ck.getData()); } ck.on('change', updateModel); ck.on('key', updateModel); ck.on('dataReady', updateModel); ngModel.$render = function (value) { ck.setData(ngModel.$viewValue); }; } }; }]); myApp.directive('myMobile', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ctrl) { ctrl.$validators["my-mobile"] = function (mv, viewValue) { if (!viewValue) return true; return (viewValue.length == 10); } } } }); myApp.directive('myEmail', function () { var regx = /\b[a-zA-Z0-9\u00C0-\u017F._%+-]+@[a-zA-Z0-9\u00C0-\u017F.-]+\.[a-zA-Z]{2,}\b/; return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ctrl) { ctrl.$validators["my-email"] = function (mv, viewValue) { if (!viewValue) return true; return regx.test(viewValue); } } } }); myApp.directive("chkModel", ["$compile", function ($compile) { return { restrict: "A", scope: { chkModel: '=', chkValue: '=', isMultiple: '=?' }, link: function (scope, ele, attrs) { if (!scope.chkModel) scope.chkModel = []; scope.isMultiple = !!scope.isMultiple; // Defining updateSelection function on the parent scope scope.updateSelections = function (selectedItems, item, isMultiple) { var itemIndex = selectedItems.indexOf(item) var isPresent = (itemIndex > -1) if (isMultiple) { if (isPresent) { selectedItems.splice(itemIndex, 1) } else { selectedItems.push(item) } } else { if (isPresent) { selectedItems.splice(0, 1) } else { selectedItems.splice(0, 1, item) } } } // Adding or removing attributes ele.attr("ng-checked", "chkModel.indexOf(" + scope.chkValue + ") > -1") var multiple = attrs.multiple ? "true" : "false" ele.attr("ng-click", "updateSelections(chkModel,chkValue, isMultiple)"); // Removing the checkbox-model attribute, // it will avoid recompiling the element infinitly ele.removeAttr("chk-model"); ele.removeAttr("chk-value"); ele.removeAttr("multiple"); $compile(ele)(scope); } } } ]); myApp.directive('myDate', ['$filter', function ($filter) { return { restrict: 'A', scope: { ngModel: '=' }, require: 'ngModel', link: function (scope, element, attrs, ngModelCtrl) { ngModelCtrl.$formatters.unshift(function (modelValue) { return $filter('date')(modelValue, 'dd/MM/yyyy'); }); ngModelCtrl.$parsers.unshift(function (viewValue) { return todate(viewValue); }); ngModelCtrl.$validators['date'] = function (modelValue, viewValue) { if (viewValue) return todate(viewValue) ? true : false; return true; }; } }; }]); myApp.directive('jqplugin', function ($compile, $timeout) { return { restrict: 'A', link: function (scope, element, attrs) { $timeout(function () { angular.element(element).jstree(); }, 3000000) scope.$on("$destroy", function () { element.jstree("destroy"); }); } }; }); myApp.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(); } }); }; }); myApp.filter('sum', function () { return function (input) { var i = input instanceof Array ? input.length : 0; var a = arguments.length; if (a === 1 || i === 0) return i; var keys = []; while (a-- > 1) { var key = arguments[a].split('.'); var property = getNestedPropertyByKey(input[0], key); if (isNaN(property)) throw 'filter sumProduct can count only numeric values'; keys.push(key); } var total = 0; while (i--) { var product = 1; for (var k = 0; k < keys.length; k++) product *= getNestedPropertyByKey(input[i], keys[k]); total += product; } return total; function getNestedPropertyByKey(data, key) { for (var j = 0; j < key.length; j++) data = data[key[j]]; return data; } } }); myApp.directive('serverVal', ['$compile', '$timeout', '$filter', function ($compile, $timeout, $filter) { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ctrl) { if (!ctrl) return; ctrl.$validators.serverVal = function (modelValue, viewValue) { return true; }; } }; } ]); myApp.directive('myList', ['$http', '$uibModal', function ($http, $uibModal) { var list = function (e) { this.element = e; } list.prototype = { reload: function () { this.element.trigger('reloadGrid'); }, search: function () { this.element.jqGrid('searchGrid'); } } return { restrict: 'E', scope: { listId: '@', listLoader: '@', listPager: '@' }, replace: true, link: function (scope, element, attr) { element.append('
'); var listing = scope.listLoader || "initlist"; scope.$parent.list = new list(eval(listing + '()')); } } } ]); myApp.directive('myPrint', ['$http', '$uibModal', function ($http, $uibModal) { return function (scope, element, attrs) { var _data = url; if (_data) { _data += '?' + (typeof data === "object" ? $.param(data) : 'i=' + data); } element.bind("click", function (event) { event.preventDefault(); $http.post(url, data).success(function (d) { var win = window.open(_data); win.document.write(d); }); }); }; } ]); myApp.directive('mySubmit', ['$http', '$q', '$timeout', '$myService', function ($http, $q, $timeout, $myService) { return { restrict: 'A', link: function (scope, element, attrs) { element.bind("click", function (event) { event.preventDefault(); var loader = $myService.$loader(); var $error = '$error'; var config = { form: scope.frm, data: scope.m } angular.forEach(scope.$eval(attrs.config), function (v, k) { config[k] = v; }); var defer = $q.defer(); if (!config.form.$valid) { loader.close(); return; } (function save() { $http.post(attrs.mySubmit, config.data).success(function (d) { var e = { data: d,buzy:false } if (angular.isDefined(config.callback)) config.callback(e); if (angular.isDefined(scope.close)) scope.close(); if (angular.isDefined(scope.list)) scope.list.reload(); if (!e.buzy) defer.resolve(d); }).error(function (d) { if (angular.isDefined(attrs.error)) $error = attrs.error; scope[$error] = d; defer.resolve(d); }); return defer.promise; })().then(function (t) { loader.close(); }); }); } } }]); myApp.directive('myDelete', ['$http', function ($http) { return { restrict: 'A', link: function (scope, element, attrs) { element.bind("click", function (event) { event.preventDefault(); var $error = '$error'; var data = scope.$eval(attrs.postData); if (!confirm("Want to Delete?")) return; $http.post(attrs.myDelete, typeof data === "object" ? data : { i: data }).success(function () { if (angular.isDefined(scope.list)) scope.list.reload(); }).error(function (e) { if (angular.isDefined(attrs.error)) $error = attrs.error; if (angular.isDefined(scope[$error])) scope[$error] = e; }) }); } } } ]); myApp.directive('vTag', ['$http', '$q', 'Upload', function ($http, $q, $upload) { }]); myApp.directive('myUpload', ['$http', '$q', 'Upload', function ($http, $q, $upload) { return { restrict: 'E', require:'ngModel', scope: { 'files': '=ngModel' }, replace: true, link: function (scope, element, attrs, ngModel) { var multiple = attrs.hasOwnProperty('multiple'); scope.getTemplateUrl = function () { if (multiple) return "multi-file-uploader.tpl.html"; else return "single-file-uploader.tpl.html"; } scope.text = attrs.text ? attrs.text : 'Upload'; scope.upload = function (file) { if (!file) return; $upload.upload({ url: window.location.origin + '/MyApi/Upload', data: { file: file } }).then(function (resp) { if (multiple) { if (!scope.files) scope.files = []; scope.files.push(resp.data); } else { scope.files = resp.data } }, function (resp) { console.log(resp.status); }, function (evt) { var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name); }); } scope.uploadFiles = function (files, $invalidFiles) { if (files && files.length) { for (var i = 0; i < files.length; i++) { scope.upload(files[i]); } } } scope.filename = function (f) { return f.substr(f.lastIndexOf("#")+1); } scope.download = function (file) { return window.location.origin + '/MyApi/download?i=' + window.encodeURIComponent(file); } }, template: '
' } }]); myApp.directive('ajaxSubmit', ['$http', function ($http) { return { restrict: 'A', scope: {ajaxData:"="}, link: function (scope, element, attrs) { function submit(url, data) { $http.post(url, data).then(function (d) { $('', { url: d.data }).appendTo('body').submit(); }, function (d) { }); var form = $('', { action: url, method: "post" }); for (var prop in data) { $(form).append('', { name: prop, value: data[prop] }); } } element.bind("click", function (event) { var url = window.location.href; var data = scope.$parent.m; event.preventDefault(); if (angular.isDefined(attrs.ajaxSubmit)) url = attrs.ajaxSubmit; if (scope.ajaxData) data = scope.ajaxData; submit(url,data); }); } } }]); myApp.directive('ngJqGrid', function () { return { restrict: 'E', scope: { jqList: '=', }, link: function (scope, element, attrs) { var table; var pager = { search: false, add: false, edit: false, del: false, refresh: true } scope.$watch('jqList.setup', function (newValue) { element.children().empty(); table = angular.element('
'); element.append(table); if (!newValue.hasOwnProperty('loadComplete')) { newValue["loadComplete"] = function () { angular.element(document).injector().invoke(function ($compile) { var scope = angular.element(table).scope(); if (scope) $compile(table)(scope); }); } } $(table).jqGrid(newValue); $(table).jqGrid('filterToolbar', { stringResult: true, //searchOnEnter: false, searchOperators:false }); scope.jqList.reload = function () { $(table).trigger('reloadGrid') } scope.jqList.grid = $(table); }); } } }); myApp.directive('myModal', ['$http', '$uibModal', '$q', function ($http, $uibModal, $q) { return { restrict: 'A', link: function ($scope, $element, $attr) { var template = '
'; var $error='$error' $element.bind("click", function (e) { var mscope = $scope.$new(true); if (angular.isDefined($scope.list)) mscope.list = $scope.list; var options = $.extend({ animation: true, backdrop: 'static', template: template, scope: mscope, controller: $attr.ctrl }, $scope.$eval($attr.options)); var modalInstance = $uibModal.open(options); var data; if ($attr.postData) { data=$scope.$eval($attr.postData); if (!angular.isObject(data)) data = { i: data } } mscope.close = function () { modalInstance.close('dissmiss'); } $http.post($attr.myModal, data).success(function (d) { angular.forEach(d, function (v, k) { mscope[k] = v; }); }).error(function (d) { if (angular.isDefined($attr.error)) $error = $attr.error; $scope[$error] = d; }); }); } } }]); myApp.directive('myMsgbox', ['$http', '$uibModal', '$q', function ($http, $uibModal, $q) { return { restrict: 'A', link: function ($scope, $element, $attr) { var template = '
"); });