npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mentatjs

v1.2.6

Published

MVC JS Framework

Downloads

3

Readme

mentat-js

MentatJS is a javascript library for the creation of Single Page Applications.

FEATURES:

  • No dependencies
  • Module loading

BUILDING BLOCKS

OVERVIEW

MentatJS.View, MentatJS.ViewController, MentatJS.Application, MentatJS.NavigationController are the building blocks of a SPA built with MentatJS

On startup, an Application object is instantiated and a root View is created and attached to the body of the web page. A root NavigationController object then takes charge of loading and presenting View Controllers into that root View.

THE VIEW OBJECT

The View object is the simplest component. It creates a HTML DIV element when initialised. Labels, Buttons, ListView all inherit View properties and functions.

MyView = MentatJS.View.extend({

});

Extensibility is done by overriding functions:

viewWillLoad: function () {
    // called after the div element is created
    this.getDiv().style.backgroundColor = "red";
},
viewDidLoad: function () {
    // called after viewWillLoad
},
viewWasAttached: function () {
    // called after the view is attached ( the div element is now on the DOM )
}

A view position on the page can be set by overriding the boundsForView:

boundsForView: function (parentBounds) {
    return {
        x: 10,
        y: 10,
        width: 300,
        height: 200,
        unit: "px",
        position: "absolute"
    };
}

the function is called automatically after the view is attached and the result is stored in the property bounds A view when added as a subview is initialised in this manner :

var myView = new MyView();
myView.initView('some_unique_id');

and attached to its parent view this way:

this.attach(myView);

View can contains other views:

viewWasAttached: function () {
    this.myButton = new MentatJS.Button();
    this.myButton.boundsForView = function (parentBounds) {
        return {
            x: 10,
            y: 10,
            width: 150,
            height: 30,
            unit: "px",
            position: "absolute"
        };
    };
    this.myButton.text = "CLICK ME";
    this.myButton.initView(this.id + ".myButton");
    this.attach(this.myButton);
}
THE VIEW CONTROLLER OBJECT

The View Controller object is used to answer to user events ( clicks or taps )

MyViewController = MentatJS.ViewController.extend({

    viewForViewController: function () {
        return new MyView();
    },

    viewWasPresented: function () {
        this.view.myButton.setActionDelegate(this, 'onButtonClicked');
    },

    onButtonClicked: function (sender) {
        window.alert('Button was clicked');
    }

});

When the view controller is initialised by the navigation controller, the function viewForViewController is called to instantiate the view that will be added to the root view.

viewForViewController: function () {
    return new MyView();
},

Once the view is initialised and attach to the root view, we can now hook up actions and set content dynamically:

viewWasPresented: function () {
    this.view.myButton.setActionDelegate(this, 'onButtonClicked');
},

THE NAVIGATION CONTROLLER OBJECT

The NavigationController object loads and presents view controllers on the root view it is managing.

you can call the navigation controller on a view controller to load a different view controller and push it to the screen.

onButtonClicked: function (sender) {
    this.navigationController.loadViewController(
        { class: "NextViewController", id: "NextViewControllerUniqueID" },
        [ { uri: "NextVC.js", id: "NextViewController"},
          { uri: "NextView.js", id: "NextView"}
        ], this);
}

the js files are loaded asynchronously and the function viewControllerWasLoadedSuccessfully is called, to give us a chance to pass data between view controllers.

viewControllerWasLoadedSuccessfully : function (vc) {
    vc.some_data_to_pass_along = this.some_data_we_want_to_keep;
    this.navigationController.present(vc, {animated: true});
}

GET STARTED

  1. MyApplication

Create a new javascript file MyApplication.js:

MyApplication = MentatJS.Application.extend({

    applicationWillStart: function () {
        // add code here
    }
});

and hook up the onload event

var _myApp = null;
window.onload = function () {
    _myApp = new MyApplication();
    _myApp.launch();
};
  1. Define a new View and its Controller

Create the file MyView.js:

