Monday, September 29, 2014

Paged Model for Knockout.js foreach pagination

Here is a paged model which can be used with knockout.js to display paged tables etc.



1:  var Model = function() {  
2:    var self = this;  
3:    //Array container to host items  
4:    this.all = ko.observableArray([]);  
5:    //Current Page Number  
6:    this.pageNumber = ko.observable(1);  
7:    //No of Items Per page  
8:    this.nbPerPage = ko.observable(3);  
9:    //Total Number of Pages  
10:    this.totalPages = ko.computed(function() {  
11:      var div = Math.floor(self.all().length / self.nbPerPage());  
12:      div += self.all().length % self.nbPerPage() > 0 ? 1 : 0;  
13:      return div ;  
14:    });  
15:    //Number of Items in the Array  
16:    this.nItems = ko.computed(function () {  
17:      return self.all().length;  
18:    });  
19:    //Paginated array  
20:    this.paginated = ko.computed(function() {  
21:      var first = (self.pageNumber() - 1) * self.nbPerPage();  
22:      return self.all.slice(first, first + self.nbPerPage());  
23:    });  
24:    //Has a previous item  
25:    this.hasPrevious = ko.computed(function() {  
26:      return self.pageNumber() !== 1;  
27:    });  
28:    //Has a next item  
29:    this.hasNext = ko.computed(function() {  
30:      return self.pageNumber() !== self.totalPages();  
31:    });  
32:    //Move to next  
33:    this.next = function() {  
34:      if(self.pageNumber() < self.totalPages()) {  
35:        self.pageNumber(self.pageNumber() + 1);  
36:      }  
37:    }  
38:    //Move to first page  
39:    this.first = function () {    
40:        self.pageNumber(1);   
41:    }  
42:    //Move to Last page  
43:    this.last = function () {  
44:      self.pageNumber(self.totalPages());  
45:    }  
46:    //Move to Previous page  
47:    this.previous = function() {  
48:      if(self.pageNumber() != 1) {  
49:        self.pageNumber(self.pageNumber() - 1);  
50:      }  
51:    }  
52:    //set number of Items per page  
53:    this.setnPerPage = function (val) {  
54:      return function () {  
55:        self.nbPerPage(val);  
56:      }  
57:    }  
58:    //filtering options  
59:    this.filter = function (val,id) {  
60:      return function () {    
61:      //Create Own filtering  
62:    }  
63:    }  
64:  }  

Use following code to initialed and start binding.
1:   var m = new Model();  
2:        m.all(data);  
3:        m.nbPerPage(5);  
4:        ko.applyBindings(m);  
5:        $('#loading').button('reset');  

where data can be any json object.
Usage:
    1. use 'paginated' in foreach binding.
    2. load initial data to 'all'
    3. Use click bind for use 'first,next, last, previous'.

Display thaana dates in dhivehi sites.


Here is a small JavaScript snippet which can be used to display date strings in thaana format.

thaanaDates.js

 function thaanaDates(dateObj){   
           this.getLong = function() {  
             return dateObj.getDate() + " " + dateObj.getThaanaMonthName() + " " + dateObj.getFullYear() + " (" + dateObj.getThaanaDayName() + ")";  
           }  
           this.getShort = function() {  
             return dateObj.getDate() + " " + dateObj.getThaanaMonthName() + " " + dateObj.getFullYear();  
           }  
           this.getLongReverse = function() {  
              return dateObj.getFullYear() + " " + dateObj.getThaanaMonthName() + " " + dateObj.getDate() + "، " + dateObj.getThaanaDayName() + "";  
           }  
           this.getDayFirst = function() {  
              return dateObj.getThaanaDayName() + "، " + dateObj.getDate() + " " + dateObj.getThaanaMonthName() + " " + dateObj.getFullYear() + "";  
           }       
 }  
 Date.prototype.getThaanaMonthName = function() {  
           var m = ["ޖަނަވަރީ","ފެބްރުއަރީ","މާރިޗު","އޭޕުރީލް","މޭ","ޖޫން","ޖުލައި","އޯގަސްޓް","ސެޕްޓެމްބަރު","އޮކްޓޯބަރު","ނޮވެމްބަރު","ޑިސެމްބަރު"];            
           return m[this.getMonth()];  
 }   
 Date.prototype.getThaanaDayName = function() {  
           var d = ["އާދީއްތަ","ހޯމަ","އަންގާރަ","ބުދަ","ބުރާސްފަތި","ހުކުރު","ހޮނިހިރު"];  
           return d[this.getDay()];  
 }  


Methods:

  •  getLong: Displays long formatted thaana date string
  •  getShort: Displays date without a day string
  •  getLongReverse: Displays date string with year at the beginning
  •  getDayFirst: Displays date string with day string at the beginning

Simpla Usage:


 document.getElementById("demo").innerHTML = new thaanaDates(new Date).getLong();  

Downloads:

thaanaDates.js
thaanaDates.min.js

Thursday, March 13, 2014

.NET Function to check if a file in use

Here is a .NET Function to use whether a file in system is in use by another process or application before attempting to use the file.

If the file is in use following function will return false.

VB.NET

  1. Public Function FileInUse(ByVal sFile As String) As Boolean
  2. Dim thisFileInUse As Boolean = False
  3. If System.IO.File.Exists(sFile) Then
  4. Try
  5. Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
  6. ' thisFileInUse = False
  7. End Using
  8. Catch
  9. thisFileInUse = True
  10. End Try
  11. End If
  12. Return thisFileInUse
  13. End Function

C#

  1. public bool FileInUse(string sFile)
  2. {
  3. bool thisFileInUse = false;
  4. if (System.IO.File.Exists(sFile)) {
  5. try {
  6. using (System.IO.FileStream f = new System.IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
  7. // thisFileInUse = False
  8. }
  9. } catch {
  10. thisFileInUse = true;
  11. }
  12. }
  13. return thisFileInUse;
  14. }

Wednesday, March 12, 2014

Free PDF Splitter - Nashath Auto PDF Splitter 1.0


This is a FREE Software under GPL 3.0, which splits pdf files on a given folder and saves to another location while leaving  a copy of original file into a different folder.

This standalone tool watches any given folder for PDF files and once a pdf file is created on the folder, it splits file into seperate single page files and save it into a different folder and moves orginals to another folder. This software uses pdftk-server http://www.pdflabs.com/tools/pdftk-server/ to split pdf files.

Download link
http://nashathpdfsplitter.codeplex.com/