/*
Name: Mag-importer v0.1.1
Description: similar to html-imports implementation
Author: Michael Glazer
License: MIT
Homepage: https://github.com/magnumjs/mag.js
@TODO: implement css loader
@requires mag.js & mag addons
(c) 2016
*/
mag.importer = function(url, eleSelector) {

  function checkIfIncluded(file) {
    var links = document.getElementsByTagName("link");
    for (var i = 0; i < links.length; i++) {
      if (links[i].href.substr(-file.length) == file) {
        return true;
      }
    }

    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
      if (scripts[i].src.substr(-file.length) == file) {
        return true;
      }
    }

    return false;
  }

  function loadInternalScripts(node, type) {
    var scripts = node.getElementsByTagName(type);
    // multiple promises
    var promises = [];
    var names = {}
    for (var i = 0; i < scripts.length; i++) {
      // and doesn't already exist
      var url = type == 'script' ? scripts[i].attributes.src.value : scripts[i].attributes.href.value;
      if (url != "") {

        if (checkIfIncluded(url)) {
          promises.push(mag.importer.namedPromises[url].promise);
          continue;
        }

        var deferred = mag.deferred();
        if (!names[url]) {
          mag.importer.namedPromises[url] = names[url] = deferred;
        } else continue;

        var tag = document.createElement(type);
        tag[(type == 'script' ? 'src' : 'href')] = url;
        if (type == 'link') tag.rel = 'stylesheet';

        tag.onload = function() {
          deferred.resolve(1);
        }

        document.getElementsByTagName("head")[0].appendChild(tag);
        promises.push(deferred.promise);

      }
    }
    return promises;
  }

  var deferred = mag.deferred();

  mag.request({
      url: url,
      cache: true
    })
    .then(function(data) {
      //save data
      //get html as node
      var template = document.createElement('template');
      template.innerHTML = data;
      var newNode = template.content.firstChild;

      var node = document.querySelector(eleSelector);

      var promises1 = loadInternalScripts(newNode, 'script');
      var promises2 = loadInternalScripts(newNode, 'link');
      var promises = mag.merge(promises1, promises2);

      mag.when(promises, function() {
        // multiple invocations
        newNode.children[0].id = eleSelector
        node.appendChild(newNode.children[0]);
        deferred.resolve(1);
      })

    });
  return deferred.promise;
};

mag.importer.namedPromises = {};

@@ -20,17 +20,17 @@
 er v0.1.
-1
+2
 %0ADescrip
@@ -159,36 +159,8 @@
 .js%0A
-@TODO: implement css loader%0A
 @req
@@ -691,16 +691,178 @@
 e;%0A  %7D%0A%0A
+   function getId(id)%7B%0A    if(mag.importer.ids%5Bid%5D)%7B%0A      return id+(++mag.importer.ids%5Bid%5D);%0A    %7D else %7B%0A      return id+(mag.importer.ids%5Bid%5D='');%0A    %7D%0A  %7D%0A%0A
   functi
@@ -1858,17 +1858,16 @@
 omise);%0A
-%0A
       %7D%0A
@@ -1904,41 +1904,14 @@
 %0A%0A  
-var deferred = mag.deferred();%0A%0A 
+return
  mag
@@ -2176,63 +2176,8 @@
 d;%0A%0A
-      var node = document.querySelector(eleSelector);%0A%0A
     
@@ -2348,22 +2348,66 @@
 ises2);%0A
-%0A
      
+ var deferred = mag.deferred();%0A%0A      return
  mag.whe
@@ -2463,16 +2463,101 @@
 cations%0A
+        if(eleSelector)%7B%0A          var node = document.querySelector(eleSelector);%0A  
         
@@ -2601,16 +2601,18 @@
         
+  
 node.app
@@ -2642,32 +2642,34 @@
 en%5B0%5D);%0A        
+  
 deferred.resolve
@@ -2673,60 +2673,334 @@
 lve(
-1);%0A      %7D)%0A%0A    %7D);%0A  return deferred.promise;%0A%7D;%0A
+eleSelector);%0A        %7D else %7B%0A          // track incoming ids and increment%0A           newNode.children%5B0%5D.id = getId(newNode.children%5B0%5D.id)%0A           deferred.resolve(%7B_html: newNode.children%5B0%5D.outerHTML, id: newNode.children%5B0%5D.id%7D);%0A        %7D%0A        return deferred.promise;%0A      %7D);%0A    %7D);%0A%7D;%0Amag.importer.ids = %7B%7D;
 %0Amag

Update mag-importer.js