This shows you the differences between two versions of the page.
sample_code:visitor_log [2014/10/19 10:50] stevegarman Set to Landscape |
sample_code:visitor_log [2014/12/07 05:05] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Visitor log ====== | ||
- | This is the first working draft of a visitor sign-in log for my workplace. | ||
- | The live version includes more error-checking for signing out, extra code to deal with the possibility that the app is left running over midnight and a fair bit of prettying up. | ||
- | ===== The code ===== | ||
- | <code javascript visitors.js> | ||
- | //Visitor sign-in app by Steve Garman | ||
- | //saves one day's data as a JSON string in a file | ||
- | |||
- | //Global variables | ||
- | var recArray = [], fileFolder = "/ | ||
- | //Global layouts and controls | ||
- | var scrollr, layScroll, edtName, edtOrg, edtVehicle, | ||
- | |||
- | //Called when application is started. | ||
- | function OnStart(){ | ||
- | if( ! app.IsTablet()) app.SetOrientation(" | ||
- | //file variables | ||
- | var d = new Date(); | ||
- | jsonPath = fileFolder + " | ||
- | | ||
- | + " | ||
- | app.MakeFolder(fileFolder) | ||
- | //Create main layout. | ||
- | var lay = app.CreateLayout( " | ||
- | //Create a scroller and add it to main layout | ||
- | scrollr = app.CreateScroller( 1.0, 0.8 ); | ||
- | lay.AddChild( scrollr ); | ||
- | //Create a layout inside scroller. | ||
- | layScroll = app.CreateLayout( " | ||
- | scrollr.AddChild( layScroll ); | ||
- | | ||
- | //Create a sign-in layout | ||
- | var layNew = app.CreateLayout( " | ||
- | edtName = app.CreateTextEdit("", | ||
- | edtName.SetHint(" | ||
- | layNew.AddChild(edtName); | ||
- | edtOrg = app.CreateTextEdit("", | ||
- | edtOrg.SetHint(" | ||
- | layNew.AddChild(edtOrg); | ||
- | edtVehicle = app.CreateTextEdit("", | ||
- | edtVehicle.SetHint(" | ||
- | layNew.AddChild(edtVehicle); | ||
- | edtReason = app.CreateTextEdit("", | ||
- | edtReason.SetHint(" | ||
- | layNew.AddChild(edtReason); | ||
- | var btnCreate = app.CreateButton(" | ||
- | btnCreate.SetOnTouch(btnCreate_OnClick); | ||
- | layNew.AddChild(btnCreate); | ||
- | | ||
- | //Add sign-in layout to main layout | ||
- | lay.AddChild(layNew); | ||
- | |||
- | //Add main layout to app. | ||
- | app.AddLayout( lay ); | ||
- | //check if today' | ||
- | if(app.FileExists(jsonPath)){ | ||
- | loadJSONfile(); | ||
- | } | ||
- | }//OnStart | ||
- | |||
- | function btnCreate_OnClick( ){ | ||
- | app.HideKeyboard(); | ||
- | var name = edtName.GetText(); | ||
- | var org = edtOrg.GetText(); | ||
- | var reg = edtVehicle.GetText(); | ||
- | var reason = edtReason.GetText(); | ||
- | //check required fields | ||
- | var errs = ""; | ||
- | if ( name == "" | ||
- | if ( reason == "" | ||
- | if ( errs != "" | ||
- | //bail out | ||
- | app.Alert( errs + " | ||
- | return; | ||
- | } | ||
- | // clear sign-in form for next visitor | ||
- | edtName.SetText("" | ||
- | edtOrg.SetText("" | ||
- | edtVehicle.SetText("" | ||
- | edtReason.SetText("" | ||
- | // create a record | ||
- | var obj = {}; | ||
- | obj.name = name; | ||
- | obj.org = org; | ||
- | obj.reg = reg; | ||
- | obj.reason = reason; | ||
- | var d = new Date(); | ||
- | obj.timeIn = d.toTimeString().slice(0, | ||
- | obj.timeOut = " | ||
- | // add this record to the array | ||
- | recArray.push( obj ); | ||
- | saveJSONfile(); | ||
- | // display record on scroller | ||
- | layScroll.AddChild( recordLayout(obj) ); | ||
- | scrollr.ScrollTo(0, | ||
- | }// | ||
- | |||
- | function btnOut_OnClick( ){ | ||
- | //has this visitor already signed out? | ||
- | if (this.GetText() != " | ||
- | //not yet, so record time out | ||
- | var d = new Date(); | ||
- | this.obj.timeOut = d.toTimeString().slice(0, | ||
- | this.SetText(this.obj.timeOut); | ||
- | //update copy of recArray on file | ||
- | saveJSONfile(); | ||
- | }// | ||
- | |||
- | function recordLayout(obj){ | ||
- | //create a new horizontal layout to display this record | ||
- | var newlay = app.CreateLayout( " | ||
- | var txt = app.CreateText(obj.name, | ||
- | newlay.AddChild(txt); | ||
- | txt = app.CreateText(obj.org, | ||
- | newlay.AddChild(txt); | ||
- | txt = app.CreateText(obj.reg, | ||
- | newlay.AddChild(txt); | ||
- | txt = app.CreateText(obj.reason, | ||
- | newlay.AddChild(txt); | ||
- | txt = app.CreateText(obj.timeIn, | ||
- | newlay.AddChild(txt); | ||
- | var btn = app.CreateButton(obj.timeOut, | ||
- | btn.obj = obj; | ||
- | btn.SetOnTouch(btnOut_OnClick); | ||
- | newlay.AddChild(btn); | ||
- | return newlay; | ||
- | }// | ||
- | |||
- | function saveJSONfile(){ | ||
- | //update copy of recArray on file | ||
- | var s = JSON.stringify(recArray); | ||
- | app.WriteFile(jsonPath, | ||
- | }// | ||
- | |||
- | function loadJSONfile(){ | ||
- | //read copy of recArray from file | ||
- | var s = app.ReadFile(jsonPath) | ||
- | recArray = JSON.parse(s) | ||
- | var len = recArray.length; | ||
- | for (var i=0; i< | ||
- | var obj = recArray[i]; | ||
- | layScroll.AddChild( recordLayout(obj) ); | ||
- | } | ||
- | scrollr.ScrollTo(0, | ||
- | }// | ||
- | |||
- | </ | ||
- | ===== Notes ===== | ||
- | This app was designed for a tablet. It is a replacement for an old paper form and is designed to be similar. | ||
- | |||
- | The final version is in use on a 7-inch tablet, which is mainly kept in landscape mode, though sometimes they rotate the tablet to make it easier to log-out a visitor who is way back the list. | ||
- | |||
- | At the suggestion of Jorge Ramirez, I have set the orientation to Landscape if it is being used on a phone. | ||
- | |||
- | The final version includes code to save previous files to a network server and the internal code to deal with date/time is significantly more robust, so that visitors who have not been logged out can be copied across to the live list as part of the backup process. |