无论你是互联网世界的一个高手或是一个从来没有接触过互联网的新手,这篇文章将给你带来完整的在Ubuntu平台上开发HTML 5的应用。我们将慢慢地通过这个练习让你很自然地进入并熟悉整个的HTML 5应用的开发流程。在这个练习中,我们将设计一个最简单的RSS阅读器。当我们的应用设计完整后,应用的显示如下:
如果你是一个固执的HTM 5黑客,你可以选择任何你所喜欢的工具及工具包来开发你的HTML 5应用。它们将会很好地工作于Ubuntu手机上。我们将只专注于Ubuntu SDK提供的工具及工具包。更多关于HTML 5开发的信息可以在Ubuntu的官方网站或中文网站得到。
如果你还没有安装好自己的Ubuntu SDK,请参照文章“Ubuntu SDK 安装”来安装好自己的SDK。
特别提醒:在模拟器中参阅文章”怎么在Ubuntu手机中打开开发者模式“打开开发者模式,这样才可以把应用部署到模拟器中。
http://192.168.1.106:9221
<div data-role="content">
<div data-role="content">
<body> <div data-role="mainview"> <header data-role="header"> <ul data-role="tabs"> <li data-role="tabitem" data-page="hello-page">Hello World</li> </ul> </header> <div data-role="content"> </div> </div> <script src="js/app.js"></script> </body>
function addClass(elem, className) { elem.className += ' ' + className; }; function removeClass(elem, className) { elem.className = elem.className.replace(className, ''); };
// Detect if Cordova script is uncommented or not and show the appropriate status.
};
/** * Wait before the DOM has been loaded before initializing the Ubuntu UI layer */ window.onload = function () { var UI = new UbuntuUI(); UI.init(); };
<title>An Ubuntu HTML5 application</title> <meta name="description" content="An Ubuntu HTML5 application">
<title>RSS Mobile Reader</title> <meta name="description" content="RSS Mobile Reader">
<ul data-role="tabs"> <li data-role="tabitem" data-page="hello-page">Hello World</li> </ul>
<div data-role="content">
<div data-role="pagestack"> <div data-role="page" id="main" data-title="RSS Mobile Reader"> </div> </div>
UI.pagestack.push("main");
<script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/list.js"></script>
<section data-role="list" id="yourfeeds"></section>
UI.pagestack.push("main");
if (typeof localStorage["feeds"] == "undefined") { restoreDefault(); } //load local storage feeds var feeds = eval(localStorage["feeds"]); if (feeds !== null) { var feeds_list = UI.list('#yourfeeds'); feeds_list.removeAllItems(); feeds_list.setHeader('My feeds'); for (var i = 0; i < feeds.length; i++) { feeds_list.append(feeds[i], null, null, function (target, thisfeed) { loadFeed(thisfeed); }, feeds[i]); } }
function restoreDefault() { localStorage.clear(); var feeds = []; feeds.push("http://daker.me/feed.xml"); feeds.push("http://www.omgubuntu.co.uk/feed"); feeds.push("http://hespress.com/feed/index.rss"); feeds.push("http://rss.slashdot.org/Slashdot/slashdot"); feeds.push("http://www.reddit.com/.rss"); feeds.push("http://www.guokr.com/rss/"); try { localStorage.setItem("feeds", JSON.stringify(feeds)); window.location.reload(); } catch (e) { if (e == QUOTA_EXCEEDED_ERR) { console.log("Error: Local Storage limit exceeds."); } else { console.log("Error: Saving to local storage."); } } }
cp /usr/share/javascript/jquery/jquery.min.js .
git clone https://github.com/jfhovinne/jFeed.git
<!-- External javascript imports --> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.jfeed.pack.js"></script>
<div data-role="content"> <div data-role="pagestack"> <div data-role="page" id="main" data-title="RSS Mobile Reader"> <section data-role="list" id="yourfeeds"></section> </div> <div data-role="page" id="results" data-title="Articles >"> <section data-role="list" id="resultscontent"></section> </div> </div> <div data-role="dialog" id="loading"><section><progress></progress></section></div> </div>
window.onload = function () { var UI = new UbuntuUI();
var UI = new UbuntuUI(); $(document).ready(function () {
$(document).ready(function () { UI.init(); UI.pagestack.push("main"); if (typeof localStorage["feeds"] == "undefined") { restoreDefault(); } //load local storage feeds var feeds = eval(localStorage["feeds"]); if (feeds !== null) { var feeds_list = UI.list('#yourfeeds'); feeds_list.removeAllItems(); feeds_list.setHeader('My feeds'); for (var i = 0; i < feeds.length; i++) { feeds_list.append(feeds[i], null, null, function (target, thisfeed) { loadFeed(thisfeed); }, feeds[i]); } } });
function loadFeed(url) { UI.pagestack.push("results"); UI.dialog("loading").show() console.log("url is: " + url ); $.getFeed( { url: url, success: function(feed) { UI.dialog("loading").hide(); var results_list = UI.list('#resultscontent'); results_list.removeAllItems(); results_list.setHeader(feed.title); console.log("title: " + feed.title); // walk through the feeds for( var i = 0; i < feed.items.length; i++ ) { var item = feed.items[ i ]; // console.log("title: " + item.title); // console.log("link: " + item.link); // console.log("content: " + item.description); results_list.append( item.title.replace(/"/g, "'"), null, null, function (target, result_infos) { showArticle.apply(null, result_infos); }, [ escape(item.title), escape(item.link), escape(item.description) ] ); } } }); }
<div data-role="content"> <div data-role="pagestack"> <div data-role="page" id="main" data-title="RSS Mobile Reader"> <section data-role="list" id="yourfeeds"></section> </div> <div data-role="page" id="results" data-title="Articles >"> <section data-role="list" id="resultscontent"></section> </div> <div data-role="page" id="article" data-title="Article >"> <section id="articleinfo"></section> </div> </div> <div data-role="dialog" id="loading"><section><progress></progress></section></div> </div>
function showArticle(title, url, desc) { UI.pagestack.push("article"); if (typeof desc == "undefined") desc = "(No description provided)"; $("#articleinfo").html("<p>" + unescape(title) + "</p><p>" + unescape(desc) + "</p><p><a target=\"_blank\" href=\"" + unescape(url) + "\">" + unescape(url) + "</a></p>"); }
<script src="/usr/share/ubuntu-html5-ui-toolkit/0.1/ambiance/js/toolbars.js"></script>
<div data-role="page" id="main" data-title="RSS Mobile Reader"> <section data-role="list" id="yourfeeds"></section> <footer id="footer1" data-role="footer" class="revealed"> <nav> <ul> <li> <a href="#" id="addfeed"> <img src="/usr/share/ubuntu-html5-ui-toolkit/0.1//ambiance/img/actions/add.svg" alt="Add feed" /> <span>Add feed</span> </a> </li> </ul> </nav> </footer> </div>
<div data-role="dialog" id="loading"><section><progress></progress></section></div> <div data-role="dialog" id="addfeeddialog"> <section> <h1>Add a new feed</h1> <p>Type the url feed you want to add</p> <input type="url" id="rssFeed" placeholder="http://"> <menu> <button data-role="button" id="no">Cancel</button> <button data-role="button" class="success" id="yes">Add</button> </menu> </section>
$(document).ready(function () {
UI.button('yes').click(function (e) { var url = $("#rssFeed").val(); if (url !== "") { var feeds = eval(localStorage["feeds"]); feeds.push(url); localStorage.setItem("feeds", JSON.stringify(feeds)); window.location.reload(); } }); UI.button('addfeed').click(function () { $('#addfeeddialog').show(); });
#articleinfo { padding: 10px; -webkit-box-sizing: border-box; box-sizing: border-box; } #articleinfo iframe { max-width: 100%; } #articleinfo p { margin: 7px 0; } #articleinfo a{ text-decoration: none; color: #787878; font-weight: bold; }