Updated application sources 64/22364/1
authorPawel Sierszen <p.sierszen@samsung.com>
Mon, 2 Jun 2014 12:48:25 +0000 (14:48 +0200)
committerPawel Sierszen <p.sierszen@samsung.com>
Mon, 2 Jun 2014 12:48:25 +0000 (14:48 +0200)
Change-Id: Ic21d80ce5009ed80d7357881c4d305556db469e2
Signed-off-by: Pawel Sierszen <p.sierszen@samsung.com>
project/.project
project/config.xml
project/css/style.css
project/index.html
project/js/app.config.js
project/js/app.js
project/js/app.nfc.card.js
project/js/app.nfc.js
project/js/app.nfc.peer.js
project/js/app.ui.js
project/js/app.ui.templateManager.js

index 8352d79..196cd4d 100644 (file)
                        </arguments>
                </buildCommand>
                <buildCommand>
-                       <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
-                       <arguments>
-                       </arguments>
-               </buildCommand>
-               <buildCommand>
                        <name>json.validation.builder</name>
                        <arguments>
                        </arguments>
                                </dictionary>
                        </arguments>
                </buildCommand>
+               <buildCommand>
+                       <name>org.tizen.web.privilege.nature.PrivilegeBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.tizen.web.editor.css.nature.CSSBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
        </buildSpec>
        <natures>
                <nature>json.validation.nature</nature>
@@ -53,5 +58,7 @@
                <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
                <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
                <nature>org.tizen.web.project.builder.WebNature</nature>
+               <nature>org.tizen.web.privilege.nature.PrivilegeNature</nature>
+               <nature>org.tizen.web.editor.css.nature.CSSNature</nature>
        </natures>
 </projectDescription>
index b6a73ff..7126ae9 100644 (file)
@@ -1,40 +1,48 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<widget xmlns="http://www.w3.org/ns/widgets" xmlns:tizen="http://tizen.org/ns/widgets" id="http://sample-web-application.tizen.org/contacts-exchanger" version="2.2.0" viewmodes="maximized">
-       <tizen:app-control>
-               <tizen:src name="index.html"/>
-               <tizen:operation name="http://tizen.org/appcontrol/operation/nfc_empty_type"/>
-               <tizen:uri name=""/>
-               <tizen:mime name="*/*"/>
-       </tizen:app-control>
-       <tizen:app-control>
-               <tizen:src name="index.html"/>
-               <tizen:operation name="http://tizen.org/appcontrol/operation/nfc_well_known_type"/>
-               <tizen:uri name=""/>
-               <tizen:mime name="*/*"/>
-       </tizen:app-control>
-       <tizen:app-control>
-               <tizen:src name="index.html"/>
-               <tizen:operation name="http://tizen.org/appcontrol/operation/nfc_mime_type"/>
-               <tizen:uri name=""/>
-               <tizen:mime name="*/*"/>
-       </tizen:app-control>
-       <tizen:app-control>
-               <tizen:src name="index.html"/>
-               <tizen:operation name="http://tizen.org/appcontrol/operation/nfc_uri_type"/>
-               <tizen:uri name=""/>
-               <tizen:mime name="*/*"/>
-       </tizen:app-control>
-       <tizen:application id="CUflbBqLYv.ContactsExchanger" package="CUflbBqLYv" required_version="2.2"/>
-       <content src="index.html"/>
-       <icon src="icon.png"/>
-       <name>Contacts Exchanger</name>
-       <feature name="http://tizen.org/feature/screen.size.normal.720.1280"/>
-       <tizen:privilege name="http://tizen.org/privilege/application.launch"/>
-       <tizen:privilege name="http://tizen.org/privilege/contact.read"/>
-       <tizen:privilege name="http://tizen.org/privilege/contact.write"/>
-       <tizen:privilege name="http://tizen.org/privilege/nfc.admin"/>
-       <tizen:privilege name="http://tizen.org/privilege/nfc.common"/>
-       <tizen:privilege name="http://tizen.org/privilege/nfc.p2p"/>
-       <tizen:privilege name="http://tizen.org/privilege/nfc.tag"/>
-       <tizen:setting screen-orientation="portrait" context-menu="disable" background-support="enable" encryption="disable" install-location="auto"/>
+<widget xmlns="http://www.w3.org/ns/widgets"
+    xmlns:tizen="http://tizen.org/ns/widgets"
+    id="http://sample-web-application.tizen.org/contacts-exchanger"
+    version="2.2.0" viewmodes="maximized">
+    <tizen:app-control>
+        <tizen:src name="index.html"/>
+        <tizen:operation
+            name="http://tizen.org/appcontrol/operation/nfc_empty_type"/>
+        <tizen:uri name=""/>
+        <tizen:mime name="*/*"/>
+    </tizen:app-control>
+    <tizen:app-control>
+        <tizen:src name="index.html"/>
+        <tizen:operation
+            name="http://tizen.org/appcontrol/operation/nfc_well_known_type"/>
+        <tizen:uri name=""/>
+        <tizen:mime name="*/*"/>
+    </tizen:app-control>
+    <tizen:app-control>
+        <tizen:src name="index.html"/>
+        <tizen:operation name="http://tizen.org/appcontrol/operation/nfc_mime_type"/>
+        <tizen:uri name=""/>
+        <tizen:mime name="*/*"/>
+    </tizen:app-control>
+    <tizen:app-control>
+        <tizen:src name="index.html"/>
+        <tizen:operation name="http://tizen.org/appcontrol/operation/nfc_uri_type"/>
+        <tizen:uri name=""/>
+        <tizen:mime name="*/*"/>
+    </tizen:app-control>
+    <tizen:application id="CUflbBqLYv.ContactsExchanger"
+        package="CUflbBqLYv" required_version="2.2"/>
+    <content src="index.html"/>
+    <icon src="icon.png"/>
+    <name>Contacts Exchanger</name>
+    <feature name="http://tizen.org/feature/screen.size.normal.720.1280"/>
+    <tizen:privilege name="http://tizen.org/privilege/application.launch"/>
+    <tizen:privilege name="http://tizen.org/privilege/contact.read"/>
+    <tizen:privilege name="http://tizen.org/privilege/contact.write"/>
+    <tizen:privilege name="http://tizen.org/privilege/nfc.admin"/>
+    <tizen:privilege name="http://tizen.org/privilege/nfc.common"/>
+    <tizen:privilege name="http://tizen.org/privilege/nfc.p2p"/>
+    <tizen:privilege name="http://tizen.org/privilege/nfc.tag"/>
+    <tizen:setting screen-orientation="portrait"
+        context-menu="disable" background-support="enable" encryption="disable"
+        install-location="auto"/>
 </widget>
index b459631..9836078 100644 (file)
+body {
+    -webkit-user-select: none;
+}
 .ui-btn-start {
-       margin: 0px auto;
-       margin-top: 10px;
-       padding: 40px 0px 0px 0px;
-       width: 90%;
-       height: 50px;
-       font-size: 16px;
-}
-
+    margin: 0px auto;
+    margin-top: 10px;
+    padding: 40px 0px 0px 0px;
+    width: 90%;
+    height: 50px;
+    font-size: 16px;
+}
 .defaultText {
-       font-size: 20px;
-       text-align: center;
+    font-size: 20px;
+    text-align: center;
 }
-
 .comment {
-       font-size: 16px;
-       text-align: center;
-       color: black;
-       padding: 0px;
-       margin: 0px 0px 12px 0px;
-       white-space: nowrap;
-       width: 300px;
-       overflow: hidden;
-       text-overflow: ellipsis;
-}
-
+    font-size: 16px;
+    text-align: center;
+    color: black;
+    padding: 0px;
+    margin: 0px 0px 12px 0px;
+    white-space: nowrap;
+    width: 300px;
+    overflow: hidden;
+    text-overflow: ellipsis;
+}
 #content-start {
-       display: table;
+    display: table;
 }
-
 #comment {
-       left: 0px;
-       margin: 0 auto;
-       right: 0px;
-       width: inherit;
+    left: 0px;
+    margin: 0 auto;
+    right: 0px;
+    width: inherit;
 }
-
 .box {
-       margin: 0px;
-       padding: 0 32px;
-       width: 296px;
-       text-align: center;
-       display: table-cell;
-       vertical-align: middle;
-}
-
+    margin: 0px;
+    padding: 0 32px;
+    width: 296px;
+    text-align: center;
+    display: table-cell;
+    vertical-align: middle;
+}
 #waitingBox {
-       height: inherit;
+    height: inherit;
 }
-
 .gap {
-       width: 100%;
-       height: 10px;
+    width: 100%;
+    height: 10px;
 }
-
 #list-choose li a {
-       white-space: nowrap;
-       overflow: hidden !important;
-       text-overflow: ellipsis;
-       width: 60%;
-       float: left;
+    white-space: nowrap;
+    overflow: hidden !important;
+    text-overflow: ellipsis;
+    width: 60%;
+    float: left;
 }
-
 #list-choose li .ui-li-text-sub {
-       white-space: nowrap;
-       overflow: hidden !important;
-       text-overflow: ellipsis;
-       width: 40%;
+    white-space: nowrap;
+    overflow: hidden !important;
+    text-overflow: ellipsis;
+    width: 40%;
 }
-
 #list-choose li.ui-btn-down-s a {
-       color: black;
+    color: black;
 }
-
 #comment-userName {
-       line-height: 30px;
-       font-size: 24px;
+    line-height: 30px;
+    font-size: 24px;
 }
-
 #comment-name {
-       overflow: hidden !important;
-       text-overflow: ellipsis;
-       width: 90%;
-       margin-left: 5%;
-       font-size: 1rem;
-       font-weight: bold;
-}
-
+    overflow: hidden !important;
+    text-overflow: ellipsis;
+    width: 90%;
+    margin-left: 5%;
+    font-size: 1rem;
+    font-weight: bold;
+}
 #comment-phone {
-       color: #85837E;
-       line-height: 20px;
+    color: #85837E;
+    line-height: 20px;
 }
-
 #accept-choose {
-       width: auto;
+    width: auto;
 }
-
 .defaultVeryBigText {
-       font-size: 30px;
-       margin: 0px;
-       padding: 0px;
-       text-align: center;
-       line-height: 30px;
+    font-size: 30px;
+    margin: 0px;
+    padding: 0px;
+    text-align: center;
+    line-height: 30px;
 }
-
 .defaultCounterText {
-       margin-top: 10px;
-       margin-bottom: 10px;
-       font-size: 100px;
-       padding: 0px;
-       text-align: center;
-       line-height: 100px;
-}
-
+    margin-top: 10px;
+    margin-bottom: 10px;
+    font-size: 100px;
+    padding: 0px;
+    text-align: center;
+    line-height: 100px;
+}
 .ui-btn-create span.ui-btn-text {
-       font-size: .8rem;
+    font-size: .8rem;
 }
-
 #header-start .ui-title, #header-choose .ui-title, #header-contact .ui-title {
-       font-size: 1.1rem;
+    font-size: 1.1rem;
 }
-
 /* overwrite web-ui-fw styles for popupwindow button */
-.ui-popupwindow .center_basic_1btn .popup-button-bg .ui-btn {
-       width: 90%;
-       height: auto;
+ .ui-popupwindow .center_basic_1btn .popup-button-bg .ui-btn {
+    width: 90%;
+    height: auto;
 }
 #list-choose li.selected {
-       background-color: #ccc;
-       margin-top: -1px;
+    background-color: #ccc;
+    margin-top: -1px;
 }
-
 .ui-content {
-       padding: 0;
+    padding: 0;
 }
-
 .ui-btn-back {
-       visibility:hidden;
+    visibility:hidden;
 }
 /* temporary workaround for N_SE-46139 */
-.ui-listview .ui-li > .ui-btn-inner {
-       padding: 0 0.5909090909090909rem !important;
-       margin: 0 !important;
+ .ui-listview .ui-li > .ui-btn-inner {
+    padding: 0 0.5909090909090909rem !important;
+    margin: 0 !important;
 }
+li#vCard {
+    display: none;
+}
\ No newline at end of file
index 050679e..ae2f7d4 100644 (file)
@@ -2,82 +2,82 @@
 <html>
 
 <head>
-       <meta charset="utf-8"/>
-       <meta name="description" content="Contacts-exchanger"/>
-       <meta name="viewport" content="width=360, user-scalable=no"/>
+    <meta charset="utf-8" />
+    <meta name="description" content="Contacts-exchanger" />
+    <meta name="viewport" content="width=360, user-scalable=no" />
 
-       <title>Contacts exchanger</title>
+    <title>Contacts exchanger</title>
 
-       <script src="tizen-web-ui-fw/latest/js/jquery.min.js"></script>
-       <script src="tizen-web-ui-fw/latest/js/tizen-web-ui-fw-libs.min.js"></script>
-       <script src="tizen-web-ui-fw/latest/js/tizen-web-ui-fw.min.js" data-framework-theme="tizen-white"></script>
+    <script src="/usr/share/tizen-web-ui-fw/latest/js/jquery.min.js"></script>
+    <script src="/usr/share/tizen-web-ui-fw/latest/js/tizen-web-ui-fw-libs.min.js"></script>
+    <script src="/usr/share/tizen-web-ui-fw/latest/js/tizen-web-ui-fw.min.js" data-framework-theme="tizen-white"></script>
 
