Categories
discuss

Split div content at cursor position

I need to split an element after user clicks on it and the attr ‘contenteditable’ becomes ‘true’. This fiddle works for the first paragraph but not second because the latter is in a p tag. Similary in this fiddle you will see that when the element has html tags in it, the counter loses accuracy and hence the text before and after the cursor is not what you’d expect.

The assumption here is that the users will split the data in a way that the help tags will stay intact. As pointed out by dandavis here, e.g. the div has <i>Hello</i> <b>Wo*rld</b>, the user will only need to split the div into two divs, first will have <i>Hello</i> and the second div will have <b>Wo*rld</b> in it.

Html:

<div><mark>{DATE}</mark><i>via email: </i><mark><i>{EMAIL- BROKER OR TENANT}</i></mark></div>

JS:

var $splitbut = $('<p class="split-but">Split</p>');
$(this).attr('contenteditable', 'true').addClass('editing').append($splitbut);

var userSelection;
if (window.getSelection) {
    userSelection = window.getSelection();
}
var start = userSelection.anchorOffset;
var end = userSelection.focusOffset;

var before = $(this).html().substr(0, start);
var after = $(this).html().substr(start, $(this).html().length);

The “Split” button is not working as generating the html is not an issue once I get proper “after” and “before” text. Any ideas as to what I am doing wrong here?

Answer

Something like this could work for the specific case you describe

$('div, textarea').on('click', function(e) {
 var userSelection;
    if (window.getSelection) {
        userSelection = window.getSelection();
    }
    var start = userSelection.anchorOffset,
        end = userSelection.focusOffset,
        node = userSelection.anchorNode,
        allText = $(this).text(),
        nodeText = $(node).text();

// before and after inside node
  var nodeBefore = nodeText.substr(0, start);
  var nodeAfter = nodeText.substr(start, nodeText.length);

// before and after for whole of text
    var allExceptNode = allText.split(nodeText),
        before = allExceptNode[0] + nodeBefore,
        after = nodeAfter + allExceptNode[1];

  console.log('Before: ', before);
  console.log('------');
  console.log('After: ', after);

});

Updated demo at https://jsfiddle.net/gaby/vaLz55fv/10/


It might exhibit issues if there are tags whose content is repeated in the whole text. (problem due to splitting)

Categories
discuss

How to style Ionic input type file as a Button

I want to style a ionic file chooser button.

<input type="file" id="myFileInput">

But Ionic don’t have an Input type file button. So how can I get a better looking button than the standard Button with a Choose a File text ?

Answer

If you want only style the <input> element as a button, for example, you can adopt one of the suggested style of this post: http://geniuscarrier.com/how-to-style-a-html-file-upload-button-in-pure-css/

Or another example from CSS-tricks: https://css-tricks.com/snippets/css/custom-file-input-styling-webkitblink/

or this one: http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/

Keep in mind that in a mobile device it may not work and you may need a cordova plugin. For example: https://github.com/apache/cordova-plugin-file

Categories
discuss

How can I change a property in spring environment?

I’m using the spring Environment Bean in my application to get the application configuration properties. I want to change the value of a property in the spring Environment from java code without restarting the application server. How can I do that?

@Service
public void MyService {
    @Autowired
    private Environment environment;

    public void doSomething(){
        String value = environment.getProperty("myproperty");
        ...
    }
}

Answer

The Environment implementation that you will get by default in the Spring context is actually an instance of StandardEnvironment.

StandardEnvironment implements ConfigurableEnvironment, so if you inject a ConfigurableEnvironment instead of the super interface you will be able to make alterations at runtime.

@Service
public void MyService {
    @Autowired private ConfigurableEnvironment environment;
Categories
discuss

OKhttp PUT example

My requirement is to use PUT, send a header and a body to server which will update something in the database.

I just read okHttp documentation and I was trying to use their POST example but it doesn’t work for my use case (I think it might be because the server requires me to use PUT instead of POST).

This is my method with POST:

 public void postRequestWithHeaderAndBody(String url, String header, String jsonBody) {


        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, jsonBody);

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Authorization", header)
                .build();

        makeCall(client, request);
    }

I have tried to search for okHttp example using PUTwith no success, if I need to use PUTmethod is there anyway to use okHttp?

I’m using okhttp:2.4.0 (just in case), thanks on any help!

Answer

Change your .post with .put

public void putRequestWithHeaderAndBody(String url, String header, String jsonBody) {


        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, jsonBody);

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .put(body) //PUT
                .addHeader("Authorization", header)
                .build();

        makeCall(client, request);
    }
Categories
discuss

RxJava – how to invoke subscriber on 4 click events in Android

In android I have a text view defined like this:

<TextView
     android:id="@+id/text1"       
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/hello_world"
     android:textColorHighlight="#000000"
     android:textIsSelectable="true" />

My goal is that after it is clicked 4 times, I want to start a new activity. I need to do this with RXJava; this is a requirement. Or rxAndroid, rxBinding, etc.

My activity looks like this:

public class MainActivity extends Activity implements onClickListener {
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView)findViewById(R.id.text1);
        tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.text1){

        }
    }
}

UPDATE: but this is just the standard way of doing it. There must be a way to do with with reactive rxJava API.

So instead of using a onClicklistener with rxBinding I read we can do this:

RxView.clicks(tv).flatMap(tv -> { 
        // another observable which can throw onError. 
        return Observable.error(null); 
    }).subscribe(object -> { 
        Log.d("CLICK", "textview clicked", start activity);
    }, error -> { 
        Log.d("CLICK", "ERROR");
    });

Can I somehow use a command in rxBinding to make it execute only after 4 clicks? I’m not looking to store a static variable, or use a anonymous class and store a member variable for count in it. There should be an observable method for this.

Answer

You could create an observable to emit each time the button is clicked, then count the total times it has emitted, and use a filter so that your subscriber will only see it once it has emitted four times.

Observable<View> buttonObservable = ViewObservable.clicks(initiateButton, false);
buttonObservable.count()
        .filter(count -> (count >= 4))
        .subscribe(object -> {
            //Your code here
        });
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..