Categories
discuss

Given a permission name, how do I find the PermissionGroup?

I want to look up the permission group for a permission at runtime (to provide guidance in the UI about how to visit Settings and then enable the permission, after the user has checked the “never ask again” button on the prompt). So I need the human-readable name for the permission group, but all I have is the machine-readable fully qualified permission string (Manifest.permission.READ_PHONE_STATE). Is there a way to do this?

Answer

getPermissionInfo() on PackageManager returns a PermissionInfo given the name of the permission. PermissionInfo has a group field that contains the group name ("android.permission-group.CALENDAR", a.k.a. Manifest.permission_group.CALENDAR).

getPermissionGroupInfo() on PackageManager returns a PermissionGroupInfo for that group name. On that, call loadLabel(), supplying the PackageManager as input, to get the human-readable name of the permission group.

Categories
discuss

Final piece of javascript closures I stil don’t understand

Lets say we have defined this function in the global scope:

function createCounter() {
  var counter = 0;

  function increment() {
    counter = counter + 1;

    console.log("Number of events: " + counter);
  }

  return increment;
}

In most examples explaining closures I see executing:

createCounter();

from the global scope would just return the inner function:

function increment() {
    counter = counter + 1;

    console.log("Number of events: " + counter);
}

Now that makes total sense, because of this line in createCounter function declaration

return increment;

So my question is, why does this:

var counter1 = createCounter();

counter1();

Number of events: 1 //result

Finally get the function to work?

Essentially aren’t counter1 and createCounter both pointers to that inner function which exist in the global scope?

Maybe a better way to ask this is why does counter1() work and not just return the inner function like createCounter does?

Answer

No, createCounter is a function that returns a separate instance of the increment function holding a new closure with a different local counter variable.

So yes, you need the extra call to get the separate instance of the function and call that as many times as you wish. Notice that calling createCounter doesn’t increase the counter, but calling counter1 or counter2 did increase it.

var counter1 = createCounter(); //local counter is still 0
var counter2 = createCounter();
counter1(); // 1
counter2(); // 1
counter1(); // 2
counter2(); // 2
Categories
discuss

ReactJS JSX toString()

I’m writing a documentation website based on React. I want to show the code that is necessary to use a given component from my framework. At the same time I would like to show the actual component running, like a side-by-side view.

Currently, I’m adding the component as a String for the reference implementation and the component as JSX for the running scenario. Something like this:

var ButtonDoc = React.createClass({
  render: function () {
    let buttonComponent = (
      <Button label="Add" />
    );

    let buttonCode = `<Button label="Add" />`;

    return (
      <div>
        {buttonComponent}
        <pre><code>{buttonCode}</code></pre>
      </div>
    );
  }
});

Question: Is there a way that I can get the string representation of the given React component without the need to replicate the code?

I’m expecting something like this:

var ButtonDoc = React.createClass({
  render: function () {
    let buttonComponent = (
      <Button label="Add" />
    );

    let buttonCode = `${buttonComponent}`;

    return (
      <div>
        {buttonComponent}
        <pre><code>{buttonCode}</code></pre>
      </div>
    );
  }
});

The output of the given code is object [object].

Answer

As I did not find anything that solved my problem, I ended up creating a npm repository to achieve this task.

https://github.com/alansouzati/jsx-to-string

Usage:

import React from 'react';
import jsxToString from 'jsx-to-string';

let Basic = React.createClass({
  render() {
    return (
      <div />
    );
  }
}); //this is your react component

console.log(jsxToString(<Basic test1="test" />)); //outputs: <Basic test1="test" />
Categories
discuss

ReactJS – newlines lose spaces between components

I’m trying to indent my JSX a little nicer, but I’m losing a blank space between components. For example

<span>Saved: { this.props.fetchedTitle + ' ' }
    <AjaxButton css='btn-danger btn-xs' running={this.props.deleting} onClick={this.props.deleteBook} text='Delete' runningText='Deleting' />{' '}
    <BootstrapButton preset='info-xs' onClick={() => this.expand()}>D</BootstrapButton>
</span>

Note the manual space inserted after fetchedTitle and after AjaxButton. If I cram everything onto one line with spaces between the components, the space is inserted for me. When I break things into multiple lines, the space is gone, forcing me to manually add it, even if I manually insert a space before or after the line break.

Is there a way around this? (this ui is just a rough prototype / exploratory exercise – eventually there’ll probably be a more robust layout, but I would still like to understand why React is responding to the jsx the way it is)

Answer

Try this.

<span>Saved: { this.props.fetchedTitle }&nbsp;
    <AjaxButton css='btn-danger btn-xs' running={this.props.deleting} onClick={this.props.deleteBook} text='Delete' runningText='Deleting' />&nbsp;
    <BootstrapButton preset='info-xs' onClick={() => this.expand()}>D</BootstrapButton>
</span>
Categories
discuss

Draw video on canvas HTML5

I’m trying to draw a video on a canvas. To achieve this I capture the onMouseDown and onMouseUp events in Javascript to get the x and y coordinates of each event (that I need to set the position, width and height of the video inside the canvas).

Therefore, every time I draw a video on the canvas, the previous I create should be stopped and the new one has to be played. Two problems:

1) the video doesn’t play (the function only draws the first frame), but his audio does

2) how can I get previously drawn videos to stop?

Demo: http://jsfiddle.net/e3c3kore/

<body>
    <canvas id="canvas" width="800" height="600"></canvas>
</body>



var canvas, context, xStart, yStart, xEnd, yEnd;

canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.addEventListener("mousedown", mouseDown);
canvas.addEventListener("mouseup", mouseUp);

function mouseDown(e) {
    xStart = e.offsetX;
    yStart = e.offsetY;
}

function mouseUp(e) {
    xEnd = e.offsetX;
    yEnd = e.offsetY;
    if (xStart != xEnd && yStart != yEnd) {
    var video = document.createElement("video");
                        video.src = "http://techslides.com/demos/sample-videos/small.mp4";
                        video.addEventListener('loadeddata', function() {
                            video.play();
                            context.drawImage(video, xStart, yStart, xEnd-xStart, yEnd-yStart);
                        });
    }
}

Demo: http://jsfiddle.net/e3c3kore/

Answer

1) The drawImage() method was only be called once. It needs to be called once per frame.

2) Call pause() method to stop video.

For example, the follow code starts a timer loop (for drawing the frames) on mouse up and stops any previous video on mouse down.

var canvas, context, video, xStart, yStart, xEnd, yEnd;

canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.addEventListener("mousedown", mouseDown);
canvas.addEventListener("mouseup", mouseUp);

function mouseDown(e) {
    if (video) {
        video.pause();
        video = null;
        context.clearRect(0, 0, canvas.width, canvas.height);
    }
    xStart = e.offsetX;
    yStart = e.offsetY;
}

function mouseUp(e) {
    xEnd = e.offsetX;
    yEnd = e.offsetY;
    if (xStart != xEnd && yStart != yEnd) {
        video = document.createElement("video");
        video.src = "http://techslides.com/demos/sample-videos/small.mp4";
        video.addEventListener('loadeddata', function() {
            console.log("loadeddata");
            video.play();
            setTimeout(videoLoop, 1000 / 30);
        });
    }
}

function videoLoop() {
    if (video && !video.paused && !video.ended) {
        context.drawImage(video, xStart, yStart, xEnd - xStart, yEnd - yStart);
        setTimeout(videoLoop, 1000 / 30);
    }
}
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..