-       <script defer="defer" type="text/javascript" src="./js/app.js"></script>
-       <script defer="defer" type="text/javascript" src="./js/app.config.js"></script>
-       <script defer="defer" type="text/javascript" src="./js/app.ui.js"></script>
-       <script defer="defer" type="text/javascript" src="./js/app.ui.templateManager.js"></script>
-       <script defer="defer" type="text/javascript" src="./js/app.nfc.js"></script>
-       <script defer="defer" type="text/javascript" src="./js/app.nfc.card.js"></script>
-       <script defer="defer" type="text/javascript" src="./js/app.nfc.peer.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.config.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.ui.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.ui.templateManager.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.nfc.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.nfc.card.js"></script>
+    <script defer="defer" type="text/javascript" src="./js/app.nfc.peer.js"></script>
 
-       <link rel="stylesheet" type="text/css" href="./css/style.css"/>
+    <link rel="stylesheet" type="text/css" href="./css/style.css" />
 </head>
 
 <body>
-       <div id="start" data-role="page">
-               <div data-role="header" id="header-start">
-                       <h1>Contacts exchanger</h1>
-               </div>
-               <div data-role="content" id="content-start"></div>
-               <div id="contact-nfc-error" data-role="popup" class="center_title_1btn">
-                       <div class="ui-popup-title"><h1>Warning</h1></div>
-                       <div class="ui-popup-text">
-                               If you want to use "Contact Exchanger" you must agree to access
-                               Contacts and NFC by this application.
-                       </div>
-                       <div class="ui-popup-button-bg">
-                               <a data-role="button" data-rel="back" data-inline="true"
-                                       id="acceptWarning">OK</a>
-                       </div>
-               </div>
-       </div>
+    <div id="start" data-role="page">
+        <div data-role="header" id="header-start">
+            <h1>Contacts exchanger</h1>
+        </div>
+        <div data-role="content" id="content-start"></div>
+        <div id="contact-nfc-error" data-role="popup" class="center_title_1btn">
+            <div class="ui-popup-title"><h1>Warning</h1></div>
+            <div class="ui-popup-text">
+                If you want to use "Contact Exchanger" you must agree to access
+                Contacts and NFC by this application.
+            </div>
+            <div class="ui-popup-button-bg">
+                <a data-role="button" data-rel="back" data-inline="true"
+                    id="acceptWarning">OK</a>
+            </div>
+        </div>
+    </div>
 
-       <div id="choose" data-role="page" data-footer-exist="true">
-               <div data-role="header" id="header-choose" data-position="fixed">
-                       <h1>Contacts list</h1>
-               </div>
+    <div id="choose" data-role="page" data-footer-exist="true">
+        <div data-role="header" id="header-choose" data-position="fixed">
+            <h1>Contacts list</h1>
+        </div>
 
-               <div data-role="content" id="content-choose"></div>
-       </div>
+        <div data-role="content" id="content-choose"></div>
+    </div>
 
-       <div id="transfer" data-role="page">
-               <div data-role="header" id="header-transfer" data-position="fixed">
-                       <h1></h1>
-               </div>
+    <div id="transfer" data-role="page">
+        <div data-role="header" id="header-transfer" data-position="fixed">
+            <h1></h1>
+        </div>
 
-               <div data-role="content" id="content-transfer"></div>
-       </div>
+        <div data-role="content" id="content-transfer"></div>
+    </div>
 
-       <div id="contact" data-role="page" data-footer-exist="true">
-               <div data-role="header" id="header-contact" data-position="fixed">
-                       <h1>Add contact</h1>
-                       <a data-role="button" id="save-contact">Save</a>
-               </div>
+    <div id="contact" data-role="page" data-footer-exist="true">
+        <div data-role="header" id="header-contact" data-position="fixed">
+            <h1>Add contact</h1>
+            <a data-role="button" id="save-contact">Save</a>
+        </div>
 
-               <div data-role="content" id="content-contact"></div>
+        <div data-role="content" id="content-contact"></div>
 
-               <div data-role="footer" id="footer-contact" data-position="fixed"></div>
-       </div>
-       <div id="popupContainer" data-role="page">
-               <div data-role="popup" id="alertPopup">
-                       <div class="ui-popup-text"></div>
-                       <div class="ui-popup-button-bg">
-                               <a data-role="button" data-rel="back" data-inline="true">
-                                       OK
-                               </a>
-                       </div>
-               </div>
-       </div>
+        <div data-role="footer" id="footer-contact" data-position="fixed"></div>
+    </div>
+    <div id="popupContainer" data-role="page">
+        <div data-role="popup" id="alertPopup">
+            <div class="ui-popup-text"></div>
+            <div class="ui-popup-button-bg">
+                <a data-role="button" data-rel="back" data-inline="true">
+                    OK
+                </a>
+            </div>
+        </div>
+    </div>
 </body>
 
 </html>
index e0332c3..568351f 100644 (file)
@@ -1,28 +1,45 @@
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
+
 /**
  * @class Config
  */
+
 function Config() {
-       'use strict';
+    'use strict';
 }
 
 (function () { // strict mode wrapper
-       'use strict';
-       Config.prototype = {
+    'use strict';
+    Config.prototype = {
 
-               properties: {
-                       'templateDir': 'templates',
-                       'templateExtension': '.tpl'
-               },
+        properties: {
+            'templateDir': 'templates',
+            'templateExtension': '.tpl'
+        },
 
-               /**
-                * Returns config value
-                */
-               get: function (value, defaultValue) {
+        /**
+         * Returns config value
+         */
+        get: function (value, defaultValue) {
 
-                       if (this.properties.hasOwnProperty(value)) {
-                               return this.properties[value];
-                       }
-                       return defaultValue;
-               }
-       };
-}());
+            if (this.properties.hasOwnProperty(value)) {
+                return this.properties[value];
+            }
+            return defaultValue;
+        }
+    };
+}());
\ No newline at end of file
index c38e777..404b53b 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 /*jslint devel: true*/
-/*global $, tizen, Config */
+/*global $, tizen, Config, localStorage */
 
 var App = null;
 
@@ -23,213 +23,207 @@ var App = null;
 var app = null;
 
 (function () { // strict mode wrapper
-       'use strict';
-
-       /**
-        * Creates a new application object
-        *
-        * @class Application
-        */
-       App = function App() {};
-
-       App.prototype = {
-               nfcAdapter: null,
-               addressBook: null,
-               started: false,
-               timeOutHandler: null,
-               counterState: true,
-               nfc: null,
-
-               init: function appInit() {
-                       this.config = new Config();
-                       this.ui = new App.Ui(this);
-                       this.nfc = new App.NFCControl(this);
-                       this.ui.defineEvents();
-                       this.initAddressBook(this.nfc.startNFC.bind(this.nfc));
-               },
-
-               saveDefaultCard: function saveDefaultCard() {
-                       var elementSelected = $('#list-choose li.selected');
-                       localStorage.started = true;
-                       localStorage.id = elementSelected.data('id');
-                       localStorage.caller = elementSelected.data('caller');
-                       localStorage.firstName = elementSelected.data('firstName');
-                       localStorage.lastName = elementSelected.data('lastName');
-                       localStorage.phoneNumber = elementSelected.data('phoneNumber');
-                       localStorage.vCard = elementSelected.data('vCard');
-                       this.started = true;
-                       $.mobile.changePage('#start');
-               },
-
-               updateDefaultCard: function updateDefaultCard (contact) {
-                       localStorage.caller =  this.ui.prepareCallerName(contact);
-                       localStorage.firstName = contact.name.firstName || '';
-                       localStorage.lastName = contact.name.lastName || '';
-                       localStorage.phoneNumber =  contact.phoneNumbers[0].number;
-                       localStorage.id = contact.id;
-                       localStorage.vCard = contact.convertToString('VCARD_30');
-               },
-
-               /**
-                * @param {Array} addressbooks
-                */
-               getAddressBooksSuccess: function getAddressBooksSuccess(addressbooks) {
-                       if (addressbooks.length > 0) {
-                               var self = this,
-                               resetLocalSorage = function () {
-                                       // Reset localStorage
-                                       localStorage.clear();
-                                       self.started = false;
-
-                                       // Load start page with temporary content
-                                       self.ui.moveToStartPage();
-                                       self.ui.loadTemporaryContent();
-                               };
-                               this.addressBook =  addressbooks[0];
-
-                               if (localStorage.id) {
-                                       try {
-                                               this.updateDefaultCard(
-                                                       this.addressBook.get(localStorage.id)
-                                               );
-                                       } catch (err) {
-                                               if (err.name === "NotFoundError") {
-                                                       resetLocalSorage();
-                                               }
-                                       }
-                               }
-
-                               // Registers to be notified when the address book changes
-                               this.addressBook.addChangeListener({
-                                               oncontactsadded: function(contacts) {
-                                                       // Refresh if choose page active
-                                                       self.ui.refreshIfActivePage("choose");
-                                               },
-                                               oncontactsremoved: function(ids) {
-                                                       // Refresh localStorage if default contact was deleted
-                                                       if (ids.indexOf(localStorage.id) >= 0) {
-                                                               resetLocalSorage();
-                                                               alert("Your default contact has been removed. Please choose another one.");
-                                                       } else {
-                                                               // Refresh if choose page active
-                                                               self.ui.refreshIfActivePage("choose");
-                                                       }
-                                               },
-                                               oncontactsupdated: function (contacts) {
-                                                       var index = contacts.length;
-
-                                                       // check if default contact was updated
-                                                       while (index--) {
-                                                               if (contacts[index].id === localStorage.id) {
-                                                                       self.updateDefaultCard(contacts[index]);
-                                                                       break;
-                                                               }
-                                                       }
-
-                                                       self.ui.refreshIfActivePage("start");
-                                               }
-                               });
-                       } else {
-                               console.error('initAddressBook failed');
-                       }
-               },
-
-               /**
-                *
-                * @param {Error} e
-                */
-               getAddressBooksError: function getAddressBooksError(e) {
-                       console.error('getAddressBooks() error: ' + e.message);
-               },
-
-               initAddressBook: function initAddressBook(callback) {
-                       try {
-                               tizen.contact.getAddressBooks(
-                                       function (addressbook) {
-                                               this.getAddressBooksSuccess(addressbook)
-                                               callback();
-                                       }.bind(this),
-                                       this.getAddressBooksError.bind(this)
-                               );
-                       } catch (e) {
-                               if (e.name === "SecurityError") {
-                                       this.ui.showPopupWarning();
-                               }
-                               console.error('getAddressBooks() error: ' + e.message);
-                       }
-               },
-
-               countDown: function countDown(time, obj) {
-                       if (!this.counterState) {
-                               setTimeout(function () {
-                                       this.countDown(time, obj);
-                               }.bind(this), 500);
-                               return;
-                       }
-
-                       obj.text(time);
-                       if (time > 0) {
-                               if (this.nfc.isPowered()) {
-                                       time -= 1;
-                                       this.timeOutHandler = setTimeout(function () {
-                                               this.countDown(time, obj);
-                                       }.bind(this), 1000);
-                               } else {
-                                       this.nfc.timeExpired();
-                               }
-                       } else {
-                               this.nfc.timeExpired();
-                       }
-               },
-
-               saveContact: function saveContact() {
-                       var contact = null, i, empty = true,
-                               data = $('#contact').data('contactsData');
-
-                       for (i in data) {
-                               if (data.hasOwnProperty(i) && empty) {
-                                       if (data[i] !== '') {
-                                               empty = false;
-                                       }
-                               }
-                       }
-
-                       if (empty) {
-                               this.ui.moveToStartPage('Cannot add empty contact');
-                               console.error('saveContact error: empty contact');
-                       } else {
-                               try {
-                                       contact = new tizen.Contact({
-                                               name: new tizen.ContactName({
-                                                       caller: data.caller,
-                                                       firstName: data.first,
-                                                       lastName: data.last
-                                               }),
-                                               phoneNumbers: [new tizen.ContactPhoneNumber(data.phone)]
-                                       });
-                                       this.addressBook.add(contact);
-                                       this.ui.moveToStartPage('New contact added');
-                               } catch (err) {
-                                       this.ui.moveToStartPage('Problem with new contact adding');
-                                       console.error('saveContact error:' + err.name);
-                               }
-                       }
-               },
-
-               /**
-                * Load contacts from the address book and pass the result to a callback
-                *
-                * @param {function} successCallback
-                * @param {function} errorCallback
-                */
-               loadContacts: function loadContacts(successCallback, errorCallback) {
-                       this.addressBook.find(successCallback, errorCallback);
-               }
-
-       };
+    'use strict';
+
+    /**
+     * Creates a new application object
+     *
+     * @class Application
+     */
+    App = function App() {};
+
+    App.prototype = {
+        nfcAdapter: null,
+        addressBook: null,
+        started: false,
+        timeOutHandler: null,
+        counterState: true,
+        nfc: null,
+
+        init: function appInit() {
+            this.config = new Config();
+            this.ui = new App.Ui(this);
+            this.nfc = new App.NFCControl(this);
+            this.ui.defineEvents();
+            this.initAddressBook(this.nfc.startNFC.bind(this.nfc));
+        },
+
+        saveDefaultCard: function saveDefaultCard() {
+            var elementSelected = $('#list-choose li.selected');
+            localStorage.started = true;
+            localStorage.id = elementSelected.data('id');
+            localStorage.caller = elementSelected.data('caller');
+            localStorage.firstName = elementSelected.data('firstName');
+            localStorage.middleName = elementSelected.data('middleName');
+            localStorage.lastName = elementSelected.data('lastName');
+            localStorage.phoneNumber = elementSelected.data('phoneNumber');
+            localStorage.vCard = elementSelected.data('vCard');
+            this.started = true;
+
+            $.mobile.changePage('#start');
+        },
+
+        updateDefaultCard: function updateDefaultCard(contact) {
+            localStorage.caller = this.ui.prepareCallerName(contact);
+            localStorage.firstName = contact.name.firstName || '';
+            localStorage.middleName = contact.name.middleName || '';
+            localStorage.lastName = contact.name.lastName || '';
+            localStorage.phoneNumber = contact.phoneNumbers[0].number;
+            localStorage.id = contact.id;
+            localStorage.vCard = contact.convertToString('VCARD_30');
+        },
+
+        /**
+         * @param {Array} addressbooks
+         */
+        getAddressBooksSuccess: function getAddressBooksSuccess(addressbooks) {
+            if (addressbooks.length > 0) {
+                var self = this,
+                    resetLocalSorage = function () {
+                        // Reset localStorage
+                        localStorage.clear();
+                        self.started = false;
+
+                        // Load start page with temporary content
+                        self.ui.moveToStartPage();
+                        self.ui.loadTemporaryContent();
+                    };
+                this.addressBook = addressbooks[0];
+
+                if (localStorage.id) {
+                    try {
+                        this.updateDefaultCard(
+                            this.addressBook.get(localStorage.id)
+                        );
+                    } catch (err) {
+                        if (err.name === 'NotFoundError') {
+                            resetLocalSorage();
+                        }
+                    }
+                }
+
+                // Registers to be notified when the address book changes
+                this.addressBook.addChangeListener({
+                    oncontactsadded: function (contacts) {
+                        // Refresh if choose page active
+                        self.ui.refreshIfActivePage('choose');
+                    },
+                    oncontactsremoved: function (ids) {
+                        // Refresh localStorage if default contact was deleted
+                        if (ids.indexOf(localStorage.id) >= 0) {
+                            resetLocalSorage();
+                            alert('Your default contact has been removed. ' +
+                                'Please choose another one.');
+                        } else {
+                            // Refresh if choose page active
+                            self.ui.refreshIfActivePage('choose');
+                        }
+                    },
+                    oncontactsupdated: function (contacts) {
+                        var index = contacts.length;
+
+                        // check if default contact was updated
+                        while (index - 1) {
+                            if (contacts[index].id === localStorage.id) {
+                                try {
+                                    self.updateDefaultCard(contacts[index]);
+                                } catch (e) {
+                                    resetLocalSorage();
+                                    alert('That contact is no longer valid. ' +
+                                        'Choose another one.');
+                                } finally {
+                                    break;
+                                }
+                            }
+                            index -= 1;
+                        }
+
+                        self.ui.refreshIfActivePage('start');
+                    }
+                });
+            } else {
+                console.error('initAddressBook failed');
+            }
+        },
+
+        /**
+         *
+         * @param {Error} e
+         */
+        getAddressBooksError: function getAddressBooksError(e) {
+            console.error('getAddressBooks() error: ' + e.message);
+        },
+
+        initAddressBook: function initAddressBook(callback) {
+            try {
+                tizen.contact.getAddressBooks(function (addressbook) {
+                    this.getAddressBooksSuccess(addressbook);
+                    callback();
+                }.bind(this),
+                    this.getAddressBooksError.bind(this));
+            } catch (e) {
+                if (e.name === 'SecurityError') {
+                    this.ui.showPopupWarning();
+                }
+                console.error('getAddressBooks() error: ' + e.message);
+            }
+        },
+
+        countDown: function countDown(time, obj) {
+            if (!this.counterState) {
+                setTimeout(function () {
+                    this.countDown(time, obj);
+                }.bind(this), 500);
+                return;
+            }
+
+            obj.text(time);
+            if (time > 0) {
+                if (this.nfc.isPowered()) {
+                    time -= 1;
+                    this.timeOutHandler = setTimeout(function () {
+                        this.countDown(time, obj);
+                    }.bind(this), 1000);
+                } else {
+                    this.nfc.timeExpired();
+                }
+            } else {
+                this.nfc.timeExpired();
+            }
+        },
+
+        saveContact: function saveContact() {
+            var contact = null,
+                data = $('#contact').data('contactsData');
+            if (!(data.phone && (data.first || data.last))) {
+                this.ui.moveToStartPage('Cannot add empty contact');
+                console.error('saveContact error: empty contact');
+            } else {
+                try {
+                    contact = new tizen.Contact(data.vCard, 'VCARD_30');
+                    this.addressBook.add(contact);
+                    this.ui.moveToStartPage('New contact added');
+                } catch (err) {
+                    this.ui.moveToStartPage('Problem with new contact adding');
+                    console.error('saveContact error:' + err.name);
+                }
+            }
+        },
+
+        /**
+         * Load contacts from the address book and pass the result to a callback
+         *
+         * @param {function} successCallback
+         * @param {function} errorCallback
+         */
+        loadContacts: function loadContacts(successCallback, errorCallback) {
+            this.addressBook.find(successCallback, errorCallback);
+        }
+
+    };
 
 }());
 
 app = new App();
 
