<template>
  <list>
    <cell repeat="{{items}}">
      <example-list-item title="{{title}}" url="{{url}}"></example-list-item>
    </cell>
  </list>
</template>

<script>
  require('weex-components');
  module.exports = {
    data: {
      root: '', // examples, examples/syntax, test ...
      items: [
        {name: 'hello', title: 'Hello World', url: ''}
      ]
    },
    created: function() {
      var bundleUrl = this.$getConfig().bundleUrl;
      console.log('hit', bundleUrl);
      var dirs = this.root.split('/');
      dirs.forEach(function(dir, index) {
        if (!dir) dirs.splice(index, 1);
      });
      var root = dirs.length > 0 ? dirs[0] : '';
      var subRoot = dirs.length > 1 ? dirs.slice(1).join('/') + '/' : '';

      var nativeBase;
      var isAndroidAssets = bundleUrl.indexOf('your_current_IP') >= 0 || bundleUrl.indexOf('file://assets/')>=0;
      var isiOSAssets = bundleUrl.indexOf('file:///') >= 0 && bundleUrl.indexOf('WeexDemo.app') > 0;
      if (isAndroidAssets) {
        nativeBase = 'file://assets/';
      }
      else if (isiOSAssets) {
        // file:///var/mobile/Containers/Bundle/Application/{id}/WeexDemo.app/
        // file:///Users/{user}/Library/Developer/CoreSimulator/Devices/{id}/data/Containers/Bundle/Application/{id}/WeexDemo.app/
        nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf('/') + 1);
      }
      else {
        var host = 'localhost:12580';
        var matches = /\/\/([^\/]+?)\//.exec(this.$getConfig().bundleUrl);
        if (matches && matches.length >= 2) {
          host = matches[1];
        }
        nativeBase = '//' + host + '/' + root + '/build/' + subRoot;
      }
      var h5Base = './index.html?page=./' + root + '/build/' + subRoot;
      // in Native
      var base = nativeBase;
      if (typeof window === 'object') {
        // in Browser or WebView
        base = h5Base;
      }

      for (var i in this.items) {
        var item = this.items[i];
        if (!item.url) {
          item.url = base + item.name + '.js';
        }
      }
      // see log in Android Logcat
      if (this.items.length) console.log('hit', this.items[0].url);
    }
  }
</script>

<style>
</style>