Преглед изворни кода

First approach to DOM access optimization

@soyjavi пре 14 година
родитељ
комит
34dbc2f187
4 измењених фајлова са 66 додато и 29 уклоњено
  1. 0 1
      src/Lungo.Constants.js
  2. 17 0
      src/Lungo.Element.js
  3. 17 12
      src/boot/Lungo.Boot.Section.js
  4. 32 16
      src/router/Lungo.Router.js

+ 0 - 1
src/Lungo.Constants.js

@@ -5,7 +5,6 @@
  * @class Constants
  *
  * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
- * @author Guillermo Pascual <pasku@tapquo.com> || @pasku1
  */
 
 LUNGO.Constants = {

+ 17 - 0
src/Lungo.Element.js

@@ -0,0 +1,17 @@
+/**
+ * Chain of responsability
+ *
+ * @namespace LUNGO
+ * @class Element
+ *
+ * @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
+ */
+
+LUNGO.Element = {
+
+    sections: null,
+    Current: {
+        section: null,
+        article: null
+    }
+};

+ 17 - 12
src/boot/Lungo.Boot.Section.js

@@ -20,32 +20,37 @@ LUNGO.Boot.Section = (function(lng, undefined) {
      * @method init
      */
     var start = function() {
-        var sections = lng.dom(ELEMENT.SECTION);
-        _initFirstSection(sections);
-        _initAllSections(sections);
+        lng.Element.sections = lng.dom(ELEMENT.SECTION);
+        _initFirstSection();
+        _initAllSections();
 
         lng.View.Resize.toolbars();
     };
 
-    var _initFirstSection = function(sections) {
-        var first_section = sections.first();
-        var first_section_id = '#' + first_section.attr(ATTRIBUTE.ID);
+    var _initFirstSection = function() {
+        var first_section = lng.Element.sections.first();
+        lng.Element.Current.section = first_section;
 
+        var first_section_id = '#' + first_section.attr(ATTRIBUTE.ID);
         first_section.addClass(CLASS.CURRENT);
         lng.Router.History.add(first_section_id);
     };
 
-    var _initAllSections = function(sections) {
-        lng.Fallback.positionFixed(sections);
+    var _initAllSections = function() {
+        lng.Fallback.positionFixed(lng.Element.sections);
 
-        for (var i = 0, len = sections.length; i < len; i++) {
-            var section = lng.dom(sections[i]);
-            _initArticles(section);
+        for (var i = 0, len = lng.Element.sections.length; i < len; i++) {
+            _initArticles(i);
         }
     };
 
-    var _initArticles = function(section) {
+    var _initArticles = function(section_index) {
+        var section = lng.dom(lng.Element.sections[section_index]);
+
         var first_article = section.children(ELEMENT.ARTICLE).first();
+        if (!lng.Element.Current.article) {
+            lng.Element.Current.article = first_article;
+        }
         first_article.addClass(CLASS.CURRENT);
 
         var first_article_id = first_article.attr(ATTRIBUTE.ID);

+ 32 - 16
src/router/Lungo.Router.js

@@ -14,6 +14,15 @@ LUNGO.Router = (function(lng, undefined) {
     var ELEMENT = lng.Constants.ELEMENT;
     var ERROR = lng.Constants.ERROR;
     var TRIGGER = lng.Constants.TRIGGER;
+    var HASHTAG_CHARACTER = '#';
+
+    var _sections = [];
+
+    var init = function() {
+        if(!_sections) {
+            _sections = lng.dom(ELEMENT.SECTION);
+        }
+    };
 
     /**
      * Navigate to a <section>.
@@ -23,15 +32,19 @@ LUNGO.Router = (function(lng, undefined) {
      * @param {string} Id of the <section>
      */
     var section = function(section_id) {
-        var section_id = lng.Core.parseUrl(section_id);
-        var current = _getHistoryCurrent();
-        var target = ELEMENT.SECTION + section_id;
+        section_id = lng.Core.parseUrl(section_id);
+        var current =  lng.Element.Current.section;
 
-        if (_exists(target) && _notCurrentTarget(target)) {
-            lng.dom(current).removeClass(CLASS.SHOW).addClass(CLASS.HIDE);
-            lng.dom(target).addClass(CLASS.SHOW).trigger(TRIGGER.LOAD);
+        if (_notCurrentTarget(section_id, current)) {
+            var target = lng.dom(ELEMENT.SECTION + section_id);
+
+            if (target) {
+                current.removeClass(CLASS.SHOW).addClass(CLASS.HIDE);
+                target.addClass(CLASS.SHOW).trigger(TRIGGER.LOAD);
 
-            lng.Router.History.add(section_id);
+                lng.Element.Current.section = target;
+                lng.Router.History.add(section_id);
+            }
         }
     };
 
@@ -44,8 +57,8 @@ LUNGO.Router = (function(lng, undefined) {
      * @param {string} <article> Id
      */
     var article = function(section_id, article_id) {
-        var section_id = lng.Core.parseUrl(section_id);
-        var article_id = lng.Core.parseUrl(article_id);
+        section_id = lng.Core.parseUrl(section_id);
+        article_id = lng.Core.parseUrl(article_id);
         var target = ELEMENT.SECTION + section_id + ' ' + ELEMENT.ARTICLE + article_id;
 
         if (_exists(target) && _notCurrentTarget(target)) {
@@ -63,8 +76,8 @@ LUNGO.Router = (function(lng, undefined) {
      * @param {string} <aside> Id
      */
     var aside = function(section_id, aside_id) {
-        var section_id = lng.Core.parseUrl(section_id);
-        var aside_id = lng.Core.parseUrl(aside_id);
+        section_id = lng.Core.parseUrl(section_id);
+        aside_id = lng.Core.parseUrl(aside_id);
         var target = ELEMENT.ASIDE + aside_id;
 
         if (_exists(target)) {
@@ -83,15 +96,18 @@ LUNGO.Router = (function(lng, undefined) {
      * @method back
      */
     var back = function() {
-        var current_section = ELEMENT.SECTION + _getHistoryCurrent();
+        var current = lng.Element.Current.section;
+        current.removeClass(CLASS.SHOW).trigger(TRIGGER.UNLOAD);
 
-        lng.dom(current_section).removeClass(CLASS.SHOW).trigger(TRIGGER.UNLOAD);
         lng.Router.History.removeLast();
-        lng.dom(_getHistoryCurrent()).removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
+
+        target = lng.dom(_getHistoryCurrent());
+        target.removeClass(CLASS.HIDE).addClass(CLASS.SHOW);
+        lng.Element.Current.section = target;
     };
 
-    var _notCurrentTarget = function(target) {
-        return lng.dom(target).hasClass(CLASS.CURRENT) ? false : true;
+    var _notCurrentTarget = function(target, element) {
+        return (target !== HASHTAG_CHARACTER + element.attr('id')) ? true : false;
     };
 
     var _exists = function(target) {