-$(document).ready(app.init.bind(app));
+$(document).ready(app.init.bind(app));
\ No newline at end of file
index d6ad313..a9e5e95 100644 (file)
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
+
 /*jslint devel: true*/
-/*global $, tizen, App, app */
+/*global $, tizen, App, app, localStorage */
 
 (function () { // strict mode wrapper
-       'use strict';
+    'use strict';
 
-       /**
-        * Constructs NFCCard
-        * @constructor
-        * @param {NFCControl} nfc
-        */
-       App.NFCCard = function nfc_NFCCard(nfc) {
-               this.nfc = nfc;
-       };
+    /**
+     * Constructs NFCCard
+     * @constructor
+     * @param {NFCControl} nfc
+     */
+    App.NFCCard = function nfc_NFCCard(nfc) {
+        this.nfc = nfc;
+    };
 
-       App.NFCCard.prototype = {
+    App.NFCCard.prototype = {
 
-               readMessageErr: function nfc_card_readMessageErr(e) {
-                       console.error('Read error! ' + e.message, e);
-                       this.nfc.timeExpired('Read error! ' + e.message);
-               },
+        readMessageErr: function nfc_card_readMessageErr(e) {
+            console.error('Read error! ' + e.message, e);
+            this.nfc.timeExpired('Read error! ' + e.message);
+        },
 
-               /**
-                * Read contents from a tag
-                * @param {NFCTag} tag
-                */
-               sucTagReadAttach: function nfc_card_sucTagReadAttach(tag) {
-                       try {
-                               if (!tag.isSupportedNDEF) {
-                                       throw {message: "This tag doesn't support NDEF"};
-                               }
-                               tag.readNDEF(
-                                       this.nfc.readMessage.bind(this.nfc),
-                                       this.readMessageErr.bind(this)
-                               );
-                       } catch (e) {
-                               this.readMessageErr(e);
-                       }
-               },
+        /**
+         * Read contents from a tag
+         * @param {NFCTag} tag
+         */
+        sucTagReadAttach: function nfc_card_sucTagReadAttach(tag) {
+            try {
+                if (!tag.isSupportedNDEF) {
+                    throw {
+                        message: "This tag doesn't support NDEF"
+                    };
+                }
+                tag.readNDEF(
+                    this.nfc.readMessage.bind(this.nfc),
+                    this.readMessageErr.bind(this)
+                );
+            } catch (e) {
+                this.readMessageErr(e);
+            }
+        },
 
-               /**
-                * Set tag listener
-                */
-               setTagDetectRead: function nfc_card_setTagDetectRead() {
-                       try {
-                               this.nfc.nfcAdapter.setTagListener({
-                                       onattach: this.sucTagReadAttach.bind(this),
-                                       ondetach: this.nfc.sucDetach.bind(this.nfc)
-                               });
-                       } catch (error) {
-                               this.readMessageErr(error);
-                       }
-               },
+        /**
+         * Set tag listener
+         */
+        setTagDetectRead: function nfc_card_setTagDetectRead() {
+            try {
+                this.nfc.nfcAdapter.unsetTagListener();
+                this.nfc.nfcAdapter.setTagListener({
+                    onattach: this.sucTagReadAttach.bind(this),
+                    ondetach: this.nfc.sucDetach.bind(this.nfc)
+                });
+            } catch (error) {
+                this.readMessageErr(error);
+            }
+        },
 
-               sucSend: function nfc_card_sucSend() {
-                       this.nfc.timeExpired('Send success!');
-               },
+        sucSend: function nfc_card_sucSend() {
+            this.nfc.timeExpired('Send success!');
+        },
 
-               errSend: function nfc_card_errSend(e) {
-                       console.warn('errSend', e);
-                       this.nfc.timeExpired('Write error! ' + e.message);
-               },
+        errSend: function nfc_card_errSend(e) {
+            console.warn('errSend', e);
+            this.nfc.timeExpired('Write error! ' + e.message);
+        },
 
-               sucTagWriteAttach: function nfc_card_sucTagWriteAttach(tag) {
-                       var newMessage = null,
-                               fullContact = '';
+        sucTagWriteAttach: function nfc_card_sucTagWriteAttach(tag) {
+            var newMessage = null,
+                fullContact = '';
 
-                       try {
-                               fullContact = this.nfc.prepareForNFC(localStorage);
-                               newMessage = this.nfc.phoneNumber2NDEF(fullContact);
-                               if (!tag.isSupportedNDEF) {
-                                       throw {message: "This tag doesn't support NDEF"};
-                               }
-                               tag.writeNDEF(
-                                       newMessage,
-                                       this.sucSend.bind(this),
-                                       this.errSend.bind(this)
-                               );
-                       } catch (e) {
-                               this.errSend(e);
-                       }
-               },
+            try {
+                fullContact = this.nfc.prepareForNFC(localStorage);
+                newMessage = this.nfc.phoneNumber2NDEF(fullContact);
+                if (!tag.isSupportedNDEF) {
+                    throw {
+                        message: "This tag doesn't support NDEF"
+                    };
+                }
+                tag.writeNDEF(
+                    newMessage,
+                    this.sucSend.bind(this),
+                    this.errSend.bind(this)
+                );
+            } catch (e) {
+                this.errSend(e);
+            }
+        },
 
-               setTagDetectWrite: function nfc_card_setTagDetectWrite() {
-                       var suc = {
-                               onattach: this.sucTagWriteAttach.bind(this),
-                               ondetach: this.nfc.sucDetach.bind(this.nfc)
-                       };
+        setTagDetectWrite: function nfc_card_setTagDetectWrite() {
+            var suc = {
+                onattach: this.sucTagWriteAttach.bind(this),
+                ondetach: this.nfc.sucDetach.bind(this.nfc)
+            };
 
-                       try {
-                               this.nfc.nfcAdapter.setTagListener(suc);
-                       } catch (error) {
-                               console.error(error);
-                       }
-               }
+            try {
+                this.nfc.nfcAdapter.unsetTagListener();
+                this.nfc.nfcAdapter.setTagListener(suc);
+            } catch (error) {
+                console.error(error);
+            }
+        }
 
-       };
+    };
 
-}());
+}());
\ No newline at end of file
index 4d86ffb..3bedee0 100644 (file)
-/*jslint devel: true*/
-/*global $, tizen, App, app */
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
 