MentatJS.declare("MyView", function () {
    MyView = MentatJS.View.extend({
        boundsForView: function (parentBounds) {
            return MentatJS.fillParentBounds(parentBounds);
        },

        viewWasAttached: function () {
            this.label = new MentatJS.Label();
            this.label.boundsForView = function (parentBounds) {
                return {
                    x: 0,
                    y: parentBounds.height - 11,
                    width: parentBounds.width,
                    height: 11,
                    unit: "px",
                    position: "absolute"
                };
            };
            this.label.textAlignment = 'center';
            this.label.initView(this.id + ".label");
            this.attach(this.label);
        }
    });
});

the view declaration is done by the the code

MyView = MentatJS.View.extend({

});

we define its position with boundsForView which should return the view boundaries

boundsForView: function (parentBounds) {
    return {
        x: 0,
        y: 0,
        width: parentBounds.width,
        height: parentBounds.height,
        unit: "px",
        position: "absolute"
    };
}

as we want to use all the space from the rootView ( created automatically by the call MyApplication.launch() ), we just use the utility function MentatJS.fillParentBounds()

boundsForView: function (parentBounds) {
            return MentatJS.fillParentBounds(parentBounds);
        }

we override the function viewWasAttached which is called after our view is attached to the rootView; and we create a label centered in the middle

this.label = new MentatJS.Label();
this.label.boundsForView = function (parentBounds) {
                return {
                    x: 0,
                    y: parentBounds.height - 11,
                    width: parentBounds.width,
                    height: 11,
                    unit: "px",
                    position: "absolute"
                };
};
this.label.textAlignment = 'center';
this.label.initView(this.id + ".label");
this.attach(this.label);

here we set the label's text to be aligned in the center and we initialise and give it an id based on the view id and by adding ".label" to it.

finally, we attach the label to our view.

We enclose our code with MentatJS.declare as to allow the view to be loaded at runtime

Now lets create the Controller: Create the file MyViewController.js:

MentatJS.declare("MyViewController", function () {
    MyViewController = MentatJS.ViewController.extend({
        viewForViewController: function () {
            return new MyView();
        },

        viewWasPresented: function () {
            this.view.label.setText("HELLO WORLD!");
        }
    });
});

the ViewController is loaded by the NavigationController, we specify which view to use by overriding viewForViewController:

viewForViewController: function () {
    return new MyView();
}

and finally we override viewWasPresented which is called when the view is displayed on the screen; and set the label's text

viewWasPresented: function () {
    this.view.label.setText("HELLO WORLD")
}
  1. Load our View and Controller

lets return to our MyApplication.js file and load our V/VC.

MyApplication = MentatJS.Application.extend({

    applicationWillStart: function () {
        this.navigationController.loadViewController(
                    { class: 'MyViewController', id: 'MyViewController' },
                [
                    { id: 'MyView', uri:'MyView.js'},
                    { id : "MyViewController", uri:'MyViewController.js'}
                ], this);
    },

    viewControllerWasLoadedSuccessfully : function (vc) {
        this.navigationController.present(vc, {animated: false});
    }

});

var _myApp = null;
window.onload = function () {
    _myApp = new MyApplication();
    _myApp.launch();
};

In applicationWillStart, we ask the navigationController to load our view controller and its view

this.navigationController.loadViewController(
                    { class: 'MyViewController', id: 'MyViewController' },
                [
                    { id: 'MyView', uri:'MyView.js'},
                    { id : "MyViewController", uri:'MyViewController.js'}
                ], this);

the first parameter is the view controller class we want to instantiate and the unique id we want to give it. the second parameter is an array of the files to load, the id must match the id given by the MentatJS.declare call the third parameter is the delegate object which should implement the function viewControllerWasLoadedSuccessfully. viewControllerWasLoadedSuccessfully is called when MentatJS.declare is called for every file specified in the second parameter of the call loadViewController Here we tell the navigation controller to present the view on the web page.

this.navigationController.present(vc, {animated: false});
  1. BRINGING IT ALL TOGETHER

Finally, we just add the mentat.js library and our MyApplication.js file to HTML page

<script type="text/javascript" src="../../mentat.min.js"></script>

<script type='text/javascript'>
    <!--
    document.write("<script type='text/javascript' src='MyApplication.js?v=" + Date.now() + "'><\/script>");
    -->
</script>