block by bollwyvl 88abdc758a07cda4ab1c

88abdc758a07cda4ab1c

Full Screen

index.js

var derby = require('derby');

var app = module.exports = derby.createApp('notebook', __filename);

app.use(require('derby-debug'));

app.loadViews(__dirname);

app.get('/:name', function(page, model, params) {

  notebookQuery = model.query("notebooks", {name: params.name})
  notebookQuery.fetch(function(err) {
    notebooks = notebookQuery.get()
    var notebookId;
    if(!notebooks.length) {
      notebookId = model.add("notebooks", {name: params.name})
    } else {
      notebookId = notebooks[0].id;
    }

    var notebook = model.at("notebooks." + notebookId)
    var cells = model.query("cells", {_notebook: notebookId})

    model.subscribe(notebook, cells, function(err) {
      model.ref("_page.notebook", notebook);
      page.render();
    })
  })
});

// define the controller for our sample component
function Notebook() {}
Notebook.prototype.init = function() {
  var model = this.model;
  var notebookId = model.scope("_page.notebook.id").get()
  model.set("notebookId", notebookId);

  // model.scope gives you access to the root scope
  var filter = model.filter(model.scope("cells"),
    function(c){ return c._notebook === notebookId })
    .sort(function(a, b){ return a._weight - b._weight; });

  // reference the filtered cells in our local scope
  model.ref("cells", filter, {updateIndices: true});
}

//create only ever runs in the browser
Notebook.prototype.create = function() {
  console.log("im in the browser only!")
}

// a view function available within the scope of our function
Notebook.prototype.addCell = function(after) {
  var model = this.model;
  var ordered = model.get("cells");
  var cells = model.scope("cells");
  var cellId = model.get("currentCell");

  var newCell = {
    _notebook: model.get("notebook.id"),
    _weight: 1e6 // a sensible default?
  };

  if(cellId){
    var cell = ordered.filter(function(cell){ return cell.id === cellId; })[0];

    var cidx = ordered.indexOf(cell);

    var weight = {
      prev: cidx == 0 ? 0 : ordered[cidx - 1]._weight,
      current: ordered[cidx]._weight,
      next: cidx == ordered.length - 1 ?
        ordered[cidx]._weight + 1e3 :
        ordered[cidx + 1]._weight
    };

    newCell._weight = 0.5 * (
      weight.current + (after ? weight.next : weight.prev)
    );
  }

  model.set("currentCell", cells.add(newCell));
}

//associate the component's controller with the view
app.component("notebook", Notebook);


// define the controller for our sample component
function Cell() {}

Cell.prototype.selectCell = function(){
  this.model.set("currentCell", this.model.get("cell.id"));
}

//associate the component's controller with the view
app.component("cell", Cell);

// global view function
app.proto.test = function() {
  console.log("test")
}

index.html

<Body:>
  <h1>Hello, world!</h1>

  <view is="notebook" notebook="{{_page.notebook}}"></view>


<notebook:>
  hey there!<br>
  notebook name: {{notebook.name}}<br>
  notebook id: {{notebook.id}}<br>
  cells:<br>

  Add Cell <button on-click="addCell()">
    before
  </button> <button on-click="addCell(true)">
    after
  </button> {{currentCell}}

  <ul>
  {{each cells as #cell, #i}}
    <view name="cell"
          cell="{{#cell}}" idx="{{#i}}" currentCell="{{currentCell}}"/>
  {{/each}}
  </ul>

<cell:>
    <li on-click="selectCell()"
        style="border: solid 1px {{if currentCell == cell.id}}blue{{else}}white{{/if}}">
      cell {{idx}} {{cell.id}} <br>
      <textarea>{{cell.source}}</textarea>
    </li>

Dockerfile

FROM dockerfile/nodejs

ADD package.json /srv/
WORKDIR /srv/
RUN npm install .

ADD patch-components-483.sh /srv/
RUN ./patch-components-483.sh

VOLUME /srv/app/

WORKDIR /srv/app/
EXPOSE 3000

CMD ["./run.sh"]

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/trusty64"

  config.vm.provider "virtualbox" do |vbox|
    vbox.name = "my-app"
    vbox.memory = 1024
    vbox.cpus = 2
  end

  config.vm.network "forwarded_port", guest: 3000, host: 3000,
    auto_correct: true

  config.vm.provision :shell,
    inline: <<-eos
      add-apt-repository ppa:docker-maint/testing
      apt-get update
      apt-get install -y docker.io

      usermod -a -G docker vagrant

      wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
      python get-pip.py

      /usr/local/bin/pip install -q -U docker-compose
      yes | /usr/local/bin/pip uninstall -q requests
      /usr/local/bin/pip install -q requests==2.4.3
      cd /vagrant
      docker-compose build
    eos
end

docker-compose.yml

app:
  build: .
  restart: always
  links:
    - redis
    - mongo
  ports:
    - "3000:3000"
  volumes:
    - ./:/srv/app/

redis:
  image: redis

mongo:
  image: dockerfile/mongodb

package.json

{
  "name": "notebook",
  "version": "0.1.0",
  "description": "Mini derby-app",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "keywords": [
    "derby",
    "mini"
  ],
  "dependencies": {
    "derby-starter": "*",
    "derby-debug": "*",
    "derby": "*"
  },
  "author": "",
  "license": "MIT"
}

patch-components-483.sh

grep -rFl \
    "model.root.ref(model._at + '.' + key, segments.join('.'));" \
    node_modules \
  | xargs sed -i \
    's/\(.*model.root.ref.*segments.*\)).*/\1, {updateIndices: true});/g'

run.sh

#!/bin/bash
MONGO_HOST=$MONGO_PORT_27017_TCP_ADDR \
  MONGO_PORT=$MONGO_PORT_27017_TCP_PORT \
  REDIS_HOST=$REDIS_PORT_6379_TCP_ADDR \
  REDIS_PORT=$REDIS_PORT_6379_TCP_PORT \
  node server.js

server.js

require('derby-starter').run(__dirname, {port: 3000});