+/*jslint devel: true*/
+/*global $, tizen, App, app */ 
 (function () { // strict mode wrapper
-       'use strict';
-
-       /**
-        * Constructs NFCControl
-        * @constructor
-        * @param {App} app
-        */
-       App.NFCControl = function NFCControl(app) {
-               this.app = app;
-               this.init();
-       };
-
-       App.NFCControl.prototype = {
-               nfcTarget: null,
-               nfcStateMemory: false,
-
-               /**
-                * Initialize NFC application module
-                */
-               init: function nfc_init() {
-                       this.card = new App.NFCCard(this);
-                       this.peer = new App.NFCPeer(this);
-                       this.separator = String.fromCharCode(30);
-                       this.endOfText = String.fromCharCode(3);
-               },
-               /**
-                * Disable tag/target detection and move to start page
-                *
-                * @param {string} monit message text to be displayed
-                */
-               timeExpired: function nfc_timeExpired(monit) {
-                       clearTimeout(this.app.timeOutHandler);
-                       this.unsetTargetDetect();
-                       this.unsetTagDetect();
-                       this.app.ui.moveToStartPage(monit);
-               },
+    'use strict';
 
-               /**
-                * Get the field separator
-                *
-                * @returns {string}
-                */
-               getSeparator: function nfc_getSeparator() {
-                       return this.separator;
-               },
+    /**
+     * Constructs NFCControl
+     * @constructor
+     * @param {App} app
+     */
+    App.NFCControl = function NFCControl(app) {
+        this.app = app;
+        this.init();
+    };
 
-               /**
-                * Get the end of text marker
-                *
-                * @returns {string}
-                */
-               getEndOfText: function nfc_getEndOfText() {
-                       return this.endOfText;
-               },
+    App.NFCControl.prototype = {
+        nfcTarget: null,
+        nfcStateMemory: false,
 
-               createContactString: function createContactString(contact) {
-                       var contactString;
-                       contactString = contact.phoneNumber + this.getSeparator();
-                       contactString += contact.firstName + this.getSeparator();
-                       contactString += contact.lastName;
-                       return contactString;
-               },
+        /**
+         * Initialize NFC application module
+         */
+        init: function nfc_init() {
+            this.card = new App.NFCCard(this);
+            this.peer = new App.NFCPeer(this);
+            this.endOfText = String.fromCharCode(3);
+        },
+        /**
+         * Disable tag/target detection and move to start page
+         *
+         * @param {string} monit message text to be displayed
+         */
+        timeExpired: function nfc_timeExpired(monit) {
+            clearTimeout(this.app.timeOutHandler);
+            if (tizen.nfc.getDefaultAdapter().powered) {
+                this.unsetTargetDetect();
+                this.unsetTagDetect();
+            }
+            this.app.ui.moveToStartPage(monit);
+        },
 
-               /**
-                *
-                * @param {string|object} contact
-                * @returns {string}
-                */
-               prepareForNFC: function nfc_prepareForNFC(contact) {
-                       if ($.type(contact) !== 'string') {
-                               contact = this.createContactString(contact);
-                       }
-                       if (contact.length > 31) {
-                               contact = contact.substring(0, 31);
-                               if (contact[29] !== this.getSeparator()) {
-                                       contact = contact.substring(0, 30) + this.getEndOfText();
-                               }
-                       }
-                       return contact;
-               },
+        /**
+         * Get the end of text marker
+         *
+         * @returns {string}
+         */
+        getEndOfText: function nfc_getEndOfText() {
+            return this.endOfText;
+        },
 
-               resolveData: function nfc_resolveData(value, endOfText) {
-                       if (!value) {
-                               return '';
-                       }
+        /**
+         *
+         * @param {string|object} contact
+         * @returns {string}
+         */
+        prepareForNFC: function nfc_prepareForNFC(contact) {
+            return contact.vCard;
+        },
 
-                       endOfText = endOfText || this.getEndOfText();
+        resolveData: function nfc_resolveData(value, endOfText) {
+            if (!value) {
+                return '';
+            }
+            endOfText = endOfText || this.getEndOfText();
+            return value.replace(endOfText, '…');
+        },
 
-                       return value.replace(endOfText, '…');
-               },
 
-               resolveContact: function nfc_resolveContact(contactsString) {
-                       var separator = this.getSeparator(),
-                               contactsArray = contactsString.split(separator);
+        resolveContact: function nfc_resolveContact(contactsString) {
+            var receivedContact = new tizen.Contact(contactsString, 'VCARD_30');
 
-                       return {
-                               phone: this.resolveData(contactsArray[0]),
-                               first: this.resolveData(contactsArray[1]),
-                               last: this.resolveData(contactsArray[2])
-                       };
-               },
+            return {
+                phone: receivedContact.phoneNumbers[0].number,
+                phone_type: receivedContact.phoneNumbers[0].types[0],
+                first: receivedContact.name.firstName,
+                middle: receivedContact.name.middleName,
+                last: receivedContact.name.lastName,
+                vCard: receivedContact.convertToString('VCARD_30')
+            };
+        },
 
-               /**
-                *
-                * @param {type} record
-                * @returns {undefined}
-                */
-               fillRecordInfo: function nfc_fillRecordInfo(record) {
-                       try {
-                               var contactsData = this.resolveContact(this.convertNDEF2phoneNumber(record.payload));
-                               this.app.nfc.displayContact(contactsData);
-                       } catch (error) {
-                               console.error(error);
-                       }
-               },
+        /**
+         *
+         * @param {type} record
+         * @returns {undefined}
+         */
+        fillRecordInfo: function nfc_fillRecordInfo(record) {
+            try {
+                var contactsData = this.resolveContact(
+                    this.convertNDEF2phoneNumber(record.payload)
+                );
+                this.app.nfc.displayContact(contactsData);
+            } catch (error) {
+                console.error(error);
+            }
+        },
 
-               /**
-                *
-                * @param {NDEFMessage} message
-                */
-               readMessage: function nfc_readMessage(message) {
-                       try {
-                               this.fillRecordInfo(message.records[0]);
-                       } catch (e) {
-                               console.error(e.message);
-                       }
-               },
+        /**
+         *
+         * @param {NDEFMessage} message
+         */
+        readMessage: function nfc_readMessage(message) {
+            try {
+                this.fillRecordInfo(message.records[0]);
+            } catch (e) {
+                console.error(e.message);
+            }
+        },
 
-               /**
-                *
-                * @param {string|object} contact
-                * @returns {NDEFMessage}
-                */
-               contact2NDEF: function nfc_contact2NDEF(contact) {
-                       var t, a = [], len, i, newMessage = new tizen.NDEFMessage();
-                       if (typeof contact === 'string') {
-                               t = contact;
-                       } else {
-                               t = contact.convertToString("VCARD_30");
-                       }
-                       len = t.length;
-                       for (i = 0; i < len; i += 1) {
-                               a[i] = t.charCodeAt(i);
-                       }
-                       newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', a);
-                       return newMessage;
-               },
+        /**
+         *
+         * @param {string|object} contact
+         * @returns {NDEFMessage}
+         */
+        contact2NDEF: function nfc_contact2NDEF(contact) {
+            var t, a = [],
+                len, i, newMessage = new tizen.NDEFMessage();
+            if (typeof contact === 'string') {
+                t = contact;
+            } else {
+                t = contact.convertToString('VCARD_30');
+            }
+            len = t.length;
+            for (i = 0; i < len; i += 1) {
+                a[i] = t.charCodeAt(i);
+            }
+            newMessage.records[0] =
+                new tizen.NDEFRecordMedia('text/x-vcard', a);
+            return newMessage;
+        },
 
-               /**
-                *
-                * @param {string} contact
-                * @returns {NDEFMessage}
-                */
-               phoneNumber2NDEF: function nfc_phoneNumber2NDEF(contact) {
-                       var phoneNumberArray = [], i, length = contact.length, newMessage = new tizen.NDEFMessage();
-                       for (i = 0; i < length; i += 1) {
-                               phoneNumberArray.push(contact.charCodeAt(i));
-                       }
-                       newMessage.records[0] = new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
-                       return newMessage;
-               },
+        /**
+         *
+         * @param {string} contact
+         * @returns {NDEFMessage}
+         */
+        phoneNumber2NDEF: function nfc_phoneNumber2NDEF(contact) {
+            var phoneNumberArray = [],
+                i,
+                length = contact.length,
+                newMessage = new tizen.NDEFMessage();
+            for (i = 0; i < length; i += 1) {
+                phoneNumberArray.push(contact.charCodeAt(i));
+            }
+            newMessage.records[0] =
+                new tizen.NDEFRecordMedia('text/x-vcard', phoneNumberArray);
+            return newMessage;
+        },
 
-               /**
-                *
-                * @param {string} contact
-                * @returns {string}
-                */
-               convertNDEF2phoneNumber: function nfc_convertNDEF2phoneNumber(contact) {
-                       var i, length = contact.length, phoneNumber = '';
-                       for (i = 0; i < length; i += 1) {
-                               phoneNumber += String.fromCharCode(contact[i]);
-                       }
-                       return phoneNumber;
-               },
+        /**
+         *
+         * @param {string} contact
+         * @returns {string}
+         */
+        convertNDEF2phoneNumber: function nfc_convertNDEF2phoneNumber(contact) {
+            var i, length = contact.length,
+                phoneNumber = '';
+            for (i = 0; i < length; i += 1) {
+                phoneNumber += String.fromCharCode(contact[i]);
+            }
+            return phoneNumber;
+        },
 
-               /**
-                * NFC setPowered success callback
-                * @returns {undefined}
-                */
-               onPowerOn: function nfc_onPowerOn() {
-                       console.log('Power On succeeded.');
-                       this.app.started = true;
-                       this.app.ui.loadStartPage();
-               },
+        /**
+         * NFC setPowered success callback
+         * @returns {undefined}
+         */
+        onPowerOn: function nfc_onPowerOn() {
+            console.log('Power On succeeded.');
+            this.app.started = true;
+            this.app.ui.loadStartPage();
+        },
 
-               /**
-                * NFC setPowered error callback
-                * @param {Error} err
-                * @returns {undefined}
-                */
-               onPowerOnFails: function nfc_onPowerOnFails(err) {
-                       console.error('Power On error: ' + err.message);
-                       this.app.ui.showPopupWarning();
-               },
+        /**
+         * NFC setPowered error callback
+         * @param {Error} err
+         * @returns {undefined}
+         */
+        onPowerOnFails: function nfc_onPowerOnFails(err) {
+            console.error('Power On error: ' + err.message);
+            this.app.ui.showPopupWarning();
+        },
 
-               /**
-                * NFC setPowered success callback
-                * @returns {undefined}
-                */
-               onPowerOff: function nfc_onPowerOff() {
-                       tizen.application.getCurrentApplication().exit();
-               },
+        /**
+         * NFC setPowered success callback
+         * @returns {undefined}
+         */
+        onPowerOff: function nfc_onPowerOff() {
+            tizen.application.getCurrentApplication().exit();
+        },
 
-               /**
-                * NFC setPowered error callback
-                * @param {Error} err
-                * @returns {undefined}
-                */
-               onPowerOffFails: function nfc_onPowerOffFails(err) {
-                       console.error('Power Off error', err);
-                       tizen.application.getCurrentApplication().exit();
-               },
+        /**
+         * NFC setPowered error callback
+         * @param {Error} err
+         * @returns {undefined}
+         */
+        onPowerOffFails: function nfc_onPowerOffFails(err) {
+            console.error('Power Off error', err);
+            tizen.application.getCurrentApplication().exit();
+        },
 
-               isPowered: function nfc_isPowered() {
-                       return this.nfcAdapter.powered;
-               },
+        isPowered: function nfc_isPowered() {
+            return this.nfcAdapter.powered;
+        },
 
-               startNFC: function nfc_startNFC() {
-                       try {
-                               this.nfcAdapter = tizen.nfc.getDefaultAdapter();
-                               if (this.nfcAdapter.powered) {
-                                       this.nfcStateMemory = true;
-                                       this.onPowerOn();
-                                       return;
-                               }
-                               this.nfcStateMemory = false;
-                               console.log('Turning NFC adapter On...');
-                               this.nfcAdapter.setPowered(true, this.onPowerOn.bind(this), this.onPowerOnFails.bind(this));
-                       } catch (e) {
-                               console.error('startNFC problem', e);
-                               this.app.ui.showPopupWarning();
-                       }
-               },
+        startNFC: function nfc_startNFC() {
+            try {
+                this.nfcAdapter = tizen.nfc.getDefaultAdapter();
+                if (this.nfcAdapter.powered) {
+                    this.nfcStateMemory = true;
+                    this.onPowerOn();
+                    return;
+                }
+                this.nfcStateMemory = false;
+                console.log('Turning NFC adapter On...');
+                this.nfcAdapter.setPowered(
+                    true,
+                    this.onPowerOn.bind(this),
+                    this.onPowerOnFails.bind(this)
+                );
+            } catch (e) {
+                console.error('startNFC problem', e);
+                this.app.ui.showPopupWarning();
+            }
+        },
 
-               stopNFC: function nfc_stopNFC() {
-                       try {
-                               if (this.nfcStateMemory) {
-                                       this.onPowerOff();
-                               } else {
-                                       this.nfcAdapter.setPowered(false, this.onPowerOff.bind(this), this.onPowerOffFails.bind(this));
-                               }
-                       } catch (err) {
-                               console.error('setPowered(false) problem', err);
-                       }
-               },
+        stopNFC: function nfc_stopNFC() {
+            try {
+                if (this.nfcStateMemory) {
+                    this.onPowerOff();
+                } else {
+                    this.nfcAdapter.setPowered(
+                        false,
+                        this.onPowerOff.bind(this),
+                        this.onPowerOffFails.bind(this)
+                    );
+                }
+            } catch (err) {
+                console.error('setPowered(false) problem', err);
+            }
+        },
 
-               unsetTargetDetect: function nfc_unsetTargetDetect() {
-                       try {
-                               if (this.nfcTarget) {
-                                       this.nfcTarget.unsetReceiveNDEFListener();
-                                       this.nfcTarget = null;
-                               } else {
-                                       //console.warn("app.nfc.nfcTarget not set");
-                               }
-                       } catch (error) {
-                               console.error('error: ' + error.message);
-                       }
-                       try {
-                               this.nfcAdapter.unsetPeerListener();
-                       } catch (e) {
-                               console.error('error: ' + e.message);
-                       }
-               },
+        unsetTargetDetect: function nfc_unsetTargetDetect() {
+            try {
+                if (this.nfcTarget) {
+                    this.nfcTarget.unsetReceiveNDEFListener();
+                    this.nfcTarget = null;
+                } else {
+                    //console.info('app.nfc.nfcTarget not set');
+                }
+            } catch (error) {
+                console.error('error: ' + error.message);
+            }
+            try {
+                this.nfcAdapter.unsetPeerListener();
+            } catch (e) {
+                console.error('error: ' + e.message);
+            }
+        },
 
-               unsetTagDetect: function nfc_unsetTagDetect() {
-                       try {
-                               this.nfcAdapter.unsetTagListener();
-                       } catch (error) {
-                               console.error('error: ' + error.message);
-                       }
-               },
+        unsetTagDetect: function nfc_unsetTagDetect() {
+            try {
+                this.nfcAdapter.unsetTagListener();
+            } catch (error) {
+                console.error('error: ' + error.message);
+            }
+        },
 
-               displayContact: function nfc_displayContact(obj) {
-                       clearTimeout(this.app.timeOutHandler);
-                       this.unsetTargetDetect();
-                       this.unsetTagDetect();
-                       this.app.ui.moveToContactPage(obj);
-               },
+        displayContact: function nfc_displayContact(obj) {
+            clearTimeout(this.app.timeOutHandler);
+            if (tizen.nfc.getDefaultAdapter().powered) {
+                this.unsetTargetDetect();
+                this.unsetTagDetect();
+            }
+            this.app.ui.moveToContactPage(obj);
+        },
 
-               sucDetach: function nfc_sucDetach() {
-               }
-       };
+        sucDetach: function nfc_sucDetach() {}
+    };
 
-}());
+}());
\ No newline at end of file
index f1999b7..df46512 100644 (file)
@@ -1,72 +1,88 @@
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
+
 /*jslint devel: true*/
-/*global $, tizen, App, app */
+/*global $, tizen, App, app, localStorage*/
 
 (function () { // strict mode wrapper
-       'use strict';
-
-       /**
-        * Constructs NFCPeer
-        * @constructor
-        * @param {NFCControl} nfc
-        */
-       App.NFCPeer = function (nfc) {
-               this.nfc = nfc;
-       };
+    'use strict';
 
-       App.NFCPeer.prototype = {
+    /**
+     * Constructs NFCPeer
+     * @constructor
+     * @param {NFCControl} nfc
+     */
+    App.NFCPeer = function (nfc) {
+        this.nfc = nfc;
+    };
 
-               sucSendToTarget: function nfc_peer_sucSendToTarget() {
-               },
+    App.NFCPeer.prototype = {
 
-               errSendToTarget: function nfc_peer_errSendToTarget(e) {
-                       this.nfc.timeExpired('Send problem! ' + e.message);
-               },
+        sucSendToTarget: function nfc_peer_sucSendToTarget() {},
 
-               setReceiveFromTarget: function nfc_peer_setReceiveFromTarget() {
-                       try {
-                               if (!this.nfc.nfcTarget) {
-                                       console.warn("app.nfc.nfcTarget not set");
-                                       return;
-                               }
-                               this.nfc.nfcTarget.setReceiveNDEFListener(
-                                       this.nfc.readMessage.bind(this.nfc)
-                               );
-                       } catch (error) {
-                               console.error('setReceiveFromTarget error: ' + error.message);
-                       }
-               },
+        errSendToTarget: function nfc_peer_errSendToTarget(e) {
+            this.nfc.timeExpired('Send problem! ' + e.message);
+        },
 
-               sucTargetAttach: function nfc_peer_sucTargetAttach(target) {
-                       var newMessage = null,
-                               fullContact = this.nfc.prepareForNFC(localStorage);
+        setReceiveFromTarget: function nfc_peer_setReceiveFromTarget() {
+            try {
+                if (!this.nfc.nfcTarget) {
+                    console.warn("app.nfc.nfcTarget not set");
+                    return;
+                }
+                this.nfc.nfcTarget.unsetReceiveNDEFListener();
+                this.nfc.nfcTarget.setReceiveNDEFListener(
+                    this.nfc.readMessage.bind(this.nfc)
+                );
+            } catch (error) {
+                console.error('setReceiveFromTarget error: ' + error.message);
+            }
+        },
 
-                       this.nfc.nfcTarget = target;
-                       this.setReceiveFromTarget();
+        sucTargetAttach: function nfc_peer_sucTargetAttach(target) {
+            var newMessage = null,
+                fullContact = this.nfc.prepareForNFC(localStorage);
+            this.nfc.nfcTarget = target;
+            this.setReceiveFromTarget();
 
-                       try {
-                               newMessage = this.nfc.phoneNumber2NDEF(fullContact);
-                               target.sendNDEF(
-                                       newMessage,
-                                       this.sucSendToTarget.bind(this),
-                                       this.errSendToTarget.bind(this)
-                               );
-                       } catch (e) {
-                               console.error("NDEFMessage problem: " + e.message);
-                       }
-               },
+            try {
+                newMessage = this.nfc.phoneNumber2NDEF(fullContact);
+                target.sendNDEF(
+                    newMessage,
+                    this.sucSendToTarget.bind(this),
+                    this.errSendToTarget.bind(this)
+                );
+            } catch (e) {
+                console.error("NDEFMessage problem: " + e.message);
+            }
+        },
 
-               setTargetDetect: function nfc_peer_setTargetDetect() {
-                       var successCallbacks = {
-                                       onattach: this.sucTargetAttach.bind(this),
-                                       ondetach: this.nfc.sucDetach.bind(this.nfc)
-                               };
+        setTargetDetect: function nfc_peer_setTargetDetect() {
+            var successCallbacks = {
+                onattach: this.sucTargetAttach.bind(this),
+                ondetach: this.nfc.sucDetach.bind(this.nfc)
+            };
 
-                       try {
-                               this.nfc.nfcAdapter.setPeerListener(successCallbacks);
-                       } catch (error) {
-                               console.error(error.message);
-                       }
-               }
-       };
+            try {
+                this.nfc.nfcAdapter.unsetPeerListener();
+                this.nfc.nfcAdapter.setPeerListener(successCallbacks);
+            } catch (error) {
+                console.error(error.message);
+            }
+        }
+    };
 
-}());
+}());
\ No newline at end of file
index 3f7b1b7..ae3dab7 100644 (file)
+/*
+ *      Copyright 2013  Samsung Electronics Co., Ltd
+ *
+ *      Licensed under the Flora License, Version 1.1 (the "License");
+ *      you may not use this file except in compliance with the License.
+ *      You may obtain a copy of the License at
+ *
+ *              http://floralicense.org/license/
+ *
+ *      Unless required by applicable law or agreed to in writing, software
+ *      distributed under the License is distributed on an "AS IS" BASIS,
+ *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *      See the License for the specific language governing permissions and
+ *      limitations under the License.
+ */
+
 /*jslint devel: true*/
-/*global $, tizen, App, app, localStorage: true, TemplateManager, document, window, setTimeout */
+/*global $, tizen, App, app, localStorage: true, TemplateManager,document */
+/*global window, setTimeout */
 
 App.Ui = null;
 
 (function () { // strict mode wrapper
-       'use strict';
-
-       App.Ui = function App_Ui(app) {
-               this.app = app;
-               this.templateManager = new TemplateManager(app.config);
-       };
-
-       App.Ui.prototype = {
-               prepareCallerName: function ui_prepareCallerName(contact) {
-                       var callerName, firstName, lastName;
-
-                       callerName = '';
-                       firstName = contact.name.firstName;
-                       lastName = contact.name.lastName;
-
-                       if (firstName !== '' && firstName !== null) {
-                               callerName = firstName;
-                       }
-                       if (lastName !== '' && lastName !== null) {
-                               if (callerName !== '') {
-                                       callerName += ' ';
-                               }
-                               callerName += lastName;
-                       }
-                       if (callerName === '') {
-                               callerName = 'No Name';
-                       }
-                       return callerName;
-               },
-
-               /**
-                * Show a popup
-                * @param {string} message
-                */
-               showPopup: function ui_showPopup(message) {
-                       var popup = $("#alertPopup");
-                       if(!popup.hasClass('ui-popup')) {
-                               popup.popup().trigger('create');
-                       }
-                       $(".ui-popup-text", popup).text(message);
-                       popup.popup("open", {positionTo: 'window'});
-               },
-
-               closePopup: function ui_closePopup() {
-                       var activePopup = $.mobile.popup.active;
-                       if (activePopup) {
-                               if (activePopup.attr('id') === 'alertPopup') {
-                                       activePopup.close();
-                               }
-                       }
-               },
-
-               createListRecord: function ui_createListRecord() {
-                       $.mobile.changePage('#choose');
-               },
-
-               /**
-                *
-                * @param {string} text
-                * @returns {string}
-                */
-               getWaitingContentHtml: function getWaitingContentHtml(text) {
-                       var html;
-                       html += '<p class="defaultVeryBigText">';
-                       html += text;
-                       html += '</p>';
-
-                       html += '<p class="defaultCounterText" id="counter"></p>';
-
-                       return html;
-               },
-
-               /**
-                *
-                * @param {string} title
-                * @param {string} text
-                */
-               prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
-                       var waitingBox, waitingContent,
-                               contentTransfer = $('#content-transfer');
-                       waitingBox = $('<div class="box" id="waitingBox"></div>');
-                       waitingContent = $(this.getWaitingContentHtml(text));
-                       $('#header-transfer H1').text(title);
-                       contentTransfer.empty();
-                       waitingBox.append(waitingContent);
-                       contentTransfer.append(waitingBox);
-                       $('#content-start').trigger('create');
-                       this.app.countDown(10, $('#counter'));
-               },
-
-               /**
-                * @returns {string}
-                */
-               getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
-                       return '<div class="box" id="temporaryBox"></div>';
-               },
-
-               /**
-                * @returns {string}
-                */
-               showPopupWarning: function ui_showPopupWarning(){
-                       setTimeout(function(){
-                               $("#contact-nfc-error").popup("open",{"positionTo":"window"});
-                       },500);
-               },
-
-               getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
-                       return '<p class="defaultText">'
-                               + 'Default card is not defined yet!<br/>'
-                               + 'Do you want to define it now?'
-                               + '</p>';
-               },
-
-               loadTemporaryContent: function ui_loadTemporaryContent() {
-                       var temporaryBox, temporaryContent, temporaryButton;
-
-                       temporaryBox = $(this.getTemporaryBoxHtml());
-                       temporaryContent = $(this.getTemporaryBoxContentHtml());
-                       temporaryButton = $(this.getButtonHtml('Create default card'));
-
-                       temporaryButton.on('click', function (event) {
-                               event.preventDefault();
-                               $.mobile.changePage('#choose');
-                       });
-
-                       temporaryBox.append(temporaryContent).append(temporaryButton);
-
-                       $('#content-start').empty().append(temporaryBox).trigger('create');
-               },
-               /**
-                *
-                * @param {string} text button text
-                * @returns {string}
-                */
-               getButtonHtml: function ui_getButtonHtml(text) {
-                       return '<div data-role="button" class="ui-btn-create">'
-                               + text
-                               + '</div>';
-               },
-
-               /**
-                *
-                * @param {string} firstName
-                * @param {string} lastName
-                * @returns {string}
-                */
-               getCommentHtml: function ui_getCommentHtml(data) {
-                       var def_name = $('<p class="comment" id="comment-name">')
-                               .text(data.caller).html();
-                       return '<div id="comment">'
-                               + '<p class="comment">Your default contact</p>'
-                               + '<p class="comment" id="comment-userName">'
-                               + def_name + '</p>'
-                               + '<p class="comment" id="comment-phone">'
-                               + data.phoneNumber + '</p>'
-                               + '</div>';
-               },
-
-               
-               /**
-                * Change Contact button action
-                * @event
-                * @param {Event} event
-                */
-               changeContact: function ui_changeContact(event) {
-                       event.preventDefault();
-                       $.mobile.changePage('#choose');
-               },
-
-               /**
-                * @returns {jQuery}
-                */
-               getChangeContactButton: function ui_getChangeContactButton() {
-                       var changeContactButton;
-                       changeContactButton = $(this.getButtonHtml('Change your default contact'));
-                       changeContactButton.on('click', this.changeContact);
-                       return changeContactButton;
-               },
-
-               /**
-                * Read From Card button action
-                * @event
-                * @param {Event} event
-                */
-               readFromCard: function ui_readFromCard(event) {
-                       event.preventDefault();
-                       if (tizen.nfc.getDefaultAdapter().powered) {
-                               try {
-                                       $('#transfer').data('option', 'read');
-                                       $.mobile.changePage('#transfer');
-                               } catch (e) {
-                                       console.error(e.message);
-                               }
-                       } else {
-                               $.mobile.changePage('#start');
-                               alert('Please turn on NFC adapter');
-                       }
-               },
-
-               /**
-                * @returns {jQuery}
-                */
-               getReadFromCardButton: function ui_getReadFromCardButton() {
-                       var readFromCardButton;
-                       readFromCardButton = $(this.getButtonHtml('Read from card'));
-                       readFromCardButton.on('click', this.readFromCard);
-                       return readFromCardButton;
-               },
-
-               /**
-                * Write To Card button action
-                * @param {Event} event
-                */
-               writeToCard: function ui_writeToCard(event) {
-                       event.preventDefault();
-                       if (tizen.nfc.getDefaultAdapter().powered) {
-                               try {
-                                       $('#transfer').data('option', 'write');
-                                       $.mobile.changePage('#transfer');
-                               } catch (e) {
-                                       console.error(e.message);
-                               }
-                       } else {
-                               alert('Please turn on NFC adapter');
-                       }
-               },
-
-               /**
-                * @returns {jQuery}
-                */
-               getWriteToCardButton: function ui_getWriteToCardButton() {
-                       var writeToCardButton;
-                       writeToCardButton = $(this.getButtonHtml('Write to card'));
-                       writeToCardButton.on('click', this.writeToCard);
-                       return writeToCardButton;
-               },
-
-               /**
-                * Communicate With Other Device button action
-                * @param {type} event
-                */
-               communicateWithOtherDevice: function ui_communicateWithOtherDevice(event) {
-                       event.preventDefault();
-                       if (tizen.nfc.getDefaultAdapter().powered) {
-                               try {
-                                       $('#transfer').data('option', 'communicate');
-                                       $.mobile.changePage('#transfer');
-                               } catch (e) {
-                                       console.error(e.message);
-                               }
-                       } else {
-                               alert('Please turn on NFC adapter');
-                       }
-               },
-
-               /**
-                * @returns {jQuery}
-                */
-               getCommunicateWithOtherDeviceButton: function ui_getCommunicateWithOtherDeviceButton() {
-                       var communicateWithOtherDeviceButton;
-                       communicateWithOtherDeviceButton = $(this.getButtonHtml('Communicate with another device'));
-                       communicateWithOtherDeviceButton.on('click', this.communicateWithOtherDevice);
-                       return communicateWithOtherDeviceButton;
-               },
-
-               loadStartContent: function ui_loadStartContent() {
-                       var startBox, contentStart, gap, comment;
-                       contentStart = $('#content-start');
-                       startBox = $('<div class="box" id="startBox"></div>');
-                       gap = $('<div class="gap"></div>');
-                       comment = $(this.getCommentHtml(localStorage));
-
-                       contentStart.empty();
-                       startBox
-                               .append(this.getChangeContactButton())
-                               .append(gap.clone())
-                               .append(this.getReadFromCardButton())
-                               .append(gap.clone())
-                               .append(this.getWriteToCardButton())
-                               .append(gap.clone())
-                               .append(this.getCommunicateWithOtherDeviceButton())
-                               .prepend(comment);
-
-                       contentStart.append(startBox);
-                       contentStart.trigger('create');
-               },
-
-               loadStartPage: function ui_loadStartPage() {
-                       if (localStorage.started === undefined) {
-                               this.loadTemporaryContent();
-                       } else {
-                               this.loadStartContent();
-                       }
-               },
-
-               /**
-                *
-                * @param {string} value
-                * @param {string} label
-                * @returns {string}
-                */
-               getLiHtml: function ui_getLiHtml(value, label) {
-                       var html;
-                       html = '<li class="ui-li-multiline">'
-                               + '<a href="#">'
-                               + '<span>' + label + '</span>'
-                               + '<span class="ui-li-text-sub">'
-                               + ((!value) ? '...' : value)
-                               + '</span>'
-                               + '</a>'
-                               + '</li>';
-                       return html;
-               },
-
-               /**
-                *
-                * @param {string} phone
-                * @param {string} first
-                * @param {string} last
-                * @returns {string}
-                */
-               getContactsUlHtml: function ui_getContactsUlHtml(phone, first, last) {
-                       var html;
-                       html = '<ul data-role="listview" id="contacts-data">';
-                       html += this.getLiHtml(first, 'First Name');
-                       html += this.getLiHtml(last, 'Last Name');
-                       html += this.getLiHtml(phone, 'Phone');
-                       html += '</ul>';
-                       return html;
-               },
-
-               /**
-                * @param {object} contact
-                * @returns {string}
-                */
-               getContactsListElement: function ui_getContactsListElement(contact) {
-                       var caller = $('<span class="ui-li-text-sub">')
-                                       .text(contact.caller).html(),
-                               phoneNumber = contact.phoneNumber,
-                               html =
-                                       '<li class="ui-li-multiline">'
-                                       + '<a href="#">' + caller
-                                       + '<span class="ui-li-text-sub">' + phoneNumber + '</span>'
-                                       + '</a>'
-                                       + '</li>';
-                       return html;
-               },
-
-               prepareContactsTemplate: function ui_prepareContactsTemplate(phone, first, last) {
-                       $('#content-contact > .ui-scrollview-view')
-                               .empty()
-                               .append(this.getContactsUlHtml(phone, first, last));
-                       $('#content-contact').trigger('create');
-               },
-
-               contactsCompare: function ui_contactsCompare(a, b) {
-                       if (a.caller < b.caller) {
-                               return -1;
-                       }
-                       if (a.caller > b.caller) {
-                               return 1;
-                       }
-                       return 0;
-               },
-
-               createSortedContactArray: function ui_createSortedContactArray(contacts) {
-                       var i, len, sortedContactList = [], contact, phoneNumber;
-
-                       for (i = 0, len = contacts.length; i < len; i += 1) {
-                               contact = contacts[i];
-                               if (contact.phoneNumbers.length === 0) {
-                                       phoneNumber = '';
-                               } else {
-                                       phoneNumber = contact.phoneNumbers[0].number;
-                               }
-                               sortedContactList.push({
-                                       caller: this.prepareCallerName(contact),
-                                       firstName: contact.name.firstName || '',
-                                       lastName: contact.name.lastName || '',
-                                       phoneNumber: phoneNumber,
-                                       id: contact.id,
-                                       vCard: contact.convertToString('VCARD_30'),
-                                       contact: contact
-                               });
-                       }
-                       sortedContactList.sort(this.contactsSort);
-
-                       return sortedContactList;
-               },
-
-               createSortedContactList: function ui_createSortedContactList(contacts) {
-                       var sortedContactList = this.createSortedContactArray(contacts),
-                               ul = $('<ul data-role="listview" id="list-choose"></ul>'),
-                               i,
-                               len,
-                               listElement,
-                               listElementTap,
-                               self = this,
-                               contact;
-
-                       listElementTap = function (event) {
-                               event.preventDefault();
-                               $(this).addClass('selected').siblings().removeClass('selected');
-                               self.app.saveDefaultCard();
-                       };
-
-                       for (i = 0, len = sortedContactList.length; i < len; i += 1) {
-                               contact = sortedContactList[i];
-                               if (contact.phoneNumber !== '') {
-                                       listElement = $(this.getContactsListElement(contact));
-                                       listElement
-                                               .data('caller', contact.caller)
-                                               .data('firstName', contact.firstName)
-                                               .data('lastName', contact.lastName)
-                                               .data('phoneNumber', contact.phoneNumber)
-                                               .data('id', contact.id)
-                                               .data('vCard', contact.vCard);
-                                       if (localStorage.id === listElement.data('id')) {
-                                               listElement.addClass('selected');
-                                       }
-                                       ul.append(listElement);
-                               }
-                       }
-                       ul.on('tap taphold click', 'li', listElementTap);
-                       return ul;
-               },
-
-               showContactsList: function ui_showContactsList(contacts) {
-                       var ul = this.createSortedContactList(contacts);
-                       $('#content-choose > .ui-scrollview-view').empty().append(ul);
-                       $('#content-choose').trigger('create');
-               },
-
-               moveToStartPage: function ui_moveToStartPage(monit) {
-                       $('#start').data('monit', monit || '');
-                       $.mobile.changePage('#start');
-               },
-
-               isActivePage: function ui_isActivePage(id) {
-                        return (id === $.mobile.activePage.attr("id"));
-               },
-
-               refreshIfActivePage: function ui_refreshIfActivePage(id) {
-                       if (this.isActivePage(id)) {
-                               $.mobile.activePage
-                                       .trigger("pagebeforeshow")
-                                       .trigger("pageshow");
-                       }
-               },
-
-               moveToContactPage: function ui_moveToContactPage(obj) {
-                       $('#start').data('monit', '');
-                       $('#contact').data('contactsData', obj);
-                       $.mobile.changePage('#contact');
-               },
-
-               defineEvents: function ui_defineEvents() {
-                       var self = this;
-
-                       $('#header-start .ui-btn-back').on('click', function (event) {
-                               event.preventDefault();
-                               self.app.nfc.stopNFC();
-                       });
-
-                       $('#header-start').on('click', '.ui-btn-back.ui-focus', function () {
-                               return false;
-                       });
-
-                       $('#footer-contact').on('click', '.ui-btn-back', function (event) {
-                               event.preventDefault();
-                               $.mobile.changePage('#start');
-                       });
-
-                       $('#footer-choose').on('click', '.ui-btn-back', function (event) {
-                               event.preventDefault();
-                               $.mobile.changePage('#start');
-                       });
-
-                       $('#choose').on('pageshow', function (event) {
-                               self.app.loadContacts(self.showContactsList.bind(self), function (e) {
-                                       alert('Cannot load the contacts list: ' + e.message);
-                                       console.error(e.message, e);
-                               });
-                       });
-
-                       $('#contact').on('pageshow', function (event) {
-                               var data = $(this).data('contactsData');
-                               self.prepareContactsTemplate(data.phone, data.first, data.last);
-                       });
-
-                       $('#save-contact').on('click', function (event) {
-                               event.preventDefault();
-                               self.app.saveContact();
-                       });
-
-                       $('#start').on('pagebeforeshow', function () {
-                               if (self.app.started) {
-                                       self.loadStartPage();
-                               }
-                       });
-
-                       $('#start').on('pageshow', function () {
-                               var obj = $(this), monit = obj.data('monit');
-                               if (monit !== '' && monit !== undefined) {
-                                       self.showPopup(obj.data('monit'));
-                                       obj.data('monit', '');
-                               }
-                       });
-
-                       $( "#contact-nfc-error" ).bind({
-                                       popupafterclose: function(){
-                                       tizen.application.getCurrentApplication().exit();
-                               }
-                       });
-
-                       window.addEventListener('tizenhwkey', function(e) {
-                               if (e.keyName == "back") {
-                                       if ($.mobile.popup.active) {
-                                               $.mobile.popup.active.close();
-                                       } else if ($.mobile.activePage.attr('id') === 'start') {
-                                               tizen.application.getCurrentApplication().exit();
-                                       } else {
-                                               self.app.nfc.timeExpired();
-                                       }
-                               }
-                       });
-
-                       document.addEventListener('webkitvisibilitychange', function () {
-                               if(document.webkitVisibilityState === "visible") {
-                                       if ($.mobile.activePage.attr('id') === "choose") {
-                                               $.mobile.activePage.trigger('pageshow');
-                                       }
-                                       app.counterState = true;
-                               } else {
-                                       app.counterState = false;
-                               }
-                       });
-
-                       $('#transfer').on('pageshow', function () {
-                               if (tizen.nfc.getDefaultAdapter().powered) {
-                                       try {
-                                               var option = $(this).data('option');
-                                               if (option === 'read') {
-                                                       self.prepareWaitingPage('Card to device', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
-                                                       self.app.nfc.card.setTagDetectRead();
-                                               } else if (option === 'write') {
-                                                       self.prepareWaitingPage('Device to card', 'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE');
-                                                       self.app.nfc.card.setTagDetectWrite();
-                                               } else {
-                                                       self.prepareWaitingPage('Device to device', 'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
-                                                       self.app.nfc.peer.setTargetDetect();
-                                               }
-                                       } catch (e) {
-                                               console.error(e.message);
-                                       }
-                               } else {
-                                       $.mobile.changePage('#start');
-                                       alert('Please turn on NFC adapter');
-                               }
-                       });
-               }
-
-       };
-
-}());
+    'use strict';
+
+    App.Ui = function App_Ui(app) {
+        this.app = app;
+        this.templateManager = new TemplateManager(app.config);
+    };
+
+    App.Ui.prototype = {
+        prepareCallerName: function ui_prepareCallerName(contact) {
+            var name = contact.name.displayName;
+            return (name && name.length > 0) ? name : 'No Name';
+        },
+
+        /**
+         * Show a popup
+         * @param {string} message
+         */
+        showPopup: function ui_showPopup(message) {
+            var popup = $('#alertPopup');
+            if (!popup.hasClass('ui-popup')) {
+                popup.popup().trigger('create');
+            }
+            $('.ui-popup-text', popup).text(message);
+            popup.popup('open', {
+                positionTo: 'window'
+            });
+        },
+
+        closePopup: function ui_closePopup() {
+            var activePopup = $.mobile.popup.active;
+            if (activePopup) {
+                if (activePopup.attr('id') === 'alertPopup') {
+                    activePopup.close();
+                }
+            }
+        },
+
+        createListRecord: function ui_createListRecord() {
+            $.mobile.changePage('#choose');
+        },
+
+        /**
+         *
+         * @param {string} text
+         * @returns {string}
+         */
+        getWaitingContentHtml: function getWaitingContentHtml(text) {
+            var html;
+            html += '<p class="defaultVeryBigText">';
+            html += text;
+            html += '</p>';
+
+            html += '<p class="defaultCounterText" id="counter"></p>';
+
+            return html;
+        },
+
+        /**
+         *
+         * @param {string} title
+         * @param {string} text
+         */
+        prepareWaitingPage: function ui_prepareWaitingPage(title, text) {
+            var waitingBox, waitingContent,
+                contentTransfer = $('#content-transfer');
+            waitingBox = $('<div class="box" id="waitingBox"></div>');
+            waitingContent = $(this.getWaitingContentHtml(text));
+            $('#header-transfer H1').text(title);
+            contentTransfer.empty();
+            waitingBox.append(waitingContent);
+            contentTransfer.append(waitingBox);
+            $('#content-start').trigger('create');
+            this.app.countDown(10, $('#counter'));
+        },
+
+        /**
+         * @returns {string}
+         */
+        getTemporaryBoxHtml: function ui_getTemporaryBoxHtml() {
+            return '<div class="box" id="temporaryBox"></div>';
+        },
+
+        /**
+         * @returns {string}
+         */
+        showPopupWarning: function ui_showPopupWarning() {
+            setTimeout(function () {
+                $('#contact-nfc-error').popup('open', {
+                    'positionTo': 'window'
+                });
+            }, 500);
+        },
+
+        getTemporaryBoxContentHtml: function ui_getTemporaryBoxContentHtml() {
+            return '<p class="defaultText">'
+                + 'Default card is not defined yet!<br/>'
+                + 'Do you want to define it now?' + '</p>';
+        },
+
+        loadTemporaryContent: function ui_loadTemporaryContent() {
+            var temporaryBox, temporaryContent, temporaryButton;
+
+            temporaryBox = $(this.getTemporaryBoxHtml());
+            temporaryContent = $(this.getTemporaryBoxContentHtml());
+            temporaryButton = $(this.getButtonHtml('Create default card'));
+
+            temporaryButton.on('click', function (event) {
+                event.preventDefault();
+                $.mobile.changePage('#choose');
+            });
+
+            temporaryBox.append(temporaryContent).append(temporaryButton);
+
+            $('#content-start').empty().append(temporaryBox).trigger('create');
+        },
+        /**
+         *
+         * @param {string} text button text
+         * @returns {string}
+         */
+        getButtonHtml: function ui_getButtonHtml(text) {
+            return '<div data-role="button" class="ui-btn-create">'
+                + text + '</div>';
+        },
+
+        /**
+         *
+         * @param {string} firstName
+         * @param {string} lastName
+         * @returns {string}
+         */
+        getCommentHtml: function ui_getCommentHtml(data) {
+            var def_name = $('<p class="comment" id="comment-name">')
+                .text(data.caller).html();
+            return '<div id="comment">'
+                + '<p class="comment">Your default contact</p>'
+                + '<p class="comment" id="comment-userName">'
+                + def_name + '</p>' + '<p class="comment" id="comment-phone">'
+                + data.phoneNumber + '</p>' + '</div>';
+        },
+
+
+        /**
+         * Change Contact button action
+         * @event
+         * @param {Event} event
+         */
+        changeContact: function ui_changeContact(event) {
+            event.preventDefault();
+            $.mobile.changePage('#choose');
+        },
+
+        /**
+         * @returns {jQuery}
+         */
+        getChangeContactButton: function ui_getChangeContactButton() {
+            var changeContactButton;
+            changeContactButton =
+                $(this.getButtonHtml('Change your default contact'));
+            changeContactButton.on('click', this.changeContact);
+            return changeContactButton;
+        },
+
+        /**
+         * Read From Card button action
+         * @event
+         * @param {Event} event
+         */
+        readFromCard: function ui_readFromCard(event) {
+            event.preventDefault();
+            if (tizen.nfc.getDefaultAdapter().powered) {
+                try {
+                    $('#transfer').data('option', 'read');
+                    $.mobile.changePage('#transfer');
+                } catch (e) {
+                    console.error(e.message);
+                }
+            } else {
+                $.mobile.changePage('#start');
+                alert('Please turn on NFC adapter');
+            }
+        },
+
+        /**
+         * @returns {jQuery}
+         */
+        getReadFromCardButton: function ui_getReadFromCardButton() {
+            var readFromCardButton;
+            readFromCardButton = $(this.getButtonHtml('Read from card'));
+            readFromCardButton.on('click', this.readFromCard);
+            return readFromCardButton;
+        },
+
+        /**
+         * Write To Card button action
+         * @param {Event} event
+         */
+        writeToCard: function ui_writeToCard(event) {
+            event.preventDefault();
+            if (tizen.nfc.getDefaultAdapter().powered) {
+                try {
+                    $('#transfer').data('option', 'write');
+                    $.mobile.changePage('#transfer');
+                } catch (e) {
+                    console.error(e.message);
+                }
+            } else {
+                alert('Please turn on NFC adapter');
+            }
+        },
+
+        /**
+         * @returns {jQuery}
+         */
+        getWriteToCardButton: function ui_getWriteToCardButton() {
+            var writeToCardButton;
+            writeToCardButton = $(this.getButtonHtml('Write to card'));
+            writeToCardButton.on('click', this.writeToCard);
+            return writeToCardButton;
+        },
+
+        /**
+         * Communicate With Other Device button action
+         * @param {type} event
+         */
+        commWithOtherDevice: function ui_commWithOtherDevice(event) {
+            event.preventDefault();
+            if (tizen.nfc.getDefaultAdapter().powered) {
+                try {
+                    $('#transfer').data('option', 'communicate');
+                    $.mobile.changePage('#transfer');
+                } catch (e) {
+                    console.error(e.message);
+                }
+            } else {
+                alert('Please turn on NFC adapter');
+            }
+        },
+
+        /**
+         * @returns {jQuery}
+         */
+        getCommWithOtherDeviceBtnn: function ui_getCommWithOtherDeviceBtnn() {
+            var communicateWithOtherDeviceButton;
+            communicateWithOtherDeviceButton = 
+                $(this.getButtonHtml('Communicate with another device'));
+            communicateWithOtherDeviceButton
+                .on('click', this.commWithOtherDevice);
+            return communicateWithOtherDeviceButton;
+        },
+
+        loadStartContent: function ui_loadStartContent() {
+            var startBox, contentStart, gap, comment;
+            contentStart = $('#content-start');
+            startBox = $('<div class="box" id="startBox"></div>');
+            gap = $('<div class="gap"></div>');
+            comment = $(this.getCommentHtml(localStorage));
+
+            contentStart.empty();
+            startBox
+                .append(this.getChangeContactButton())
+                .append(gap.clone())
+                .append(this.getReadFromCardButton())
+                .append(gap.clone())
+                .append(this.getWriteToCardButton())
+                .append(gap.clone())
+                .append(this.getCommWithOtherDeviceBtnn())
+                .prepend(comment);
+
+            contentStart.append(startBox);
+            contentStart.trigger('create');
+        },
+
+        loadStartPage: function ui_loadStartPage() {
+            if (localStorage.started === undefined) {
+                this.loadTemporaryContent();
+            } else {
+                this.loadStartContent();
+            }
+        },
+
+        /**
+         *
+         * @param {string} value
+         * @param {string} label
+         * @returns {string}
+         */
+        getLiHtml: function ui_getLiHtml(value, label) {
+            var html;
+            html = '<li class="ui-li-multiline" id="' + label + '">'
+                + '<a href="#">' + '<span>' + label + '</span>'
+                + '<span class="ui-li-text-sub">' + ((!value) ? '...' : value)
+                + '</span>' + '</a>' + '</li>';
+            return html;
+        },
+
+        /**
+         *
+         * @param {string} phone
+         * @param {string} first
+         * @param {string} last
+         * @returns {string}
+         */
+        getContactsUlHtml: function ui_getContactsUlHtml(
+            phone,
+            phone_type,
+            first,
+            middle,
+            last,
+            vCard
+        ) {
+            var html;
+            html = '<ul data-role="listview" id="contacts-data">';
+            html += this.getLiHtml(first, 'First Name');
+            html += this.getLiHtml(middle, 'Middle Name');
+            html += this.getLiHtml(last, 'Last Name');
+            html += this.getLiHtml(phone, 'Phone');
+            html += this.getLiHtml(phone_type, 'Type');
+            html += this.getLiHtml(vCard, 'vCard');
+            html += '</ul>';
+            return html;
+        },
+
+        /**
+         * @param {object} contact
+         * @returns {string}
+         */
+        getContactsListElement: function ui_getContactsListElement(contact) {
+            var caller = $('<span class="ui-li-text-sub">')
+                    .text(contact.caller).html(),
+                phoneNumber = contact.phoneNumber,
+                html = '<li class="ui-li-multiline">'
+                    + '<a href="#">' + caller + '<span class="ui-li-text-sub">'
+                    + phoneNumber + '</span>' + '</a>' + '</li>';
+            return html;
+        },
+
+        prepareContactsTemplate: function ui_prepareContactsTemplate(
+            phone,
+            phone_type,
+            first,
+            middle,
+            last,
+            vCard
+        ) {
+            $('#content-contact > .ui-scrollview-view')
+                .empty()
+                .append(
+                    this.getContactsUlHtml(
+                        phone,
+                        phone_type,
+                        first,
+                        middle,
+                        last,
+                        vCard
+                    )
+                );
+            $('#content-contact').trigger('create');
+        },
+
+        contactsCompare: function ui_contactsCompare(a, b) {
+            if (a.caller < b.caller) {
+                return -1;
+            }
+            if (a.caller > b.caller) {
+                return 1;
+            }
+            return 0;
+        },
+
+        createSortedContactArray: function ui_createSortedContactArray(
+            contacts
+        ) {
+            var i, len, sortedContactList = [],
+                contact, phoneNumber;
+
+            for (i = 0, len = contacts.length; i < len; i += 1) {
+                contact = contacts[i];
+                if (contact.phoneNumbers.length === 0) {
+                    phoneNumber = '';
+                } else {
+                    phoneNumber = contact.phoneNumbers[0].number;
+                }
+                sortedContactList.push({
+                    caller: this.prepareCallerName(contact),
+                    firstName: contact.name.firstName || '',
+                    middleName: contact.name.middleName || '',
+                    lastName: contact.name.lastName || '',
+                    phoneNumber: phoneNumber,
+                    id: contact.id,
+                    vCard: contact.convertToString('VCARD_30'),
+                    contact: contact
+                });
+            }
+            sortedContactList.sort(this.contactsSort);
+
+            return sortedContactList;
+        },
+
+        createSortedContactList: function ui_createSortedContactList(
+            contacts
+        ) {
+            var sortedContactList = this.createSortedContactArray(contacts),
+                ul = $('<ul data-role="listview" id="list-choose"></ul>'),
+                i,
+                len,
+                listElement,
+                listElementTap,
+                self = this,
+                contact;
+
+            listElementTap = function (event) {
+                event.preventDefault();
+                $(this).addClass('selected').siblings().removeClass('selected');
+                self.app.saveDefaultCard();
+            };
+
+            for (i = 0, len = sortedContactList.length; i < len; i += 1) {
+                contact = sortedContactList[i];
+                if (contact.phoneNumber !== '') {
+                    listElement = $(this.getContactsListElement(contact));
+                    listElement
+                        .data('caller', contact.caller)
+                        .data('firstName', contact.firstName)
+                        .data('middleName', contact.middleName)
+                        .data('lastName', contact.lastName)
+                        .data('phoneNumber', contact.phoneNumber)
+                        .data('id', contact.id)
+                        .data('vCard', contact.vCard);
+                    if (localStorage.id === listElement.data('id')) {
+                        listElement.addClass('selected');
+                    }
+                    ul.append(listElement);
+                }
+            }
+            ul.on('tap taphold click', 'li', listElementTap);
+            return ul;
+        },
+
+        showContactsList: function ui_showContactsList(contacts) {
+            var ul = this.createSortedContactList(contacts);
+            $('#content-choose > .ui-scrollview-view').empty().append(ul);
+            $('#content-choose').trigger('create');
+        },
+
+        moveToStartPage: function ui_moveToStartPage(monit) {
+            $('#start').data('monit', monit || '');
+            $.mobile.changePage('#start');
+        },
+
+        isActivePage: function ui_isActivePage(id) {
+            return (id === $.mobile.activePage.attr('id'));
+        },
+
+        refreshIfActivePage: function ui_refreshIfActivePage(id) {
+            if (this.isActivePage(id)) {
+                $.mobile.activePage
+                    .trigger('pagebeforeshow')
+                    .trigger('pageshow');
+            }
+        },
+
+        moveToContactPage: function ui_moveToContactPage(obj) {
+            $('#start').data('monit', '');
+            $('#contact').data('contactsData', obj);
+            $.mobile.changePage('#contact');
+        },
+
+        defineEvents: function ui_defineEvents() {
+            var self = this;
+
+            $('#header-start .ui-btn-back').on('click', function (event) {
+                event.preventDefault();
+                self.app.nfc.stopNFC();
+            });
+
+            $('#header-start')
+                .on('click', '.ui-btn-back.ui-focus', function () {
+                    return false;
+                });
+
+            $('#footer-contact').on('click', '.ui-btn-back', function (event) {
+                event.preventDefault();
+                $.mobile.changePage('#start');
+            });
+
+            $('#footer-choose').on('click', '.ui-btn-back', function (event) {
+                event.preventDefault();
+                $.mobile.changePage('#start');
+            });
+
+            $('#choose').on('pageshow', function (event) {
+                self.app.loadContacts(
+                    self.showContactsList.bind(self),
+                    function (e) {
+                        alert('Cannot load the contacts list: ' + e.message);
+                        console.error(e.message, e);
+                    }
+                );
+            });
+
+            $('#contact').on('pageshow', function (event) {
+                var data = $(this).data('contactsData');
+                self.prepareContactsTemplate(
+                    data.phone,
+                    data.phone_type,
+                    data.first,
+                    data.middle,
+                    data.last,
+                    data.vCard
+                );
+            });
+
+            $('#save-contact').on('click', function (event) {
+                event.preventDefault();
+                self.app.saveContact();
+            });
+
+            $('#start').on('pagebeforeshow', function () {
+                if (self.app.started) {
+                    self.loadStartPage();
+                }
+            });
+
+            $('#start').on('pageshow', function () {
+                var obj = $(this),
+                    monit = obj.data('monit');
+                if (monit !== '' && monit !== undefined) {
+                    self.showPopup(obj.data('monit'));
+                    obj.data('monit', '');
+                }
+            });
+
+            $('#contact-nfc-error').bind({
+                popupafterclose: function () {
+                    tizen.application.getCurrentApplication().exit();
+                }
+            });
+
+            window.addEventListener('tizenhwkey', function (e) {
+                if (e.keyName === 'back') {
+                    if ($.mobile.popup.active) {
+                        $.mobile.popup.active.close();
+                    } else if ($.mobile.activePage.attr('id') === 'start') {
+                        tizen.application.getCurrentApplication().exit();
+                    } else {
+                        self.app.nfc.timeExpired();
+                    }
+                }
+            });
+
+            document.addEventListener('webkitvisibilitychange', function () {
+                if (document.webkitVisibilityState === 'visible') {
+                    if ($.mobile.activePage.attr('id') === 'choose') {
+                        $.mobile.activePage.trigger('pageshow');
+                    }
+                    app.counterState = true;
+                } else {
+                    app.counterState = false;
+                }
+            });
+
+            $('#transfer').on('pageshow', function () {
+                if (tizen.nfc.getDefaultAdapter().powered) {
+                    try {
+                        var option = $(this).data('option');
+                        if (option === 'read') {
+                            self.prepareWaitingPage(
+                                'Card to device',
+                                'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE'
+                            );
+                            self.app.nfc.card.setTagDetectRead();
+                        } else if (option === 'write') {
+                            self.prepareWaitingPage(
+                                'Device to card',
+                                'PUT WIRELESS TAG<br>CLOSE TO<br>YOUR DEVICE'
+                            );
+                            self.app.nfc.card.setTagDetectWrite();
+                        } else {
+                            self.prepareWaitingPage('Device to device',
+                                'PUT YOUR DEVICE<br>CLOSE TO<br>OTHER DEVICE');
+                            self.app.nfc.peer.setTargetDetect();
+                        }
+                    } catch (e) {
+                        console.error(e.message);
+                    }
+                } else {
+                    $.mobile.changePage('#start');
+                    alert('Please turn on NFC adapter');
+                }
+            });
+        }
+
+    };
+
+}());
\ No newline at end of file
index 68c6678..c880c5e 100644 (file)
-/*global tizen, $, app */
+/*
+*      Copyright 2013  Samsung Electronics Co., Ltd
+*
+*      Licensed under the Flora License, Version 1.1 (the "License");
+*      you may not use this file except in compliance with the License.
+*      You may obtain a copy of the License at
+*
+*              http://floralicense.org/license/
+*
+*      Unless required by applicable law or agreed to in writing, software
+*      distributed under the License is distributed on an "AS IS" BASIS,
+*      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+*      See the License for the specific language governing permissions and
+*      limitations under the License.
+*/
+
+/*global tizen, $, app, ModifierManager */
+
 /**
- * @class TemplateManager
- */
+* @class TemplateManager
+*/
 function TemplateManager() {
-       'use strict';
-       this.init();
+    'use strict';
+    this.init();
 }
 
 (function () { // strict mode wrapper
-       'use strict';
-       TemplateManager.prototype = {
-
-               /**
-                * Template cache
-                */
-               cache: {},
-
-               /**
-                * UI module initialisation
-                */
-               init: function init() {
-               },
-
-               /**
-                * Returns template html (from cache)
-                * @param {string} tplName
-                * @param {string} tplParams
-                */
-               get: function TemplateManager_get(tplName, tplParams) {
-                       if (this.cache[tplName] !== undefined) {
-                               return this.getCompleted(this.cache[tplName], tplParams);
-                       }
-                       return '';
-               },
-
-               /**
-                * Load templates to cache
-                * @param {string} tplNames
-                * @param {function} onSuccess
-                */
-               loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
-                       var self = this,
-                               cachedTemplates = 0,
-                               tplName,
-                               tplPath;
-
-                       if ($.isArray(tplNames)) {
-
-                               // for each template
-                               $.each(tplNames, function (index, fileName) {
-
-                                       // cache template html
-                                       if (self.cache[fileName] === undefined) {
-                                               tplName = [fileName, app.config.get('templateExtension')].join('');
-                                               tplPath = [app.config.get('templateDir'), tplName].join('/');
-
-                                               $.ajax({
-                                                       url: tplPath,
-                                                       cache: true,
-                                                       dataType: 'html',
-                                                       async: true,
-                                                       success: function (data) {
-                                                               // increase counter
-                                                               cachedTemplates += 1;
-
-                                                               // save to cache
-                                                               self.cache[fileName] = data;
-
-                                                               // if all templates are cached launch callback
-                                                               if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
-                                                                       onSuccess();
-                                                               }
-                                                       },
-                                                       error: function (jqXHR, textStatus, errorThrown) {
-                                                               console.error('templateManagerError: ' + errorThrown);
-                                                       }
-                                               });
-                                       } else {
-                                               // template is already cached
-                                               cachedTemplates += 1;
-                                               // if all templates are cached launch callback
-                                               if (cachedTemplates >= tplNames.length && typeof onSuccess === 'function') {
-                                                       onSuccess();
-                                               }
-                                       }
-                               });
-
-                       }
-               },
-
-               /**
-                * Returns template completed by specified params
-               * @param {string} tplHtml
-               * @param {string} tplParams
-                */
-               getCompleted: function TemplateManager_getCompleted(tplHtml, tplParams) {
-                       var tplParam, replaceRegExp;
-
-                       for (tplParam in tplParams) {
-                               if (tplParams.hasOwnProperty(tplParam)) {
-                                       replaceRegExp = new RegExp(['%', tplParam, '%'].join(''), 'g');
-                                       tplHtml = tplHtml.replace(replaceRegExp, tplParams[tplParam]);
-                               }
-                       }
-
-                       return tplHtml;
-               }
-       };
-
-}());
\ No newline at end of file
+    'use strict';
+
+    TemplateManager.prototype = {
+
+        /**
+        * Template cache
+        */
+        cache: {},
+
+        /**
+        * UI module initialisation
+        */
+        init: function init() {
+            this.modifiers = new ModifierManager().getAll();
+        },
+
+        /**
+        * Returns template html (from cache)
+        * @param {string} tplName
+        * @param {string} tplParams
+        */
+        get: function TemplateManager_get(tplName, tplParams) {
+            if (this.cache[tplName] !== undefined) {
+                return this.getCompleted(this.cache[tplName], tplParams);
+            }
+            return '';
+        },
+
+        /**
+        * Load templates to cache
+        * @param {string} tplNames
+        * @param {function} onSuccess
+        */
+        loadToCache: function TemplateManager_loadToCache(tplNames, onSuccess) {
+            var self = this,
+                cachedTemplates = 0,
+                tplName,
+                tplPath;
+
+            if ($.isArray(tplNames)) {
+
+                // for each template
+                $.each(tplNames, function (index, fileName) {
+
+                    // cache template html
+                    if (self.cache[fileName] === undefined) {
+                        tplName = [
+                            fileName,
+                            app.config.get('templateExtension')
+                        ].join('');
+                        tplPath = [
+                            app.config.get('templateDir'),
+                            tplName
+                        ].join('/');
+
+                        $.ajax({
+                            url: tplPath,
+                            cache: true,
+                            dataType: 'html',
+                            async: true,
+                            success: function (data) {
+                                // increase counter
+                                cachedTemplates += 1;
+
+                                // save to cache
+                                self.cache[fileName] = data;
+
+                                // if all templates are cached launch callback
+                                if (
+                                    cachedTemplates >= tplNames.length &&
+                                        typeof onSuccess === 'function'
+                                ) {
+                                    onSuccess();
+                                }
+                            },
+                            error: function (jqXHR, textStatus, errorThrown) {
+                                console.error(
+                                    'templateManagerError: ' +
+                                        errorThrown
+                                );
+                            }
+                        });
+                    } else {
+                        // template is already cached
+                        cachedTemplates += 1;
+                        // if all templates are cached launch callback
+                        if (
+                            cachedTemplates >= tplNames.length &&
+                                typeof onSuccess === 'function'
+                        ) {
+                            onSuccess();
+                        }
+                    }
+                });
+
+            }
+        },
+
+        /**
+        * Returns template completed by specified params
+        * @param {string} tplHtml
+        * @param {string} tplParams
+        */
+        getCompleted: function TemplateManager_getCompleted(
+            tplHtml,
+            tplParams
+        ) {
+            var tplParam;
+
+            for (tplParam in tplParams) {
+                if (tplParams.hasOwnProperty(tplParam)) {
+                    tplHtml = this.passThruModifiers(
+                        tplHtml,
+                        tplParam,
+                        tplParams[tplParam]
+                    );
+                }
+            }
+
+            return tplHtml;
+        },
+
+        /**
+         * Returns template completed by specified params
+         * including modifiers
+         * @param {string} tplHtml
+         * @param {string} tplParams
+         * @param {string} content
+         */
+        passThruModifiers: function (tplHtml, tplParam, content) {
+            var regModOn = new RegExp('%' + tplParam + '(\\|(.+?)){1,}%', 'g'),
+                regModOff = new RegExp(['%', tplParam, '%'].join(''), 'g'),
+                regModGet = new RegExp('%' + tplParam + '\\|(.+?)%'),
+                regModPut = new RegExp('%' + tplParam + '\\|(.+?)%', 'g'),
+                specRegExp = new RegExp('\\$', 'g'),
+                modifiers,
+                i;
+
+            if (content && (typeof content === 'string')) {
+                content = content.replace(specRegExp, '$$$$');
+            }
+
+            if (regModOn.test(tplHtml)) {
+                modifiers = tplHtml.match(regModGet)[1].split('|');
+                for (i in modifiers) {
+                    if (this.modifiers[modifiers[i]] instanceof Function) {
+                        content = this.modifiers[modifiers[i]](content);
+                    } else {
+                        console.error('unknown modifier: ' + modifiers[i]);
+                    }
+                }
+                tplHtml = tplHtml.replace(regModPut, content);
+            }
+            tplHtml = tplHtml.replace(regModOff, content);
+
+            return tplHtml;
+        }
+    };
+
+}());